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

Add beginner friendly lifetime elision hint to E0623 #90170

Closed
arifd opened this issue Oct 22, 2021 · 4 comments
Closed

Add beginner friendly lifetime elision hint to E0623 #90170

arifd opened this issue Oct 22, 2021 · 4 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

@arifd
Copy link

arifd commented Oct 22, 2021

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

@arifd arifd 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 Oct 22, 2021
@Noratrieb
Copy link
Member

@rustbot claim

@Noratrieb
Copy link
Member

We could maybe even add a suggestion for adding the explicit generic lifetime

Noratrieb added a commit to Noratrieb/rust that referenced this issue Oct 22, 2021
Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't

Issue rust-lang#90170
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 26, 2021
…-hint, r=estebank

Add beginner friendly lifetime elision hint to E0623

Address rust-lang#90170

Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't.

Example:

```
error[E0623]: lifetime mismatch
  --> $DIR/issue-90170-elision-mismatch.rs:2:35
   |
LL | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                 ---------           --------- these two types are declared with different lifetimes...
LL |     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: Explicitly declare a lifetime and assign it to both
   |
LL | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |       ++++           ++                     ++

```

for

```rust
fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
    core::mem::swap(&mut slice_a, &mut slice_b);
}
```
Noratrieb added a commit to Noratrieb/rust that referenced this issue Nov 3, 2021
Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't

Issue rust-lang#90170

This also changes the tests introduced by the previous commits because of another rustc issue (rust-lang#90258)
bors added a commit to rust-lang-ci/rust that referenced this issue Nov 4, 2021
…int, r=estebank

Add beginner friendly lifetime elision hint to E0623

Address rust-lang#90170

Suggest adding a new lifetime parameter when two elided lifetimes should match up but don't.

Example:

```
error[E0623]: lifetime mismatch
  --> $DIR/issue-90170-elision-mismatch.rs:2:35
   |
LL | fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
   |                 ---------           --------- these two types are declared with different lifetimes...
LL |     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: explicitly declare a lifetime and assign it to both
   |
LL | fn foo<'a>(slice_a: &'a mut [u8], slice_b: &'a mut [u8]) {
   |       ++++           ++                     ++

```

for

```rust
fn foo(slice_a: &mut [u8], slice_b: &mut [u8]) {
    core::mem::swap(&mut slice_a, &mut slice_b);
}
```
@Noratrieb
Copy link
Member

I've implemented it and it has been merged, I hope you like it! (this can be closed)

@arifd
Copy link
Author

arifd commented Nov 4, 2021

Great stuff. Congratulations and what an impressively fast turnaround!

@arifd arifd closed this as completed Nov 4, 2021
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

2 participants