Skip to content

"these two types are declared with different lifetimes..." not triggering within structs #44508

Closed
@nikomatsakis

Description

@nikomatsakis

This example:

struct Ref<'a> { x: &'a u32 }

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

fn main() { }

currently prints an old-school and crappy error:

error[E0308]: mismatched types
 --> src/main.rs:4:12
  |
4 |     x.push(y);
  |            ^ lifetime mismatch
  |
  = note: expected type `Ref<'a>`
             found type `Ref<'b>`
note: the lifetime 'b as defined on the function body at 3:1...
 --> src/main.rs:3:1
  |
3 | / fn foo<'a, 'b>(mut x: Vec<Ref<'a>>, y: Ref<'b>)  {
4 | |     x.push(y);
5 | | }
  | |_^
note: ...does not necessarily outlive the lifetime 'a as defined on the function body at 3:1
 --> src/main.rs:3:1
  |
3 | / fn foo<'a, 'b>(mut x: Vec<Ref<'a>>, y: Ref<'b>)  {
4 | |     x.push(y);
5 | | }
  | |_^

It should print something like:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-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