Skip to content

Commit 86f1ca8

Browse files
committed
Improve diagnostic for adding more bounds to opaque types
1 parent 4b323e6 commit 86f1ca8

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -2314,7 +2314,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
23142314
&self,
23152315
generic_param_scope: LocalDefId,
23162316
span: Span,
2317-
origin: Option<SubregionOrigin<'tcx>>,
2317+
mut origin: Option<SubregionOrigin<'tcx>>,
23182318
bound_kind: GenericKind<'tcx>,
23192319
sub: Region<'tcx>,
23202320
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
@@ -2349,6 +2349,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
23492349
None
23502350
}
23512351
}
2352+
GenericKind::Opaque(def_id, _substs) => {
2353+
// Avoid emitting a `... so that the type` message at the error site.
2354+
// It would be out of order for return position impl trait
2355+
origin = None;
2356+
// Make sure the lifetime suggestion is on the RPIT instead of proposing
2357+
// to add a bound for opaque types (which isn't possible)
2358+
Some((self.tcx.def_span(def_id).shrink_to_hi(), true))
2359+
}
23522360
_ => None,
23532361
};
23542362

src/test/ui/impl-trait/unactionable_diagnostic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ fn foo<'t, P>(
88
post: P,
99
x: &'t Foo,
1010
) -> &'t impl Trait {
11+
//~^ HELP: consider adding an explicit lifetime bound...
1112
x
1213
}
1314

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

2323
fn main() {}

src/test/ui/impl-trait/unactionable_diagnostic.stderr

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error[E0309]: the opaque type `foo<T>::{opaque#0}` may not live long enough
2-
--> $DIR/unactionable_diagnostic.rs:18:5
2+
--> $DIR/unactionable_diagnostic.rs:19:5
33
|
44
LL | foo(post, x)
55
| ^^^^^^^^^^^^
66
|
7-
= help: consider adding an explicit lifetime bound `foo<T>::{opaque#0}: 't`...
8-
= note: ...so that the type `impl Trait` will meet its required lifetime bounds
7+
help: consider adding an explicit lifetime bound...
8+
|
9+
LL | ) -> &'t impl Trait + 't {
10+
| ++++
911

1012
error: aborting due to previous error
1113

0 commit comments

Comments
 (0)