Skip to content

"lifetime 'static required" - poor diagnostics #90600

New issue

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

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

Already on GitHub? # to your account

Open
rukai opened this issue Nov 5, 2021 · 3 comments
Open

"lifetime 'static required" - poor diagnostics #90600

rukai opened this issue Nov 5, 2021 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@rukai
Copy link
Contributor

rukai commented Nov 5, 2021

Given the following code:

use std::cell::RefCell;
use std::io::Read;

fn main() {
    let foo: &[u8] = &[0, 1];
    inner(foo);
}

fn inner(mut foo: &[u8]) {
    //let mut foo: &[u8] = &[0, 1]; // uncommenting this line gives an improved error message
    let refcell = RefCell::new(&mut foo);
    let read = &refcell as &RefCell<dyn Read>;

    read_thing(read);
}

fn read_thing(refcell: &RefCell<dyn Read>) {
}

The current output is:

   Compiling foo_rust v0.1.0 (/home/rukai/Foo/foo_rust)
error[E0621]: explicit lifetime required in the type of `foo`
  --> src/main.rs:14:16
   |
14 |     read_thing(read);
   |                ^^^^ lifetime `'static` required

Ideally the output should look like:

error[E0621]: explicit lifetime required in the type of `foo`
  --> $DIR/issue-90600.rs:10:16
   |
LL | fn inner(mut foo: &[u8]) {
   |                   ----- the type of `foo` does not have lifetime `'static'`
...
   |
LL |     let refcell = RefCell::new(&mut foo);
   |                   ---------------------- the type of `refcell` takes the lifetime of type of `foo` here
...
LL |     read_thing(read);
   |                ^^^^ lifetime `'static` required

Problem occurs on both latest stable and latest nightly. (1.56 and 2021-11-04}

@rukai rukai added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 5, 2021
@rukai
Copy link
Contributor Author

rukai commented Nov 7, 2021

I did some investigation and noticed that this issue is at least partially reproduced here
https://github.com/rust-lang/rust/blob/bc6330d47a7a81e25f492a837822121a3cd88158/src/test/ui/issues/issue-46983.stderr
https://github.com/rust-lang/rust/blob/be5fe051a843cfbbc1ba4fcd347b641417181b8f/src/test/ui/issues/issue-46983.rs
I'll investigate further and see if I can put together a PR to improve the situation.

@estebank estebank added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. labels Nov 13, 2021
@rukai
Copy link
Contributor Author

rukai commented Nov 13, 2021

I came across this issue #86759 which is describing the same problem but has what seems to me a better solution

@rukai
Copy link
Contributor Author

rukai commented Nov 13, 2021

I experimented with skipping try_report_named_anon_conflict when static is required and its giving much better diagnostics, so ill make a PR for that once #90667 is merged.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 13, 2021
…ostics, r=estebank

Improve diagnostics when a static lifetime is expected

Makes progress towards rust-lang#90600

The diagnostics here were previously entirely removed due to giving a misleading suggestion but if we instead provide an informative label in that same location it should better help the user understand the situation.

I included the example from the issue as it demonstrates an area where the diagnostics are still lacking.
Happy to remove that if its just adding noise atm.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 13, 2021
…ostics, r=estebank

Improve diagnostics when a static lifetime is expected

Makes progress towards rust-lang#90600

The diagnostics here were previously entirely removed due to giving a misleading suggestion but if we instead provide an informative label in that same location it should better help the user understand the situation.

I included the example from the issue as it demonstrates an area where the diagnostics are still lacking.
Happy to remove that if its just adding noise atm.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 17, 2021
…ostics, r=estebank

Improve diagnostics when a static lifetime is expected

Makes progress towards rust-lang#90600

The diagnostics here were previously entirely removed due to giving a misleading suggestion but if we instead provide an informative label in that same location it should better help the user understand the situation.

I included the example from the issue as it demonstrates an area where the diagnostics are still lacking.
Happy to remove that if its just adding noise atm.
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

2 participants