Skip to content

Commit 334ce3d

Browse files
committed
always show and explain sub region
1 parent ee7e3a8 commit 334ce3d

File tree

71 files changed

+458
-339
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+458
-339
lines changed

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

+23-37
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ use crate::traits::{
5959
};
6060

6161
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
62+
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, DiagnosticStyledString};
6263
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
63-
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
6464
use rustc_hir as hir;
6565
use rustc_hir::def::DefKind;
6666
use rustc_hir::def_id::{DefId, LocalDefId};
@@ -2351,40 +2351,29 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
23512351
},
23522352
};
23532353

2354-
let mut err = match sub.kind() {
2355-
ty::ReEarlyBound(_) | ty::ReFree(_) if sub.has_name() => struct_span_err!(
2356-
self.tcx.sess,
2357-
span,
2358-
E0309,
2359-
"{} may not live long enough",
2360-
labeled_user_string
2361-
),
2362-
ty::ReStatic => struct_span_err!(
2363-
self.tcx.sess,
2364-
span,
2365-
E0310,
2366-
"{} may not live long enough",
2367-
labeled_user_string
2368-
),
2369-
_ => {
2370-
let mut err = struct_span_err!(
2371-
self.tcx.sess,
2372-
span,
2373-
E0311,
2374-
"{} may not live long enough",
2375-
labeled_user_string
2376-
);
2377-
note_and_explain_region(
2378-
self.tcx,
2379-
&mut err,
2380-
&format!("{labeled_user_string} must be valid for "),
2381-
sub,
2382-
"...",
2383-
None,
2384-
);
2385-
err
2354+
let mut err = self.tcx.sess.struct_span_err_with_code(
2355+
span,
2356+
format!("{labeled_user_string} may not live long enough"),
2357+
match sub.kind() {
2358+
ty::ReEarlyBound(_) | ty::ReFree(_) if sub.has_name() => error_code!(E0309),
2359+
ty::ReStatic => error_code!(E0310),
2360+
_ => error_code!(E0311),
2361+
},
2362+
);
2363+
2364+
'_explain: {
2365+
let (description, span) = match sub.kind() {
2366+
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
2367+
msg_span_from_named_region(self.tcx, sub, Some(span))
2368+
}
2369+
_ => (format!("lifetime `{sub}`"), Some(span)),
2370+
};
2371+
let prefix = format!("{labeled_user_string} must be valid for ");
2372+
label_msg_span(&mut err, &prefix, description, span, "...");
2373+
if let Some(origin) = origin {
2374+
self.note_region_origin(&mut err, &origin);
23862375
}
2387-
};
2376+
}
23882377

23892378
'suggestion: {
23902379
let msg = "consider adding an explicit lifetime bound";
@@ -2460,9 +2449,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
24602449
);
24612450
}
24622451

2463-
if let Some(origin) = origin {
2464-
self.note_region_origin(&mut err, &origin);
2465-
}
24662452
err
24672453
}
24682454

tests/ui/associated-inherent-types/regionck-1.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/regionck-1.rs:9:30
33
|
44
LL | type NoTyOutliv<'a, T> = &'a T;
5-
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
5+
| -- ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
68
|
79
help: consider adding an explicit lifetime bound...
810
|

tests/ui/async-await/in-trait/async-generics-and-bounds.stderr

+6-22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ error[E0311]: the parameter type `U` may not live long enough
22
--> $DIR/async-generics-and-bounds.rs:12:28
33
|
44
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
5-
| ^^^^^^^
5+
| - ^^^^^^^ ...so that the reference type `&(T, U)` does not outlive the data it points at
6+
| |
7+
| the parameter type `U` must be valid for the anonymous lifetime as defined here...
68
|
7-
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8-
--> $DIR/async-generics-and-bounds.rs:12:18
9-
|
10-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
11-
| ^
12-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13-
--> $DIR/async-generics-and-bounds.rs:12:28
14-
|
15-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
16-
| ^^^^^^^
179
help: consider adding an explicit lifetime bound...
1810
|
1911
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, U: 'a;
@@ -23,18 +15,10 @@ error[E0311]: the parameter type `T` may not live long enough
2315
--> $DIR/async-generics-and-bounds.rs:12:28
2416
|
2517
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
26-
| ^^^^^^^
18+
| - ^^^^^^^ ...so that the reference type `&(T, U)` does not outlive the data it points at
19+
| |
20+
| the parameter type `T` must be valid for the anonymous lifetime as defined here...
2721
|
28-
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
29-
--> $DIR/async-generics-and-bounds.rs:12:18
30-
|
31-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
32-
| ^
33-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
34-
--> $DIR/async-generics-and-bounds.rs:12:28
35-
|
36-
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
37-
| ^^^^^^^
3822
help: consider adding an explicit lifetime bound...
3923
|
4024
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, T: 'a;

