Closed
Description
The following UB compiles: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=4cc89e9d4c133a6398f297e6f56e07f2
#![feature(type_alias_impl_trait)]
use core::fmt::Display;
type Opaque<'a> = impl Sized + 'static;
fn define<'a>() -> Opaque<'a> {}
trait Trait {
type Assoc: Display;
}
impl<'a> Trait for Opaque<'a> {
type Assoc = &'a str;
}
// ======= Exploit =======
fn extend<T: Trait + 'static>(s: T::Assoc) -> Box<dyn Display> {
Box::new(s)
}
fn main() {
let val = extend::<Opaque<'_>>(&String::from("blah blah blah"));
println!("{}", val);
}
The impl is invalid because the lifetime parameter 'a
appears unconstrained in the impl header. It should be rejected for the same reason that projection types doesn't constraint impl generics:
impl<'a> Trait for <Opaque<'a> as Trait>::Assoc {
//~^ ERROR the lifetime parameter `'a` is not constrained
type Assoc = &'a str;
}
@rustbot label C-bug T-types I-unsound requires-nightly F-type_alias_impl_trait