Improve diagnostic for adding more bounds to opaque types

This commit is contained in:
Oli Scherer 2022-09-06 10:16:26 +00:00
parent 4b323e62ba
commit 86f1ca812b
3 changed files with 15 additions and 5 deletions

View File

@ -2314,7 +2314,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
&self,
generic_param_scope: LocalDefId,
span: Span,
origin: Option<SubregionOrigin<'tcx>>,
mut origin: Option<SubregionOrigin<'tcx>>,
bound_kind: GenericKind<'tcx>,
sub: Region<'tcx>,
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
@ -2349,6 +2349,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
None
}
}
GenericKind::Opaque(def_id, _substs) => {
// Avoid emitting a `... so that the type` message at the error site.
// It would be out of order for return position impl trait
origin = None;
// Make sure the lifetime suggestion is on the RPIT instead of proposing
// to add a bound for opaque types (which isn't possible)
Some((self.tcx.def_span(def_id).shrink_to_hi(), true))
}
_ => None,
};

View File

@ -8,6 +8,7 @@ fn foo<'t, P>(
post: P,
x: &'t Foo,
) -> &'t impl Trait {
//~^ HELP: consider adding an explicit lifetime bound...
x
}
@ -17,7 +18,6 @@ fn bar<'t, T>(
) -> &'t impl Trait {
foo(post, x)
//~^ ERROR: the opaque type `foo<T>::{opaque#0}` may not live long enough
//~| HELP: consider adding an explicit lifetime bound `foo<T>::{opaque#0}: 't`
}
fn main() {}

View File

@ -1,11 +1,13 @@
error[E0309]: the opaque type `foo<T>::{opaque#0}` may not live long enough
--> $DIR/unactionable_diagnostic.rs:18:5
--> $DIR/unactionable_diagnostic.rs:19:5
|
LL | foo(post, x)
| ^^^^^^^^^^^^
|
= help: consider adding an explicit lifetime bound `foo<T>::{opaque#0}: 't`...
= note: ...so that the type `impl Trait` will meet its required lifetime bounds
help: consider adding an explicit lifetime bound...
|
LL | ) -> &'t impl Trait + 't {
| ++++
error: aborting due to previous error