tests/ui/async-await/in-trait/async-generics.stderr

+6-22
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,10 @@ error[E0311]: the parameter type `U` may not live long enough
22
--> $DIR/async-generics.rs:9:28
33
|
44
LL | async fn foo(&self) -> &(T, U);
5-
| ^^^^^^^
5+
| - ^^^^^^^ ...so that the reference type `&(T, U)` does not outlive the data it points at
6+
| |
7+
| the parameter type `U` must be valid for the anonymous lifetime as defined here...
68
|
7-
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
8-
--> $DIR/async-generics.rs:9:18
9-
|
10-
LL | async fn foo(&self) -> &(T, U);
11-
| ^
12-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
13-
--> $DIR/async-generics.rs:9:28
14-
|
15-
LL | async fn foo(&self) -> &(T, U);
16-
| ^^^^^^^
179
help: consider adding an explicit lifetime bound...
1810
|
1911
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where U: 'a;
@@ -23,18 +15,10 @@ error[E0311]: the parameter type `T` may not live long enough
2315
--> $DIR/async-generics.rs:9:28
2416
|
2517
LL | async fn foo(&self) -> &(T, U);
26-
| ^^^^^^^
18+
| - ^^^^^^^ ...so that the reference type `&(T, U)` does not outlive the data it points at
19+
| |
20+
| the parameter type `T` must be valid for the anonymous lifetime as defined here...
2721
|
28-
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
29-
--> $DIR/async-generics.rs:9:18
30-
|
31-
LL | async fn foo(&self) -> &(T, U);
32-
| ^
33-
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
34-
--> $DIR/async-generics.rs:9:28
35-
|
36-
LL | async fn foo(&self) -> &(T, U);
37-
| ^^^^^^^
3822
help: consider adding an explicit lifetime bound...
3923
|
4024
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: 'a;

tests/ui/builtin-superkinds/builtin-superkinds-self-type.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/builtin-superkinds-self-type.rs:10:16
33
|
44
LL | impl <T: Sync> Foo for T { }
5-
| ^^^ ...so that the type `T` will meet its required lifetime bounds...
5+
| ^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds...
69
|
710
note: ...that is required by this bound
811
--> $DIR/builtin-superkinds-self-type.rs:6:24

tests/ui/coercion/issue-53475.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/issue-53475.rs:10:1
33
|
44
LL | impl<T> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
710
help: consider adding an explicit lifetime bound...
811
|

tests/ui/consts/issue-102117.stderr

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/issue-102117.rs:19:26
33
|
44
LL | type_id: TypeId::of::<T>(),
5-
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
710
help: consider adding an explicit lifetime bound...
811
|
@@ -13,7 +16,10 @@ error[E0310]: the parameter type `T` may not live long enough
1316
--> $DIR/issue-102117.rs:19:26
1417
|
1518
LL | type_id: TypeId::of::<T>(),
16-
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
19+
| ^^^^^^^^^^^^^^^^^
20+
| |
21+
| the parameter type `T` must be valid for the static lifetime...
22+
| ...so that the type `T` will meet its required lifetime bounds
1723
|
1824
help: consider adding an explicit lifetime bound...
1925
|

tests/ui/error-codes/E0311.stderr

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,11 @@
11
error[E0311]: the parameter type `T` may not live long enough
22
--> $DIR/E0311.rs:6:5
33
|
4-
LL | with_restriction::<T>(x)
5-
| ^^^^^^^^^^^^^^^^^^^^^
6-
|
7-
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
8-
--> $DIR/E0311.rs:5:25
9-
|
104
LL | fn no_restriction<T>(x: &()) -> &() {
11-
| ^^^
12-
note: ...so that the type `T` will meet its required lifetime bounds
13-
--> $DIR/E0311.rs:6:5
14-
|
5+
| --- the parameter type `T` must be valid for the anonymous lifetime defined here...
156
LL | with_restriction::<T>(x)
16-
| ^^^^^^^^^^^^^^^^^^^^^
7+
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
8+
|
179
help: consider adding an explicit lifetime bound...
1810
|
1911
LL | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {

tests/ui/fn/implied-bounds-unnorm-associated-type-5.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:6:13
33
|
44
LL | impl<'a, T> Trait<'a> for T {
5-
| ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
5+
| -- ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
68
|
79
note: ...that is required by this bound
810
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:1:18

tests/ui/generic-associated-types/issue-84931.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/issue-84931.rs:14:21
33
|
44
LL | type Item<'a> = &'a mut T;
5-
| ^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at
5+
| -- ^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at
6+
| |
7+
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
68
|
79
help: consider adding an explicit lifetime bound...
810
|

tests/ui/impl-trait/must_outlive_least_region_or_bound.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ error[E0310]: the parameter type `T` may not live long enough
117117
--> $DIR/must_outlive_least_region_or_bound.rs:43:5
118118
|
119119
LL | x
120-
| ^ ...so that the type `T` will meet its required lifetime bounds
120+
| ^
121+
| |
122+
| the parameter type `T` must be valid for the static lifetime...
123+
| ...so that the type `T` will meet its required lifetime bounds
121124
|
122125
help: consider adding an explicit lifetime bound...
123126
|

tests/ui/impl-trait/type_parameters_captured.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/type_parameters_captured.rs:8:5
33
|
44
LL | x
5-
| ^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
710
help: consider adding an explicit lifetime bound...
811
|

tests/ui/impl-trait/unactionable_diagnostic.stderr

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
error[E0309]: the parameter type `T` may not live long enough
22
--> $DIR/unactionable_diagnostic.rs:21:5
33
|
4+
LL | pub fn bar<'t, T>(
5+
| -- the parameter type `T` must be valid for the lifetime `'t` as defined here...
6+
...
47
LL | foo(post, x)
58
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
69
|

tests/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr

+19-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:10
33
|
44
LL | foo: &'static T
5-
| ^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
5+
| ^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the reference type `&'static T` does not outlive the data it points at
69
|
710
help: consider adding an explicit lifetime bound...
811
|
@@ -13,7 +16,9 @@ error[E0309]: the parameter type `K` may not live long enough
1316
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
1417
|
1518
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
16-
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
19+
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
20+
| |
21+
| the parameter type `K` must be valid for the lifetime `'a` as defined here...
1722
|
1823
help: consider adding an explicit lifetime bound...
1924
|
@@ -24,7 +29,9 @@ error[E0309]: the parameter type `M` may not live long enough
2429
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
2530
|
2631
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
27-
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
32+
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
33+
| |
34+
| the parameter type `M` must be valid for the lifetime `'a` as defined here...
2835
|
2936
help: consider adding an explicit lifetime bound...
3037
|
@@ -35,7 +42,9 @@ error[E0309]: the parameter type `K` may not live long enough
3542
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
3643
|
3744
LL | fn foo<'a, L: X<&'a Nested<K>>>();
38-
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
45+
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
46+
| |
47+
| the parameter type `K` must be valid for the lifetime `'a` as defined here...
3948
|
4049
help: consider adding an explicit lifetime bound...
4150
|
@@ -46,7 +55,9 @@ error[E0309]: the parameter type `Self` may not live long enough
4655
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
4756
|
4857
LL | fn bar<'a, L: X<&'a Nested<Self>>>();
49-
| ^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
58+
| -- ^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
59+
| |
60+
| the parameter type `Self` must be valid for the lifetime `'a` as defined here...
5061
|
5162
help: consider adding an explicit lifetime bound...
5263
|
@@ -57,7 +68,9 @@ error[E0309]: the parameter type `L` may not live long enough
5768
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
5869
|
5970
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
60-
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
71+
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
72+
| |
73+
| the parameter type `L` must be valid for the lifetime `'a` as defined here...
6174
|
6275
help: consider adding an explicit lifetime bound...
6376
|

tests/ui/lifetimes/lifetime-errors/issue_74400.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
22
--> $DIR/issue_74400.rs:12:5
33
|
44
LL | f(data, identity)
5-
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
5+
| ^^^^^^^^^^^^^^^^^
6+
| |
7+
| the parameter type `T` must be valid for the static lifetime...
8+
| ...so that the type `T` will meet its required lifetime bounds
69
|
710
help: consider adding an explicit lifetime bound...
811
|

0 commit comments

Comments
 (0)