Skip to content

suggest adding a where-clause #44507

Closed
Closed
@nikomatsakis

Description

@nikomatsakis

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.T-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