Closed
Description
This code is invalid because it returns a reference to a local:
fn foo<T: 'static>() -> Option<impl Iterator<Item = T>> {
let mut n = None.into_iter();
let i: &mut dyn Iterator<Item=T> = &mut n;
Some(i)
}
It produces this diagnostic:
error[E0597]: `n` does not live long enough
--> src/lib.rs:3:40
|
1 | fn foo<T: 'static>() -> Option<impl Iterator<Item=T>> {
| ----------------------------- opaque type requires that `n` is borrowed for `'static`
2 | let mut n = None.into_iter();
3 | let i: &mut dyn Iterator<Item=T> = &mut n;
| ^^^^^^ borrowed value does not live long enough
4 | Some(i)
5 | }
| - `n` dropped here while still borrowed
|
help: you can add a bound to the opaque type to make it last less than `'static` and match `'static`
|
1 | fn foo<T: 'static>() -> Option<impl Iterator<Item=T>> + 'static {
| ^^^^^^^^^
- I don't understand what "help: you can add a bound to the opaque type to make it last less than
'static
and match'static
" means - The suggestion(?) of
Option<impl Iterator<Item=T>> + 'static
isn't valid
It's probably trying to say Option<impl Iterator<Item=T> + 'static>
, if we try that, we get a more straightforward error message.
I'm not sure if this is just #69982, but that one seems to want a closure-specific other diagnostic rather than being generally confused like me.
(tried this on playground with stable 1.48 and nightly)
Metadata
Metadata
Assignees
Labels
Area: Messages for errors, warnings, and lintsArea: `impl Trait`. Universally / existentially quantified anonymous types with static dispatch.Area: Lifetimes / regionsArea: Suggestions generated by the compiler applied by `cargo fix`Category: This is a bug.Diagnostics: Confusing error or lint that should be reworked.Diagnostics: A structured suggestion resulting in incorrect code.Medium priorityRelevant to the compiler team, which will review and decide on the PR/issue.