Skip to content

Mismatched return type error spuriously masked #144084

@Plecra

Description

@Plecra

Code

/*
error: lifetime may not live long enough
 --> src/lib.rs:8:12
  |
7 | fn foo1<'a, 'b>(a: &'a mut (), b: &'b mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
8 |     let x: &'a mut () = b;
  |            ^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`

error: lifetime may not live long enough
 --> src/lib.rs:9:5
  |
7 | fn foo1<'a, 'b>(a: &'a mut (), b: &'b mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
8 |     let x: &'a mut () = b;
9 |     x
  |     ^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
  |
  = help: consider adding the following bound: `'a: 'b`

help: `'a` and `'b` must be the same: replace one with the other
*/
fn foo1<'a, 'b>(a: &'a mut (), b: &'b mut ()) -> &'b mut () {
    let x: &'a mut () = b;
    x
}
/*error: lifetime may not live long enough
 --> src/lib.rs:4:12
  |
1 | fn foo2<'a, 'b>(a: &'b mut &'a mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
...
4 |     let x: &'a mut () = &mut *a;
  |            ^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`
*/
fn foo2<'a, 'b>(a: &'b mut &'a mut ()) -> &'b mut () {
    // the error is that the user's return type is wrong:
    // this should be `&'b mut ()`
    let x: &'a mut () = &mut *a;
    x
}

Current output

error: lifetime may not live long enough
 --> src/lib.rs:4:12
  |
1 | fn foo2<'a, 'b>(a: &'b mut &'a mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
...
4 |     let x: &'a mut () = &mut *a;
  |            ^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`

Desired output

error: lifetime may not live long enough
 --> src/lib.rs:4:12
  |
1 | fn foo2<'a, 'b>(a: &'b mut &'a mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
...
4 |     let x: &'a mut () = &mut *a;
  |            ^^^^^^^^^^ type annotation requires that `'b` must outlive `'a`
  |
  = help: consider adding the following bound: `'b: 'a`

error: lifetime may not live long enough
 --> src/lib.rs:9:5
  |
7 | fn foo2<'a, 'b>(a: &'b mut &'a mut ()) -> &'b mut () {
  |         --  -- lifetime `'b` defined here
  |         |
  |         lifetime `'a` defined here
8 |     let x: &'a mut () = &mut *a;
9 |     x
  |     ^ function was supposed to return data with lifetime `'b` but it is returning data with lifetime `'a`
  |
  = help: consider adding the following bound: `'a: 'b`

Rationale and extra context

When a type annotation is incorrect, it leads to two potential errors - the input is wrong, or the output is wrong. foo1 demonstrates both of these errors being presented for a lifetime incompatibility. For some reason, in foo2 the return type error is not given, making the problem unclear (Especially in the source case, where this is caused by incorrect use of Self inside an impl block). Both errors should still be shown.

#112947 appears potentially related.

Other cases

Rust Version

rustc 1.88.0 stable

(I'm sorry. I'm not sure how to get this out of the playground)

Anything else?

No response

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