Skip to content

missing hints for elided lifetime mismatch in traits and impl #94462

Closed
@kckeiks

Description

@kckeiks

This merge introduced a helpful hint for a (elided) lifetime mismatch error. However, we don't get the hint when the function is in a trait or an impl.

Given the following code:

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

The current output is:


error[E0623]: lifetime mismatch
  --> src/main.rs:21:35
   |
20 | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                 ---------           --------- these two types are declared with different lifetimes...
21 |     core::mem::swap(&mut slice_a, &mut slice_b);
   |                                   ^^^^^^^^^^^^ ...but data from `slice_b` flows into `slice_a` here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
20 | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |       ++++           ++                     ++

error[E0623]: lifetime mismatch
  --> src/main.rs:21:35
   |
20 | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                 ---------           ---------
   |                 |
   |                 these two types are declared with different lifetimes...
21 |     core::mem::swap(&mut slice_a, &mut slice_b);
   |                                   ^^^^^^^^^^^^ ...but data from `slice_a` flows into `slice_b` here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
20 | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |       ++++           ++                     ++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0623`.

But given the following:

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

The output is missing the hint:


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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0623`.

Ideally the output should look like:

error[E0623]: lifetime mismatch
  --> src/main.rs:30:39
   |
29 |     fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                     ---------           --------- these two types are declared with different lifetimes...
30 |         core::mem::swap(&mut slice_a, &mut slice_b);
   |                                       ^^^^^^^^^^^^ ...but data from `slice_b` flows into `slice_a` here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
29 |     fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |           ++++           ++                     ++

error[E0623]: lifetime mismatch
  --> src/main.rs:30:39
   |
29 |     fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                     ---------           ---------
   |                     |
   |                     these two types are declared with different lifetimes...
30 |         core::mem::swap(&mut slice_a, &mut slice_b);
   |                                       ^^^^^^^^^^^^ ...but data from `slice_a` flows into `slice_b` here
   |
   = note: each elided lifetime in input position becomes a distinct lifetime
help: consider introducing a named lifetime parameter
   |
29 |     fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |           ++++           ++                     ++

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0623`.

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