Mismatched return type error spuriously masked
Nobody has claimed this yet.
- Dominant language
- Rust
- Stars
- 119k
- Forks
- 16.1k
- PR merge metrics
- PR metrics pending
Description
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.
https://github.com/rust-lang/rust/issues/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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Reproduce the foo2 example in src/lib.rs with rustc 1.88.0 and compare its diagnostics with the foo1 example and the desired output. Trace the compiler path responsible for suppressing the return-type lifetime error, then add coverage showing that both errors are emitted for foo2.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- rust
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100