Specialization and lifetime dispatch

280034b
Opened by David Tolnay at 2023-04-27 19:30:55

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)
}
  1. Nominating for a priority assignment, though I'm guessing this is a "fixed by chalk, we hope" issue.

    Mark Rousskov at 2017-07-27 18:33:27

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

    Gábor Lehel at 2017-07-27 18:54:01

  3. triage: P-medium

    Known problem, doin' what we can here!

    Niko Matsakis at 2017-08-24 20:40:09