Skip to content

Commit 2af7bc0

Browse files
Rollup merge of #152226 - fmease:modernize-indeterminate-object-lifetime-diag, r=chenyukang
Modernize diagnostic for indeterminate trait object lifetime bounds * remove suggestion from the diagnostic message (bad style, too long) and turn it into a structured suggestion * replace *object type* with *trait object type* since the former is an outdated term
2 parents 2aaf3a1 + dc24aae commit 2af7bc0

11 files changed

+63
-27
lines changed

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ use rustc_abi::{ExternAbi, Size};
2222
use rustc_ast::Recovered;
2323
use rustc_data_structures::assert_matches;
2424
use rustc_data_structures::fx::{FxHashSet, FxIndexMap};
25-
use rustc_errors::{
26-
Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey, struct_span_code_err,
27-
};
25+
use rustc_errors::{Applicability, Diag, DiagCtxtHandle, E0228, ErrorGuaranteed, StashKey};
2826
use rustc_hir::attrs::AttributeKind;
2927
use rustc_hir::def::{DefKind, Res};
3028
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -318,16 +316,24 @@ impl<'tcx> HirTyLowerer<'tcx> for ItemCtxt<'tcx> {
318316
}
319317

320318
fn re_infer(&self, span: Span, reason: RegionInferReason<'_>) -> ty::Region<'tcx> {
321-
if let RegionInferReason::ObjectLifetimeDefault = reason {
322-
let e = struct_span_code_err!(
323-
self.dcx(),
324-
span,
325-
E0228,
326-
"the lifetime bound for this object type cannot be deduced \
327-
from context; please supply an explicit bound"
328-
)
329-
.emit();
330-
ty::Region::new_error(self.tcx(), e)
319+
if let RegionInferReason::ObjectLifetimeDefault(sugg_sp) = reason {
320+
// FIXME: Account for trailing plus `dyn Trait+`, the need of parens in
321+
// `*const dyn Trait` and `Fn() -> *const dyn Trait`.
322+
let guar = self
323+
.dcx()
324+
.struct_span_err(
325+
span,
326+
"cannot deduce the lifetime bound for this trait object type from context",
327+
)
328+
.with_code(E0228)
329+
.with_span_suggestion_verbose(
330+
sugg_sp,
331+
"please supply an explicit bound",
332+
" + /* 'a */",
333+
Applicability::HasPlaceholders,
334+
)
335+
.emit();
336+
ty::Region::new_error(self.tcx(), guar)
331337
} else {
332338
// This indicates an illegal lifetime in a non-assoc-trait position
333339
ty::Region::new_error_with_message(self.tcx(), span, "unelided lifetime in signature")

compiler/rustc_hir_analysis/src/hir_ty_lowering/dyn_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
460460
// Parent lifetime must have failed to resolve. Don't emit a redundant error.
461461
RegionInferReason::ExplicitObjectLifetime
462462
} else {
463-
RegionInferReason::ObjectLifetimeDefault
463+
RegionInferReason::ObjectLifetimeDefault(span.shrink_to_hi())
464464
}
465465
} else {
466466
RegionInferReason::ExplicitObjectLifetime

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub enum RegionInferReason<'a> {
104104
/// Lifetime on a trait object that is spelled explicitly, e.g. `+ 'a` or `+ '_`.
105105
ExplicitObjectLifetime,
106106
/// A trait object's lifetime when it is elided, e.g. `dyn Any`.
107-
ObjectLifetimeDefault,
107+
ObjectLifetimeDefault(Span),
108108
/// Generic lifetime parameter
109109
Param(&'a ty::GenericParamDef),
110110
RegionPredicate,

tests/ui/object-lifetime/object-lifetime-default-ambiguous.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ struct Ref2<'a,'b:'a,T:'a+'b+?Sized> {
2121
}
2222

2323
fn a<'a,'b>(t: Ref2<'a,'b, dyn Test>) {
24-
//~^ ERROR lifetime bound for this object type cannot be deduced from context
24+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
2525
}
2626

2727
fn b(t: Ref2<dyn Test>) {
28-
//~^ ERROR lifetime bound for this object type cannot be deduced from context
28+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
2929
}
3030

3131
fn c(t: Ref2<&dyn Test>) {
@@ -41,7 +41,7 @@ fn e(t: Ref2<Ref0<dyn Test>>) {
4141
}
4242

4343
fn f(t: &Ref2<dyn Test>) {
44-
//~^ ERROR lifetime bound for this object type cannot be deduced from context
44+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
4545
}
4646

4747
fn main() {

tests/ui/object-lifetime/object-lifetime-default-ambiguous.stderr

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,35 @@
1-
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
1+
error[E0228]: cannot deduce the lifetime bound for this trait object type from context
22
--> $DIR/object-lifetime-default-ambiguous.rs:23:28
33
|
44
LL | fn a<'a,'b>(t: Ref2<'a,'b, dyn Test>) {
55
| ^^^^^^^^
6+
|
7+
help: please supply an explicit bound
8+
|
9+
LL | fn a<'a,'b>(t: Ref2<'a,'b, dyn Test + /* 'a */>) {
10+
| ++++++++++
611

7-
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
12+
error[E0228]: cannot deduce the lifetime bound for this trait object type from context
813
--> $DIR/object-lifetime-default-ambiguous.rs:27:14
914
|
1015
LL | fn b(t: Ref2<dyn Test>) {
1116
| ^^^^^^^^
17+
|
18+
help: please supply an explicit bound
19+
|
20+
LL | fn b(t: Ref2<dyn Test + /* 'a */>) {
21+
| ++++++++++
1222

13-
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
23+
error[E0228]: cannot deduce the lifetime bound for this trait object type from context
1424
--> $DIR/object-lifetime-default-ambiguous.rs:43:15
1525
|
1626
LL | fn f(t: &Ref2<dyn Test>) {
1727
| ^^^^^^^^
28+
|
29+
help: please supply an explicit bound
30+
|
31+
LL | fn f(t: &Ref2<dyn Test + /* 'a */>) {
32+
| ++++++++++
1833

1934
error: aborting due to 3 previous errors
2035

tests/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn is_static<T>(_: T) where T: 'static { }
1818
// Here, we should default to `dyn Bar + 'static`, but the current
1919
// code forces us into a conservative, hacky path.
2020
fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
21-
//~^ ERROR please supply an explicit bound
21+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
2222

2323
fn main() {
2424
let s = format!("foo");

tests/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic1.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
1+
error[E0228]: cannot deduce the lifetime bound for this trait object type from context
22
--> $DIR/object-lifetime-default-dyn-binding-nonstatic1.rs:20:50
33
|
44
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
55
| ^^^^^^^
6+
|
7+
help: please supply an explicit bound
8+
|
9+
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar + /* 'a */> { &() }
10+
| ++++++++++
611

712
error: aborting due to 1 previous error
813

tests/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ fn is_static<T>(_: T) where T: 'static { }
1818
// Here, we default to `dyn Bar + 'a`. Or, we *should*, but the
1919
// current code forces us into a conservative, hacky path.
2020
fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
21-
//~^ ERROR please supply an explicit bound
21+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
2222

2323
fn main() {
2424
let s = format!("foo");

tests/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic2.stderr

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound
1+
error[E0228]: cannot deduce the lifetime bound for this trait object type from context
22
--> $DIR/object-lifetime-default-dyn-binding-nonstatic2.rs:20:50
33
|
44
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
55
| ^^^^^^^
6+
|
7+
help: please supply an explicit bound
8+
|
9+
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar + /* 'a */> { &() }
10+
| ++++++++++
611

712
error: aborting due to 1 previous error
813

tests/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic3.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn is_static<T>(_: T) where T: 'static { }
1414
// Here, we should default to `dyn Bar + 'static`, but the current
1515
// code forces us into a conservative, hacky path.
1616
fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() }
17-
//~^ ERROR please supply an explicit bound
17+
//~^ ERROR cannot deduce the lifetime bound for this trait object type
1818

1919
fn main() {
2020
let s = format!("foo");

0 commit comments

Comments
 (0)