Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unclear diagnostic with invalid suggestion for lifetime error involving an opaque type #80150

Closed
ben0x539 opened this issue Dec 18, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ben0x539
Copy link
Contributor

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 {
  |                                                       ^^^^^^^^^
  1. 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
  2. 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)

@ben0x539 ben0x539 added the C-bug Category: This is a bug. label Dec 18, 2020
@camelid camelid added A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 18, 2020
@camelid camelid added I-prioritize Issue: Indicates that prioritization has been requested for this issue. P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jan 17, 2021
@camelid
Copy link
Member

camelid commented Jan 18, 2021

Assigning P-medium and removing I-prioritize as discussed in the prioritization working group.

@estebank
Copy link
Contributor

estebank commented Aug 3, 2023

Current output:

error[[E0597]](https://doc.rust-lang.org/stable/error_codes/E0597.html): `n` does not live long enough
 --> src/lib.rs:3:40
  |
2 |     let mut n = None.into_iter();
  |         ----- binding `n` declared here
3 |     let i: &mut dyn Iterator<Item=T> = &mut n;
  |                                        ^^^^^^ borrowed value does not live long enough
4 |     Some(i)
  |     ------- opaque type requires that `n` is borrowed for `'static`
5 | }
  | - `n` dropped here while still borrowed

@estebank estebank closed this as completed Aug 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-impl-trait Area: impl Trait. Universally / existentially quantified anonymous types with static dispatch. A-lifetimes Area: lifetime related A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. C-bug Category: This is a bug. D-confusing Diagnostics: Confusing error or lint that should be reworked. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. P-medium Medium priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants