-
Notifications
You must be signed in to change notification settings - Fork 13k
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 #105345 - yanchen4791:issue-103582-fix, r=jackh726
Add hint for missing lifetime bound on trait object when type alias is used Fix issue #103582. The problem: When a type alias is used to specify the return type of the method in a trait impl, the suggestion for fixing the problem of "missing lifetime bound on trait object" of the trait impl will not be created. The issue caused by the code which searches for the return trait objects when constructing the hint suggestion is not able to find the trait objects since they are specified in the type alias path instead of the return path of the trait impl. The solution: Trace the trait objects in the type alias path and provide them along with the alias span to generate the suggestion in case the type alias is used in return type of the method in the trait impl.
- Loading branch information
Showing
6 changed files
with
176 additions
and
19 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
45 changes: 45 additions & 0 deletions
45
...times/issue-103582-hint-for-missing-lifetime-bound-on-trait-object-using-type-alias.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,45 @@ | ||
// run-rustfix | ||
|
||
trait Greeter0 { | ||
fn greet(&self); | ||
} | ||
|
||
trait Greeter1 { | ||
fn greet(&self); | ||
} | ||
|
||
type BoxedGreeter<'a> = (Box<dyn Greeter0 + 'a>, Box<dyn Greeter1 + 'a>); | ||
//~^ HELP to declare that the trait object captures data from argument `self`, you can add a lifetime parameter `'a` in the type alias | ||
|
||
struct FixedGreeter<'a>(pub &'a str); | ||
|
||
impl Greeter0 for FixedGreeter<'_> { | ||
fn greet(&self) { | ||
println!("0 {}", self.0) | ||
} | ||
} | ||
|
||
impl Greeter1 for FixedGreeter<'_> { | ||
fn greet(&self) { | ||
println!("1 {}", self.0) | ||
} | ||
} | ||
|
||
struct Greetings(pub Vec<String>); | ||
|
||
impl Greetings { | ||
pub fn get(&self, i: usize) -> BoxedGreeter { | ||
(Box::new(FixedGreeter(&self.0[i])), Box::new(FixedGreeter(&self.0[i]))) | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut g = Greetings {0 : vec!()}; | ||
g.0.push("a".to_string()); | ||
g.0.push("b".to_string()); | ||
g.get(0).0.greet(); | ||
g.get(0).1.greet(); | ||
g.get(1).0.greet(); | ||
g.get(1).1.greet(); | ||
} |
45 changes: 45 additions & 0 deletions
45
...ifetimes/issue-103582-hint-for-missing-lifetime-bound-on-trait-object-using-type-alias.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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// run-rustfix | ||
|
||
trait Greeter0 { | ||
fn greet(&self); | ||
} | ||
|
||
trait Greeter1 { | ||
fn greet(&self); | ||
} | ||
|
||
type BoxedGreeter = (Box<dyn Greeter0>, Box<dyn Greeter1>); | ||
//~^ HELP to declare that the trait object captures data from argument `self`, you can add a lifetime parameter `'a` in the type alias | ||
|
||
struct FixedGreeter<'a>(pub &'a str); | ||
|
||
impl Greeter0 for FixedGreeter<'_> { | ||
fn greet(&self) { | ||
println!("0 {}", self.0) | ||
} | ||
} | ||
|
||
impl Greeter1 for FixedGreeter<'_> { | ||
fn greet(&self) { | ||
println!("1 {}", self.0) | ||
} | ||
} | ||
|
||
struct Greetings(pub Vec<String>); | ||
|
||
impl Greetings { | ||
pub fn get(&self, i: usize) -> BoxedGreeter { | ||
(Box::new(FixedGreeter(&self.0[i])), Box::new(FixedGreeter(&self.0[i]))) | ||
//~^ ERROR lifetime may not live long enough | ||
} | ||
} | ||
|
||
fn main() { | ||
let mut g = Greetings {0 : vec!()}; | ||
g.0.push("a".to_string()); | ||
g.0.push("b".to_string()); | ||
g.get(0).0.greet(); | ||
g.get(0).1.greet(); | ||
g.get(1).0.greet(); | ||
g.get(1).1.greet(); | ||
} |
15 changes: 15 additions & 0 deletions
15
...imes/issue-103582-hint-for-missing-lifetime-bound-on-trait-object-using-type-alias.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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/issue-103582-hint-for-missing-lifetime-bound-on-trait-object-using-type-alias.rs:32:9 | ||
| | ||
LL | pub fn get(&self, i: usize) -> BoxedGreeter { | ||
| - let's call the lifetime of this reference `'1` | ||
LL | (Box::new(FixedGreeter(&self.0[i])), Box::new(FixedGreeter(&self.0[i]))) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'1` must outlive `'static` | ||
| | ||
help: to declare that the trait object captures data from argument `self`, you can add a lifetime parameter `'a` in the type alias | ||
| | ||
LL | type BoxedGreeter<'a> = (Box<dyn Greeter0 + 'a>, Box<dyn Greeter1 + 'a>); | ||
| ++++ ++++ ++++ | ||
|
||
error: aborting due to previous error | ||
|