Skip to content

Add beginner friendly lifetime elision hint to E0623 #90170

Closed
@arifd

Description

@arifd

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=4e10570825edd99bc5f0f03fe82fa34f

fn foo(slice_a: &mut [u8], slice_b: &mut[u8]) {
    core::mem::swap(&mut slice_a, &mut slice_b);
}

fn main() {
    let a = [1u8,2,3];
    let b = [4u8,5,6];
    foo(&mut a, &mut b);
}

The current output is:

error[E0623]: lifetime mismatch
 --> src/main.rs:2:35
  |
1 | fn foo(slice_a: &mut [u8], slice_b: &mut[u8]) {
  |                 ---------           -------- these two types are declared with different lifetimes...
2 |     core::mem::swap(&mut slice_a, &mut slice_b);
  |                                   ^^^^^^^^^^^^ ...but data from `slice_b` flows into `slice_a` here

error[E0623]: lifetime mismatch
 --> src/main.rs:2:35
  |
1 | fn foo(slice_a: &mut [u8], slice_b: &mut[u8]) {
  |                 ---------           --------
  |                 |
  |                 these two types are declared with different lifetimes...
2 |     core::mem::swap(&mut slice_a, &mut slice_b);
  |                                   ^^^^^^^^^^^^ ...but data from `slice_a` flows into `slice_b` here

For more information about this error, try `rustc --explain E0623`.
error: could not compile `playground` due to 2 previous errors

Ideally the output should look (something) like:

Hint: Each elided lifetime in input position becomes a distinct lifetime. To avoid this, explicitly declare a lifetime and assign it to both

Beginners likely aren't aware of lifetime elision rules, they may be thinking "but there are NO lifetimes here"...

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-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