Skip to content

Specialization and lifetime dispatch #40582

Open
@dtolnay

Description

@dtolnay

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)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-specializationArea: Trait impl specializationB-unstableBlocker: Implemented in the nightly compiler and unstable.C-bugCategory: This is a bug.F-specialization`#![feature(specialization)]`I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-typesRelevant to the types team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions