Open
Description
Argument-free closures can return non-'static
types but be 'static
themselves:
fn foo<'a>() {
let closure = || -> &'a str { "" };
assert_static(closure);
}
fn assert_static<T: 'static>(_: T) {}
However, associated types of 'static
types must be 'static
, too. So the fact that the above compiles results in unsoundness, as demonstrated below:
use std::fmt;
trait Trait {
type Associated;
}
impl<R, F: Fn() -> R> Trait for F {
type Associated = R;
}
fn static_transfers_to_associated<T: Trait + 'static>(
_: &T,
x: T::Associated,
) -> Box<dyn fmt::Display /* + 'static */>
where
T::Associated: fmt::Display,
{
Box::new(x) // T::Associated: 'static follows from T: 'static
}
fn make_static_displayable<'a>(s: &'a str) -> Box<dyn fmt::Display> {
let f = || -> &'a str { "" };
// problem is: the closure type of `f` is 'static
static_transfers_to_associated(&f, s)
}
fn main() {
let d;
{
let x = "Hello World".to_string();
d = make_static_displayable(&x);
}
println!("{}", d);
}
����IV�
@rustbot modify labels: T-compiler, T-lang, A-closures, A-lifetimes, A-traits, A-typesystem, A-associated-items
and someone please add “I-unsound 💥”.
Found this bug while experimenting with variations of #84305.
Without the dyn
keyword, this seems to compile “successfully” since Rust 1.4.0
. Godbolt link.
Metadata
Metadata
Assignees
Labels
Area: Associated items (types, constants & functions)Area: Async & AwaitArea: Closures (`|…| { … }`)Area: Lifetimes / regionsArea: Trait systemArea: Type systemAsync-await issues that have been triaged during a working group meeting.Category: This is a bug.Issue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessHigh priorityStatus: This bug is tracked inside the repo by a `known-bug` test.Relevant to the compiler team, which will review and decide on the PR/issue.Relevant to the language team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
Type
Projects
Status
unknown