Skip to content

Don't suggest let bindings if they don't help with borrows #52049

Closed
@oli-obk

Description

@oli-obk
fn foo(_: &'static u32) {}

fn unpromotable<T>(t: T) -> T { t }

fn main() {
    foo(&unpromotable(5u32));
}

suggests

error[E0597]: borrowed value does not live long enough
 --> src/main.rs:6:10
  |
6 |     foo(&unpromotable(5u32));
  |          ^^^^^^^^^^^^^^^^^^ - temporary value only lives until here
  |          |
  |          temporary value does not live long enough
  |
  = note: borrowed value must be valid for the static lifetime...
  = note: consider using a `let` binding to increase its lifetime

but

    let x = unpromotable(5u32);
    foo(&x);

still won't give us a 'static lifetime.

We should not emit the note if the required lifetime is 'static

Instructions

  1. search for the note inside the source
  2. I presume there's some code already checking for producing the "borrowed value must be valid for the static lifetime" message, find that code and bubble up the knowledge about that until you have it in the scope of the "consider using a let binding to increase its lifetime" message
  3. Don't emit the message in that case
  4. This'll already fail a bunch of tests, I don't think you need any new tests. Running ./x.py test --stage 1 src/test/{ui,compile-fail} --bless should succeed and give you a bunch of changes to commit. Check that all changes make sense and just commit them.

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsE-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions