Repository navigation
'static closures/FnDefs/futures with non-'static return type are unsound #84366
Copy link
Copy link
Open
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-async-awaitArea: Async & AwaitArea: Async & AwaitA-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityHigh priorityS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.Status: This bug is tracked inside the repo by a `known-bug` test.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types team, which will review and decide on the PR/issue.Relevant to the types team, which will review and decide on the PR/issue.
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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-associated-itemsArea: Associated items (types, constants & functions)Area: Associated items (types, constants & functions)A-async-awaitArea: Async & AwaitArea: Async & AwaitA-closuresArea: Closures (`|…| { … }`)Area: Closures (`|…| { … }`)A-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsA-trait-systemArea: Trait systemArea: Trait systemA-type-systemArea: Type systemArea: Type systemAsyncAwait-TriagedAsync-await issues that have been triaged during a working group meeting.Async-await issues that have been triaged during a working group meeting.C-bugCategory: This is a bug.Category: This is a bug.I-unsoundIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessIssue: A soundness hole (worst kind of bug), see: https://en.wikipedia.org/wiki/SoundnessP-highHigh priorityHigh priorityS-bug-has-testStatus: This bug is tracked inside the repo by a `known-bug` test.Status: This bug is tracked inside the repo by a `known-bug` test.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language teamRelevant to the language teamT-typesRelevant to the types 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
- StatusShow more project fieldsopen/unblocked
Activity
steffahn commentedon Apr 21, 2021
This also works with functions
but only since Rust
1.20. I don’t know if this would be better put as a separate issue since that’s probably a regression then.1.19gives(compiler explorer)
steffahn commentedon Apr 21, 2021
The version with functions regressed (started compiling without error) at
nightly-2017-06-29.This includes
Looking at those, almost certainly #42417 is the relevant PR.
apiraino commentedon Apr 21, 2021
Assigning priority as discussed as part of the Prioritization Working Group procedure and removing
I-prioritize.@rustbot label -I-prioritize +P-high
18 remaining items
Rollup merge of rust-lang#110480 - whtahy:105107/known-bug-tests-for-…
Rollup merge of rust-lang#110480 - whtahy:105107/known-bug-tests-for-…
Rollup merge of rust-lang#110480 - whtahy:105107/known-bug-tests-for-…
Rollup merge of rust-lang#110480 - whtahy:105107/known-bug-tests-for-…
[-]'static closures with non-'static return type are unsound[/-][+]'static closures/FnDefs/futures with non-'static return type are unsound[/+]Ensure `'static` lifetime for future output
Ensure `'static` lifetime for future output
theemathas commentedon Apr 26, 2025
Here's another way to use this unsoundness (or something similar) to implement a generic lifetime extension function.
This code started compiling in Rust 1.66.0. It gave a compiler error in Rust 1.65.0. The change was probably due to #95474
lcnr commentedon Mar 30, 2026
This unsoundness also exists for unconstrained lifetime on impls
oli-obk commentedon May 12, 2026
Fun. I think for
FnDefs the soundess issue was accidentally fixed (#70164) and then reverted (#71218) because of #70917theemathas commentedon Aug 22, 2026
Another way to exploit this, discovered at #t-compiler > Reporting a number of soundness issues in rustc @ 💬