-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #124682 - estebank:issue-40990, r=pnkfelix
Suggest setting lifetime in borrowck error involving types with elided lifetimes ``` error: lifetime may not live long enough --> $DIR/ex3-both-anon-regions-both-are-structs-2.rs:7:5 | LL | fn foo(mut x: Ref, y: Ref) { | ----- - has type `Ref<'_, '1>` | | | has type `Ref<'_, '2>` LL | x.b = y.b; | ^^^^^^^^^ assignment requires that `'1` must outlive `'2` | help: consider introducing a named lifetime parameter | LL | fn foo<'a>(mut x: Ref<'a, 'a>, y: Ref<'a, 'a>) { | ++++ ++++++++ ++++++++ ``` As can be seen above, it currently doesn't try to compare the `ty::Ty` lifetimes that diverged vs the `hir::Ty` to correctly suggest the following ``` help: consider introducing a named lifetime parameter | LL | fn foo<'a>(mut x: Ref<'_, 'a>, y: Ref<'_, 'a>) { | ++++ ++++++++ ++++++++ ``` but I believe this to still be an improvement over the status quo. Fix #40990.
- Loading branch information
Showing
42 changed files
with
698 additions
and
216 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.fixed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
//@ run-rustfix | ||
#![allow(dead_code)] | ||
struct Foo { | ||
field: i32, | ||
} | ||
|
||
impl Foo { | ||
fn foo<'a>(&self, x: &'a i32) -> &'a i32 { | ||
x | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
} | ||
|
||
fn main() {} |
17 changes: 8 additions & 9 deletions
17
tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
//@ run-rustfix | ||
#![allow(dead_code)] | ||
struct Foo { | ||
field: i32 | ||
field: i32, | ||
} | ||
|
||
impl Foo { | ||
fn foo<'a>(&self, x: &'a i32) -> &i32 { | ||
|
||
x | ||
//~^ ERROR lifetime may not live long enough | ||
|
||
} | ||
|
||
fn foo<'a>(&self, x: &'a i32) -> &i32 { | ||
x | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
} | ||
|
||
fn main() { } | ||
fn main() {} |
20 changes: 12 additions & 8 deletions
20
tests/ui/lifetimes/lifetime-errors/ex1-return-one-existing-name-return-type-is-anon.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,17 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/ex1-return-one-existing-name-return-type-is-anon.rs:8:5 | ||
--> $DIR/ex1-return-one-existing-name-return-type-is-anon.rs:9:9 | ||
| | ||
LL | fn foo<'a>(&self, x: &'a i32) -> &i32 { | ||
| -- - let's call the lifetime of this reference `'1` | ||
| | | ||
| lifetime `'a` defined here | ||
LL | | ||
LL | x | ||
| ^ method was supposed to return data with lifetime `'1` but it is returning data with lifetime `'a` | ||
LL | fn foo<'a>(&self, x: &'a i32) -> &i32 { | ||
| -- - let's call the lifetime of this reference `'1` | ||
| | | ||
| lifetime `'a` defined here | ||
LL | x | ||
| ^ method was supposed to return data with lifetime `'1` but it is returning data with lifetime `'a` | ||
| | ||
help: consider reusing a named lifetime parameter and update trait if needed | ||
| | ||
LL | fn foo<'a>(&self, x: &'a i32) -> &'a i32 { | ||
| ++ | ||
|
||
error: aborting due to 1 previous error | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.