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 where-clause #44507

Closed
nikomatsakis opened this issue Sep 11, 2017 · 2 comments
Closed

suggest adding a where-clause #44507

nikomatsakis opened this issue Sep 11, 2017 · 2 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions 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. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nikomatsakis
Copy link
Contributor

In some cases, we can suggest the user add a where clause. This applies in cases where there are two named lifetimes already:

fn foo<'a, 'b>(mut x: Vec<&'a u32>, y: &'b u32)  {
    x.push(y);
}

fn main() { }

Here we currently print:

error[E0623]: lifetime mismatch
 --> src/main.rs:2:12
  |
1 | fn foo<'a, 'b>(mut x: Vec<&'a u32>, y: &'b u32)  {
  |                           -------      ------- these two types are declared with different lifetimes...
2 |     x.push(y);
  |            ^ ...but data from `y` flows into `x` here

It'd be nice to add:

help: consider adding a where-clause like `where 'b: 'a`

Typically, we should only do this outside of trait impls, however, because otherwise the user may not be free to modify the signature. Or, if the trait is declared inside the same crate, we could find the corresponding method in the trait and add a note saying "the where-clause would also have to be added here" or some such thing.

@nikomatsakis nikomatsakis 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 Sep 11, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Sep 17, 2017
@Dylan-DPC-zz
Copy link

Will try this 👍

@estebank estebank added A-lifetimes Area: Lifetimes / regions D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. labels Oct 15, 2019
@Centril
Copy link
Contributor

Centril commented Mar 26, 2020

error: lifetime may not live long enough
 --> src/main.rs:4:5
  |
3 | fn foo<'a, 'b>(mut x: Vec<&'a u32>, y: &'b u32)  {
  |        --  -- lifetime `'b` defined here
  |        |
  |        lifetime `'a` defined here
4 |     x.push(y);
  |     ^^^^^^^^^ argument requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`

with #![feature(nll)], so I think we can close this.

@Centril Centril closed this as completed Mar 26, 2020
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 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. 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

5 participants