Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
kckeiks opened this issue Feb 28, 2022 · 2 comments
Closed

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

kckeiks opened this issue Feb 28, 2022 · 2 comments
Assignees
Labels
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.

Comments

@kckeiks
Copy link
Contributor

kckeiks commented Feb 28, 2022

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`.

@kckeiks kckeiks 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 Feb 28, 2022
@kckeiks
Copy link
Contributor Author

kckeiks commented Feb 28, 2022

@rustbot claim

@kckeiks
Copy link
Contributor Author

kckeiks commented Feb 28, 2022

Claiming this because I came up with a fix while working on another issue. No problem if this issue gets denied though. The fix is small.

kckeiks added a commit to kckeiks/rust that referenced this issue Feb 28, 2022
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Mar 2, 2022
…int-for-traits, r=estebank

Suggest adding a new lifetime parameter when two elided lifetimes should match up for traits and impls.

Suggest adding a new lifetime parameter when two elided lifetimes should match up for functions in traits and impls.

Issue rust-lang#94462
@kckeiks kckeiks closed this as completed Mar 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.
Projects
None yet
Development

No branches or pull requests

1 participant