forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
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 rust-lang#106747 - yanchen4791:issue-105507-fix, r=es…
…tebank Add 'static lifetime suggestion when GAT implied 'static requirement from HRTB Fix for issue rust-lang#105507 The problem: When generic associated types (GATs) are from higher-ranked trait bounds (HRTB), they are implied 'static requirement (see [Implied 'static requirement from higher-ranked trait bounds](https://blog.rust-lang.org/2022/10/28/gats-stabilization.html#implied-static-requirement-from-higher-ranked-trait-bounds) for more details). If the user did not explicitly specify the `'static` lifetime when using the GAT, the current error message will only point out the type `does not live long enough` where the type is used, but not where the GAT is specified and how to fix the problem. The solution: Add notes at the span where the problematic GATs are specified and suggestions of how to fix the problem by adding `'static` lifetime at the right spans.
- Loading branch information
Showing
7 changed files
with
258 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// run-rustfix | ||
// | ||
#![allow(warnings)] | ||
struct Wrapper<'a, T: ?Sized>(&'a T); | ||
|
||
trait Project { | ||
type Projected<'a> where Self: 'a; | ||
fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_>; | ||
} | ||
trait MyTrait {} | ||
trait ProjectedMyTrait {} | ||
|
||
impl<T> Project for Option<T> { | ||
type Projected<'a> = Option<Wrapper<'a, T>> where T: 'a; | ||
fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_> { | ||
this.0.as_ref().map(Wrapper) | ||
} | ||
} | ||
|
||
impl<T: MyTrait> MyTrait for Option<Wrapper<'_, T>> {} | ||
|
||
impl<T: ProjectedMyTrait> MyTrait for Wrapper<'_, T> {} | ||
|
||
impl<T> ProjectedMyTrait for T | ||
where | ||
T: Project, | ||
for<'a> T::Projected<'a>: MyTrait, | ||
//~^ NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
//~| NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
{} | ||
|
||
fn require_trait<T: MyTrait>(_: T) {} | ||
|
||
fn foo<T : MyTrait + 'static + 'static, U : MyTrait + 'static + 'static>(wrap: Wrapper<'_, Option<T>>, wrap1: Wrapper<'_, Option<U>>) { | ||
//~^ HELP consider restricting the type parameter to the `'static` lifetime | ||
//~| HELP consider restricting the type parameter to the `'static` lifetime | ||
require_trait(wrap); | ||
//~^ ERROR `T` does not live long enough | ||
require_trait(wrap1); | ||
//~^ ERROR `U` does not live long enough | ||
} | ||
|
||
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,43 @@ | ||
// run-rustfix | ||
// | ||
#![allow(warnings)] | ||
struct Wrapper<'a, T: ?Sized>(&'a T); | ||
|
||
trait Project { | ||
type Projected<'a> where Self: 'a; | ||
fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_>; | ||
} | ||
trait MyTrait {} | ||
trait ProjectedMyTrait {} | ||
|
||
impl<T> Project for Option<T> { | ||
type Projected<'a> = Option<Wrapper<'a, T>> where T: 'a; | ||
fn project(this: Wrapper<'_, Self>) -> Self::Projected<'_> { | ||
this.0.as_ref().map(Wrapper) | ||
} | ||
} | ||
|
||
impl<T: MyTrait> MyTrait for Option<Wrapper<'_, T>> {} | ||
|
||
impl<T: ProjectedMyTrait> MyTrait for Wrapper<'_, T> {} | ||
|
||
impl<T> ProjectedMyTrait for T | ||
where | ||
T: Project, | ||
for<'a> T::Projected<'a>: MyTrait, | ||
//~^ NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
//~| NOTE due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
{} | ||
|
||
fn require_trait<T: MyTrait>(_: T) {} | ||
|
||
fn foo<T : MyTrait, U : MyTrait>(wrap: Wrapper<'_, Option<T>>, wrap1: Wrapper<'_, Option<U>>) { | ||
//~^ HELP consider restricting the type parameter to the `'static` lifetime | ||
//~| HELP consider restricting the type parameter to the `'static` lifetime | ||
require_trait(wrap); | ||
//~^ ERROR `T` does not live long enough | ||
require_trait(wrap1); | ||
//~^ ERROR `U` does not live long enough | ||
} | ||
|
||
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,34 @@ | ||
error: `T` does not live long enough | ||
--> $DIR/issue-105507.rs:37:5 | ||
| | ||
LL | require_trait(wrap); | ||
| ^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
--> $DIR/issue-105507.rs:27:35 | ||
| | ||
LL | for<'a> T::Projected<'a>: MyTrait, | ||
| ^^^^^^^ | ||
help: consider restricting the type parameter to the `'static` lifetime | ||
| | ||
LL | fn foo<T : MyTrait + 'static, U : MyTrait + 'static>(wrap: Wrapper<'_, Option<T>>, wrap1: Wrapper<'_, Option<U>>) { | ||
| +++++++++ +++++++++ | ||
|
||
error: `U` does not live long enough | ||
--> $DIR/issue-105507.rs:39:5 | ||
| | ||
LL | require_trait(wrap1); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
note: due to current limitations in the borrow checker, this implies a `'static` lifetime | ||
--> $DIR/issue-105507.rs:27:35 | ||
| | ||
LL | for<'a> T::Projected<'a>: MyTrait, | ||
| ^^^^^^^ | ||
help: consider restricting the type parameter to the `'static` lifetime | ||
| | ||
LL | fn foo<T : MyTrait + 'static, U : MyTrait + 'static>(wrap: Wrapper<'_, Option<T>>, wrap1: Wrapper<'_, Option<U>>) { | ||
| +++++++++ +++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|