Skip to content

Commit 59e285f

Browse files
committed
Report diagnostics at the actually actionable site
1 parent e237aae commit 59e285f

File tree

5 files changed

+43
-21
lines changed

5 files changed

+43
-21
lines changed

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

+1-9
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-
mut origin: Option<SubregionOrigin<'tcx>>,
2317+
origin: Option<SubregionOrigin<'tcx>>,
23182318
bound_kind: GenericKind<'tcx>,
23192319
sub: Region<'tcx>,
23202320
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
@@ -2349,14 +2349,6 @@ 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-
}
23602352
_ => None,
23612353
};
23622354

compiler/rustc_infer/src/infer/outlives/obligations.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,7 @@ where
336336
GenericKind::Opaque(def_id, substs),
337337
def_id,
338338
substs,
339+
true,
339340
|ty| match *ty.kind() {
340341
ty::Opaque(def_id, substs) => (def_id, substs),
341342
_ => bug!("expected only projection types from env, not {:?}", ty),
@@ -356,6 +357,7 @@ where
356357
GenericKind::Projection(projection_ty),
357358
projection_ty.item_def_id,
358359
projection_ty.substs,
360+
false,
359361
|ty| match ty.kind() {
360362
ty::Projection(projection_ty) => (projection_ty.item_def_id, projection_ty.substs),
361363
_ => bug!("expected only projection types from env, not {:?}", ty),
@@ -371,6 +373,7 @@ where
371373
generic: GenericKind<'tcx>,
372374
def_id: DefId,
373375
substs: SubstsRef<'tcx>,
376+
is_opaque: bool,
374377
filter: impl Fn(Ty<'tcx>) -> (DefId, SubstsRef<'tcx>),
375378
) {
376379
// An optimization for a common case with opaque types.
@@ -437,7 +440,7 @@ where
437440
// inference variables, we use a verify constraint instead of adding
438441
// edges, which winds up enforcing the same condition.
439442
let needs_infer = substs.needs_infer();
440-
if approx_env_bounds.is_empty() && trait_bounds.is_empty() && needs_infer {
443+
if approx_env_bounds.is_empty() && trait_bounds.is_empty() && (needs_infer || is_opaque) {
441444
debug!("no declared bounds");
442445

443446
self.substs_must_outlive(substs, origin, region);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// run-rustfix
2+
3+
pub trait Trait {}
4+
5+
pub struct Foo;
6+
7+
impl Trait for Foo {}
8+
9+
fn foo<'x, P>(
10+
_post: P,
11+
x: &'x Foo,
12+
) -> &'x impl Trait {
13+
x
14+
}
15+
16+
pub fn bar<'t, T: 't>(
17+
//~^ HELP: consider adding an explicit lifetime bound...
18+
post: T,
19+
x: &'t Foo,
20+
) -> &'t impl Trait {
21+
foo(post, x)
22+
//~^ ERROR: the parameter type `T` may not live long enough
23+
}
24+
25+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
1-
trait Trait {}
1+
// run-rustfix
22

3-
struct Foo;
3+
pub trait Trait {}
4+
5+
pub struct Foo;
46

57
impl Trait for Foo {}
68

79
fn foo<'x, P>(
8-
post: P,
10+
_post: P,
911
x: &'x Foo,
1012
) -> &'x impl Trait {
11-
//~^ HELP: consider adding an explicit lifetime bound...
1213
x
1314
}
1415

15-
fn bar<'t, T>(
16+
pub fn bar<'t, T>(
17+
//~^ HELP: consider adding an explicit lifetime bound...
1618
post: T,
1719
x: &'t Foo,
1820
) -> &'t impl Trait {
1921
foo(post, x)
20-
//~^ ERROR: the opaque type `foo<T>::{opaque#0}` may not live long enough
22+
//~^ ERROR: the parameter type `T` may not live long enough
2123
}
2224

2325
fn main() {}

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

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

1212
error: aborting due to previous error
1313

0 commit comments

Comments
 (0)