-
Notifications
You must be signed in to change notification settings - Fork 12.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #105805 - yanchen4791:issue-105227-fix, r=estebank
Suggest adding named lifetime when the return contains value borrowed from more than one lifetimes of function inputs fix for #105227. The problem: The suggestion of adding an explicit `'_` lifetime bound is **incorrect** when the function's return type contains a value which could be borrowed from more than one lifetimes of the function's inputs. Instead, a named lifetime parameter can be introduced in such a case. The solution: Checking the number of elided lifetimes in the function signature. If more than one lifetimes found in the function inputs when the suggestion of adding explicit `'_` lifetime, change it to using named lifetime parameter `'a` instead.
- Loading branch information
Showing
8 changed files
with
184 additions
and
40 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
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,26 @@ | ||
// Regression test for issue #105227. | ||
|
||
// run-rustfix | ||
#![allow(warnings)] | ||
fn chars0<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a { | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
v.0.chars().chain(v.1.chars()) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds | ||
} | ||
|
||
fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator<Item = char> + 'a { | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
v0.chars().chain(v1.chars()) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound | ||
} | ||
|
||
fn chars2<'b>(v0 : &'b str, v1 : &'b str, v2 : &'b str) -> | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b` | ||
(impl Iterator<Item = char> + 'b , &'b str) | ||
{ | ||
(v0.chars().chain(v1.chars()), v2) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound | ||
} | ||
|
||
fn main() { | ||
} |
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,26 @@ | ||
// Regression test for issue #105227. | ||
|
||
// run-rustfix | ||
#![allow(warnings)] | ||
fn chars0(v :(& str, &str)) -> impl Iterator<Item = char> { | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
v.0.chars().chain(v.1.chars()) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds | ||
} | ||
|
||
fn chars1(v0 : & str, v1 : &str) -> impl Iterator<Item = char> { | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
v0.chars().chain(v1.chars()) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound | ||
} | ||
|
||
fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) -> | ||
//~^ HELP to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b` | ||
(impl Iterator<Item = char>, &'b str) | ||
{ | ||
(v0.chars().chain(v1.chars()), v2) | ||
//~^ ERROR hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bound | ||
} | ||
|
||
fn main() { | ||
} |
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,47 @@ | ||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds | ||
--> $DIR/issue-105227.rs:7:5 | ||
| | ||
LL | fn chars0(v :(& str, &str)) -> impl Iterator<Item = char> { | ||
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here | ||
LL | | ||
LL | v.0.chars().chain(v.1.chars()) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
| | ||
LL | fn chars0<'a>(v :(&'a str, &'a str)) -> impl Iterator<Item = char> + 'a { | ||
| ++++ ++ ++ ++++ | ||
|
||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds | ||
--> $DIR/issue-105227.rs:13:5 | ||
| | ||
LL | fn chars1(v0 : & str, v1 : &str) -> impl Iterator<Item = char> { | ||
| ----- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here | ||
LL | | ||
LL | v0.chars().chain(v1.chars()) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can introduce a named lifetime parameter `'a` | ||
| | ||
LL | fn chars1<'a>(v0 : &'a str, v1 : &'a str) -> impl Iterator<Item = char> + 'a { | ||
| ++++ ++ ++ ++++ | ||
|
||
error[E0700]: hidden type for `impl Iterator<Item = char>` captures lifetime that does not appear in bounds | ||
--> $DIR/issue-105227.rs:21:5 | ||
| | ||
LL | fn chars2<'b>(v0 : &str, v1 : &'_ str, v2 : &'b str) -> | ||
| ---- hidden type `std::iter::Chain<Chars<'_>, Chars<'_>>` captures the anonymous lifetime defined here | ||
... | ||
LL | (v0.chars().chain(v1.chars()), v2) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
help: to declare that `impl Iterator<Item = char>` captures `'_`, you can use the named lifetime parameter `'b` | ||
| | ||
LL ~ fn chars2<'b>(v0 : &'b str, v1 : &'b str, v2 : &'b str) -> | ||
LL | | ||
LL ~ (impl Iterator<Item = char> + 'b , &'b str) | ||
| | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0700`. |