From 86f1ca812b8704beb20459bd5723d0cc3a0ace62 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Tue, 6 Sep 2022 10:16:26 +0000 Subject: [PATCH] Improve diagnostic for adding more bounds to opaque types --- compiler/rustc_infer/src/infer/error_reporting/mod.rs | 10 +++++++++- src/test/ui/impl-trait/unactionable_diagnostic.rs | 2 +- src/test/ui/impl-trait/unactionable_diagnostic.stderr | 8 +++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_infer/src/infer/error_reporting/mod.rs b/compiler/rustc_infer/src/infer/error_reporting/mod.rs index e33035381e0..8b8cff0dbbe 100644 --- a/compiler/rustc_infer/src/infer/error_reporting/mod.rs +++ b/compiler/rustc_infer/src/infer/error_reporting/mod.rs @@ -2314,7 +2314,7 @@ pub fn construct_generic_bound_failure( &self, generic_param_scope: LocalDefId, span: Span, - origin: Option>, + mut origin: Option>, bound_kind: GenericKind<'tcx>, sub: Region<'tcx>, ) -> DiagnosticBuilder<'a, ErrorGuaranteed> { @@ -2349,6 +2349,14 @@ pub fn construct_generic_bound_failure( 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, }; diff --git a/src/test/ui/impl-trait/unactionable_diagnostic.rs b/src/test/ui/impl-trait/unactionable_diagnostic.rs index de482e0370c..9b82332f9b0 100644 --- a/src/test/ui/impl-trait/unactionable_diagnostic.rs +++ b/src/test/ui/impl-trait/unactionable_diagnostic.rs @@ -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::{opaque#0}` may not live long enough - //~| HELP: consider adding an explicit lifetime bound `foo::{opaque#0}: 't` } fn main() {} diff --git a/src/test/ui/impl-trait/unactionable_diagnostic.stderr b/src/test/ui/impl-trait/unactionable_diagnostic.stderr index fe6fef5815d..a36e89f58b9 100644 --- a/src/test/ui/impl-trait/unactionable_diagnostic.stderr +++ b/src/test/ui/impl-trait/unactionable_diagnostic.stderr @@ -1,11 +1,13 @@ error[E0309]: the opaque type `foo::{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::{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 -- 2.44.0