Skip to content

Unsoundness in type checking of trait impls. Differences in implied lifetime bounds are not considered. #80176

Closed
@steffahn

Description

@steffahn

Applies to current stable and nightly. See comment further down for a simplified example and one that works all the way since Rust 1.19.

type Ty = Box<&'static u8>;
trait Bad<'a> {
    fn f<'b>(x: &'static &'a Ty, y: &'b Ty) -> &'static Ty;
}

impl<'a> Bad<'a> for () {
    // NOTE that this signature does _not_ match the trait definition
    // (the first argument has different lifetimes)
    fn f<'b>(mut _x: &'static &'b Ty, y: &'b Ty) -> &'static Ty {
        let y = Box::new(y);
        let y = Box::leak(y);
        _x = y;
        foo(_x)
    }
}

fn foo<'b>(x: &'static &'b Ty) -> &'static Ty {
    x
}

fn main() {
    let v = Box::new(&42);
    let r = &v;
    let z: &_ = Box::leak(Box::new(Box::new(&0)));
    let z: &_ = Box::leak(Box::new(z));
    let r = <() as Bad<'static>>::f(z, r);
    drop(v);
    let _x = Box::new(0usize);
    println!("{}", r);
}

(Playground)

Errors:

   Compiling playground v0.0.1 (/playground)
    Finished release [optimized] target(s) in 0.94s
     Running `target/release/playground`
timeout: the monitored command dumped core
/playground/tools/entrypoint.sh: line 11:     7 Segmentation fault      timeout --signal=KILL ${timeout} "$@"

@rustbot modify labels: T-compiler, C-bug, A-lifetimes, A-traits
@rustbot prioritize

Metadata

Metadata

Labels

A-lifetimesArea: Lifetimes / regionsA-trait-systemArea: Trait systemA-type-systemArea: Type systemC-bugCategory: This is a bug.E-help-wantedCall for participation: Help is requested to fix this issue.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-mediumMedium priorityT-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.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions