]> git.lizzy.rs Git - rust.git/commitdiff
Improve diagnostic for adding more bounds to opaque types
authorOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Tue, 6 Sep 2022 10:16:26 +0000 (10:16 +0000)
committerOli Scherer <git-spam-no-reply9815368754983@oli-obk.de>
Wed, 21 Sep 2022 13:15:24 +0000 (13:15 +0000)
compiler/rustc_infer/src/infer/error_reporting/mod.rs
src/test/ui/impl-trait/unactionable_diagnostic.rs
src/test/ui/impl-trait/unactionable_diagnostic.stderr

index e33035381e0a989817bfabd044d32c53370df63a..8b8cff0dbbe64cb761431b70c1886168630422bf 100644 (file)
@@ -2314,7 +2314,7 @@ pub fn construct_generic_bound_failure(
         &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 @@ 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,
         };
 
index de482e0370ca6920938644815ecc6032d96cc23c..9b82332f9b067610dba54a413058bc10f2581bf0 100644 (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() {}
index fe6fef5815d54b4bdf3151274be0dcaefa1607a9..a36e89f58b98e4e3b4c11097fce437f757d3674f 100644 (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