Skip to content

Unconstrained lifetime in associated type using trait object leads to unsound lifetime extension and ICE #130347

@konnorandrews

Description

@konnorandrews

I was playing around with defining higher ranked types using trait objects and tried the following pattern.

trait A<T>: B<T = T> {}

trait B {
    type T;
}

To my surprise trait objects of the form dyn for<'a> A<&'a T> were allowed. After some experimentation I found that this leads to UB in safe rust.

trait A<T>: B<T = T> {}

trait B {
    type T;
}

struct Erase<T: ?Sized + B>(T::T);

fn main() {
    let x = {
        let x = String::from("hello");
        
        Erase::<dyn for<'a> A<&'a _>>(x.as_str())
    };
    
    dbg!(x.0);
}
[src/main.rs:16:5] x.0 = "\0\0\0\0\0"

playground

This is likely related to 25860 somehow, but I haven't seen this flavor before.

Meta

This effects all versions from latest nightly to 1.33.0 (on 1.32.0 it causes a ICE, and before it wasn't allowed).

Metadata

Metadata

Labels

A-dyn-traitArea: trait objects, vtable layoutC-bugCategory: This is a bug.I-ICEIssue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityT-typesRelevant to the types team, which will review and decide on the PR/issue.

Type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions