Skip to content

Commit b10ef3c

Browse files
authored
Rollup merge of #121435 - estebank:rpitit-static-119773, r=compiler-errors
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 do not emit an invalid suggestion 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 ``` Partially address #119773. Ideally we'd suggest modifying `Erased::f` instead. r? `@compiler-errors`
2 parents 8f359be + 5e6da72 commit b10ef3c

File tree

4 files changed

+36
-2
lines changed

4 files changed

+36
-2
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+8
Original file line numberDiff line numberDiff line change
@@ -2437,6 +2437,14 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
24372437
let suggestion =
24382438
if has_lifetimes { format!(" + {lt_name}") } else { format!(": {lt_name}") };
24392439
suggs.push((sp, suggestion))
2440+
} else if let GenericKind::Alias(ref p) = bound_kind
2441+
&& let ty::Projection = p.kind(self.tcx)
2442+
&& let DefKind::AssocTy = self.tcx.def_kind(p.def_id)
2443+
&& let Some(ty::ImplTraitInTraitData::Trait { .. }) =
2444+
self.tcx.opt_rpitit_info(p.def_id)
2445+
{
2446+
// The lifetime found in the `impl` is longer than the one on the RPITIT.
2447+
// Do not suggest `<Type as Trait>::{opaque}: 'static`.
24402448
} else if let Some(generics) = self.tcx.hir().get_generics(suggestion_scope) {
24412449
let pred = format!("{bound_kind}: {lt_name}");
24422450
let suggestion = format!("{} {}", generics.add_where_or_trailing_comma(), pred);

tests/ui/impl-trait/in-trait/async-and-ret-ref.stderr

-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ LL | async fn foo() -> &'static impl T;
66
| |
77
| the associated type `<Self as MyTrait>::{opaque#0}` must be valid for the static lifetime...
88
| ...so that the reference type `&'static impl T` does not outlive the data it points at
9-
|
10-
= help: consider adding an explicit lifetime bound `<Self as MyTrait>::{opaque#0}: 'static`...
119

1210
error: aborting due to 1 previous error
1311

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait Original {
2+
fn f() -> impl Fn();
3+
}
4+
5+
trait Erased {
6+
fn f(&self) -> Box<dyn Fn()>;
7+
}
8+
9+
impl<T: Original> Erased for T {
10+
fn f(&self) -> Box<dyn Fn()> {
11+
Box::new(<T as Original>::f())
12+
//~^ ERROR the associated type `<T as Original>::{opaque#0}` may not live long enough
13+
}
14+
}
15+
16+
fn main () {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0310]: the associated type `<T as Original>::{opaque#0}` may not live long enough
2+
--> $DIR/missing-static-bound-from-impl.rs:11:9
3+
|
4+
LL | Box::new(<T as Original>::f())
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the associated type `<T as Original>::{opaque#0}` must be valid for the static lifetime...
8+
| ...so that the type `impl Fn()` will meet its required lifetime bounds
9+
10+
error: aborting due to 1 previous error
11+
12+
For more information about this error, try `rustc --explain E0310`.

0 commit comments

Comments
 (0)