Skip to content

RPITITs may imply unsound outlives for late-bound args in signature #133427

Closed
@compiler-errors

Description

@compiler-errors

I tried this code:

use std::sync::Mutex;

static MUTEX: Mutex<Option<&'static str>> = Mutex::new(None);

trait Foo {
    fn foo<'a: 'static>(&self) -> impl Sized;
}

impl Foo for str {
    fn foo<'a: 'static>(&'a self) -> impl Sized + 'a {
        *MUTEX.lock().unwrap() = Some(self);
    }
}

fn call_foo<T: Foo + ?Sized>(s: &T) {
    s.foo();
}

fn main() {
    let s = String::from("hello, world");
    call_foo(s.as_str());
    drop(s);
    println!("> {}", MUTEX.lock().unwrap().unwrap());
}

I expected to see this happen: Compilation failure.

Instead, this happened: Segfault due to UAF

Why?

See comment below.

Meta

rustc --version --verbose:

2024-11-24

Not present on beta or stable.

Metadata

Metadata

Labels

A-impl-traitArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.C-bugCategory: This is a bug.F-return_position_impl_trait_in_trait`#![feature(return_position_impl_trait_in_trait)]`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

No type

Projects

Status

Completed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions