rust-lang / rust

Empowering everyone to build reliable and efficient software.
https://www.rust-lang.org
Other
97.53k stars 12.61k forks source link

Specialization and lifetime dispatch #40582

Open dtolnay opened 7 years ago

dtolnay commented 7 years ago

I see this briefly mentioned in #31844 but no existing issue tracking the incorrect behavior.

Something in the following code is unsound. It should not be possible to implement make_static.

#![feature(specialization)]

trait FromRef<'a, T: ?Sized> {
    fn from_ref(r: &'a T) -> Self;
}

impl<'a, T: ?Sized> FromRef<'a, T> for &'a T {
    fn from_ref(r: &'a T) -> Self {
        r
    }
}

impl<'a, T: ?Sized, R> FromRef<'a, T> for R {
    default fn from_ref(_: &'a T) -> Self {
        unimplemented!()
    }
}

fn main() {
    let s = "specialization".to_owned();
    println!("{:?}", make_static(s.as_str()));
}

fn make_static<T: ?Sized>(data: &T) -> &'static T {
    fn helper<T: ?Sized, R>(data: &T) -> R {
        R::from_ref(data)
    }
    helper(data)
}
Mark-Simulacrum commented 7 years ago

Nominating for a priority assignment, though I'm guessing this is a "fixed by chalk, we hope" issue.

glaebhoerl commented 7 years ago

(Is this not the same as https://internals.rust-lang.org/t/shipping-specialization-a-story-of-soundness/5507 ?)

nikomatsakis commented 7 years ago

triage: P-medium

Known problem, doin' what we can here!