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

Suggest adding a lifetime parameter on "data from X flows into Y" #78312

Closed
Aaron1011 opened this issue Oct 23, 2020 · 2 comments
Closed

Suggest adding a lifetime parameter on "data from X flows into Y" #78312

Aaron1011 opened this issue Oct 23, 2020 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Aaron1011
Copy link
Member

The following code:

fn foo(mut first: &u8, second: &u8) {
    first = second;
}

fn main() {}

produces the following error:

error[E0623]: lifetime mismatch
 --> src/lib.rs:2:13
  |
1 | fn foo(mut first: &u8, second: &u8) {
  |                   ---          --- these two types are declared with different lifetimes...
2 |     first = second;
  |             ^^^^^^ ...but data from `second` flows into `first` here

We could suggest changing the signature to fn foo<'a>(mut first: &'a u8, second: &'a u8)

@Aaron1011 Aaron1011 added C-enhancement Category: An issue proposing an enhancement or a PR with one. A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Oct 23, 2020
@JohnTitor JohnTitor added D-papercut Diagnostics: An error or lint that needs small tweaks. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-diagnostics Area: Messages for errors, warnings, and lints labels Oct 25, 2020
@BGR360
Copy link
Contributor

BGR360 commented Dec 29, 2021

Current output on nightly:

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

Send your kudos to @Nilstrieb : #90179

@estebank
Copy link
Contributor

The current output is from NLL, but the suggestion is still there :)

error: lifetime may not live long enough
 --> src/main.rs:2:5
  |
1 | fn foo(mut first: &u8, second: &u8) {
  |                   -            - let's call the lifetime of this reference `'1`
  |                   |
  |                   let's call the lifetime of this reference `'2`
2 |     first = second;
  |     ^^^^^^^^^^^^^^ assignment requires that `'1` must outlive `'2`
  |
help: consider introducing a named lifetime parameter
  |
1 | fn foo<'a>(mut first: &'a u8, second: &'a u8) {
  |       ++++             ++              ++

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 A-lifetimes Area: Lifetimes / regions A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-enhancement Category: An issue proposing an enhancement or a PR with one. D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. D-papercut Diagnostics: An error or lint that needs small tweaks. 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

4 participants