-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for RPITIT in E0310 explicit lifetime constraint suggestion
When given ```rust trait Original { fn f() -> impl Fn(); } trait Erased { fn f(&self) -> Box<dyn Fn()>; } impl<T: Original> Erased for T { fn f(&self) -> Box<dyn Fn()> { Box::new(<T as Original>::f()) } } ``` emit a suggestion to further constrain the RPITIT, instead of what we did previously, suggest restricting the `Trait::{opaque}` type in a `where` clause: ``` error[E0310]: the associated type `<T as Original>::{opaque#0}` may not live long enough --> $DIR/missing-static-bound-from-impl.rs:11:9 | LL | Box::new(<T as Original>::f()) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | | | the associated type `<T as Original>::{opaque#0}` must be valid for the static lifetime... | ...so that the type `impl Fn()` will meet its required lifetime bounds | help: consider adding an explicit lifetime bound | LL | fn f() -> impl Fn() + 'static; | +++++++++ ``` Fix #119773.
- Loading branch information
Showing
4 changed files
with
48 additions
and
1 deletion.
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
16 changes: 16 additions & 0 deletions
16
tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.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,16 @@ | ||
trait Original { | ||
fn f() -> impl Fn(); | ||
} | ||
|
||
trait Erased { | ||
fn f(&self) -> Box<dyn Fn()>; | ||
} | ||
|
||
impl<T: Original> Erased for T { | ||
fn f(&self) -> Box<dyn Fn()> { | ||
Box::new(<T as Original>::f()) | ||
//~^ ERROR the associated type `<T as Original>::{opaque#0}` may not live long enough | ||
} | ||
} | ||
|
||
fn main () {} |
17 changes: 17 additions & 0 deletions
17
tests/ui/impl-trait/in-trait/missing-static-bound-from-impl.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,17 @@ | ||
error[E0310]: the associated type `<T as Original>::{opaque#0}` may not live long enough | ||
--> $DIR/missing-static-bound-from-impl.rs:11:9 | ||
| | ||
LL | Box::new(<T as Original>::f()) | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| the associated type `<T as Original>::{opaque#0}` must be valid for the static lifetime... | ||
| ...so that the type `impl Fn()` will meet its required lifetime bounds | ||
| | ||
help: consider adding an explicit lifetime bound | ||
| | ||
LL | fn f() -> impl Fn() + 'static; | ||
| +++++++++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0310`. |