Skip to content

"lifetime 'static required" - poor diagnostics #90600

Open
@rukai

Description

@rukai

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}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-papercutDiagnostics: An error or lint that needs small tweaks.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions