Skip to content

Commit

Permalink
always show and explain sub region
Browse files Browse the repository at this point in the history
  • Loading branch information
aliemjay committed Oct 8, 2023
1 parent 5be0b22 commit 996ffcb
Show file tree
Hide file tree
Showing 71 changed files with 462 additions and 339 deletions.
60 changes: 23 additions & 37 deletions compiler/rustc_infer/src/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ use crate::traits::{
};

use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_errors::{error_code, Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_errors::{pluralize, struct_span_err, Diagnostic, ErrorGuaranteed, IntoDiagnosticArg};
use rustc_errors::{Applicability, DiagnosticBuilder, DiagnosticStyledString};
use rustc_hir as hir;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId};
Expand Down Expand Up @@ -2341,40 +2341,29 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
},
};

let mut err = match sub.kind() {
ty::ReEarlyBound(_) | ty::ReFree(_) if sub.has_name() => struct_span_err!(
self.tcx.sess,
span,
E0309,
"{} may not live long enough",
labeled_user_string
),
ty::ReStatic => struct_span_err!(
self.tcx.sess,
span,
E0310,
"{} may not live long enough",
labeled_user_string
),
_ => {
let mut err = struct_span_err!(
self.tcx.sess,
span,
E0311,
"{} may not live long enough",
labeled_user_string
);
note_and_explain_region(
self.tcx,
&mut err,
&format!("{labeled_user_string} must be valid for "),
sub,
"...",
None,
);
err
let mut err = self.tcx.sess.struct_span_err_with_code(
span,
format!("{labeled_user_string} may not live long enough"),
match sub.kind() {
ty::ReEarlyBound(_) | ty::ReFree(_) if sub.has_name() => error_code!(E0309),
ty::ReStatic => error_code!(E0310),
_ => error_code!(E0311),
},
);

'_explain: {
let (description, span) = match sub.kind() {
ty::ReEarlyBound(_) | ty::ReFree(_) | ty::ReStatic => {
msg_span_from_named_region(self.tcx, sub, Some(span))
}
_ => (format!("lifetime `{sub}`"), Some(span)),
};
let prefix = format!("{labeled_user_string} must be valid for ");
label_msg_span(&mut err, &prefix, description, span, "...");
if let Some(origin) = origin {
self.note_region_origin(&mut err, &origin);
}
};
}

'suggestion: {
let msg = "consider adding an explicit lifetime bound";
Expand Down Expand Up @@ -2450,9 +2439,6 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
}

if let Some(origin) = origin {
self.note_region_origin(&mut err, &origin);
}
err
}

Expand Down
4 changes: 3 additions & 1 deletion tests/ui/associated-inherent-types/regionck-1.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/regionck-1.rs:9:30
|
LL | type NoTyOutliv<'a, T> = &'a T;
| ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| -- ^^^^^ ...so that the reference type `&'a T` does not outlive the data it points at
| |
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand Down
30 changes: 8 additions & 22 deletions tests/ui/async-await/in-trait/async-generics-and-bounds.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ error[E0311]: the parameter type `U` may not live long enough
--> $DIR/async-generics-and-bounds.rs:12:5
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | the parameter type `U` must be valid for the anonymous lifetime as defined here...
| ...so that the reference type `&(T, U)` does not outlive the data it points at
|
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics-and-bounds.rs:12:18
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics-and-bounds.rs:12:5
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, U: 'a;
Expand All @@ -23,18 +16,11 @@ error[E0311]: the parameter type `T` may not live long enough
--> $DIR/async-generics-and-bounds.rs:12:5
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| | |
| | the parameter type `T` must be valid for the anonymous lifetime as defined here...
| ...so that the reference type `&(T, U)` does not outlive the data it points at
|
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics-and-bounds.rs:12:18
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics-and-bounds.rs:12:5
|
LL | async fn foo(&self) -> &(T, U) where T: Debug + Sized, U: Hash;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: Debug + Sized, U: Hash, T: 'a;
Expand Down
30 changes: 8 additions & 22 deletions tests/ui/async-await/in-trait/async-generics.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@ error[E0311]: the parameter type `U` may not live long enough
--> $DIR/async-generics.rs:9:5
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^
| | |
| | the parameter type `U` must be valid for the anonymous lifetime as defined here...
| ...so that the reference type `&(T, U)` does not outlive the data it points at
|
note: the parameter type `U` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics.rs:9:18
|
LL | async fn foo(&self) -> &(T, U);
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics.rs:9:5
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where U: 'a;
Expand All @@ -23,18 +16,11 @@ error[E0311]: the parameter type `T` may not live long enough
--> $DIR/async-generics.rs:9:5
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^-^^^^^^^^^^^^^^^^^
| | |
| | the parameter type `T` must be valid for the anonymous lifetime as defined here...
| ...so that the reference type `&(T, U)` does not outlive the data it points at
|
note: the parameter type `T` must be valid for the anonymous lifetime as defined here...
--> $DIR/async-generics.rs:9:18
|
LL | async fn foo(&self) -> &(T, U);
| ^
note: ...so that the reference type `&(T, U)` does not outlive the data it points at
--> $DIR/async-generics.rs:9:5
|
LL | async fn foo(&self) -> &(T, U);
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
help: consider adding an explicit lifetime bound...
|
LL | async fn foo<'a>(&'a self) -> &'a (T, U) where T: 'a;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/builtin-superkinds-self-type.rs:10:16
|
LL | impl <T: Sync> Foo for T { }
| ^^^ ...so that the type `T` will meet its required lifetime bounds...
| ^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds...
|
note: ...that is required by this bound
--> $DIR/builtin-superkinds-self-type.rs:6:24
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/coercion/issue-53475.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/issue-53475.rs:10:1
|
LL | impl<T> CoerceUnsized<Foo<dyn Any>> for Foo<T> {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
Expand Down
10 changes: 8 additions & 2 deletions tests/ui/consts/issue-102117.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/issue-102117.rs:19:26
|
LL | type_id: TypeId::of::<T>(),
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -13,7 +16,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/issue-102117.rs:19:26
|
LL | type_id: TypeId::of::<T>(),
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider adding an explicit lifetime bound...
Expand Down
14 changes: 3 additions & 11 deletions tests/ui/error-codes/E0311.stderr
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
error[E0311]: the parameter type `T` may not live long enough
--> $DIR/E0311.rs:6:5
|
LL | with_restriction::<T>(x)
| ^^^^^^^^^^^^^^^^^^^^^
|
note: the parameter type `T` must be valid for the anonymous lifetime defined here...
--> $DIR/E0311.rs:5:25
|
LL | fn no_restriction<T>(x: &()) -> &() {
| ^^^
note: ...so that the type `T` will meet its required lifetime bounds
--> $DIR/E0311.rs:6:5
|
| --- the parameter type `T` must be valid for the anonymous lifetime defined here...
LL | with_restriction::<T>(x)
| ^^^^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
LL | fn no_restriction<'a, T: 'a>(x: &'a ()) -> &'a () {
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/fn/implied-bounds-unnorm-associated-type-5.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:6:13
|
LL | impl<'a, T> Trait<'a> for T {
| ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
| -- ^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds...
| |
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
|
note: ...that is required by this bound
--> $DIR/implied-bounds-unnorm-associated-type-5.rs:1:18
Expand Down
4 changes: 3 additions & 1 deletion tests/ui/generic-associated-types/issue-84931.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ error[E0309]: the parameter type `T` may not live long enough
--> $DIR/issue-84931.rs:14:21
|
LL | type Item<'a> = &'a mut T;
| ^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at
| -- ^^^^^^^^^ ...so that the reference type `&'a mut T` does not outlive the data it points at
| |
| the parameter type `T` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/must_outlive_least_region_or_bound.rs:43:5
|
LL | x
| ^ ...so that the type `T` will meet its required lifetime bounds
| ^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/impl-trait/type_parameters_captured.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/type_parameters_captured.rs:8:5
|
LL | x
| ^ ...so that the type `T` will meet its required lifetime bounds
| ^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
Expand Down
3 changes: 3 additions & 0 deletions tests/ui/impl-trait/unactionable_diagnostic.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
error[E0309]: the parameter type `T` may not live long enough
--> $DIR/unactionable_diagnostic.rs:21:5
|
LL | pub fn bar<'t, T>(
| -- the parameter type `T` must be valid for the lifetime `'t` as defined here...
...
LL | foo(post, x)
| ^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
|
Expand Down
25 changes: 19 additions & 6 deletions tests/ui/lifetimes/lifetime-doesnt-live-long-enough.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:19:10
|
LL | foo: &'static T
| ^^^^^^^^^^ ...so that the reference type `&'static T` does not outlive the data it points at
| ^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the reference type `&'static T` does not outlive the data it points at
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -13,7 +16,9 @@ error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:41:33
|
LL | fn generic_in_parent<'a, L: X<&'a Nested<K>>>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| |
| the parameter type `K` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -24,7 +29,9 @@ error[E0309]: the parameter type `M` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:44:36
|
LL | fn generic_in_child<'a, 'b, L: X<&'a Nested<M>>, M: 'b>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<M>` does not outlive the data it points at
| |
| the parameter type `M` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -35,7 +42,9 @@ error[E0309]: the parameter type `K` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:24:19
|
LL | fn foo<'a, L: X<&'a Nested<K>>>();
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<K>` does not outlive the data it points at
| |
| the parameter type `K` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -46,7 +55,9 @@ error[E0309]: the parameter type `Self` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:28:19
|
LL | fn bar<'a, L: X<&'a Nested<Self>>>();
| ^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
| -- ^^^^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<Self>` does not outlive the data it points at
| |
| the parameter type `Self` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand All @@ -57,7 +68,9 @@ error[E0309]: the parameter type `L` may not live long enough
--> $DIR/lifetime-doesnt-live-long-enough.rs:32:22
|
LL | fn baz<'a, L, M: X<&'a Nested<L>>>() {
| ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
| -- ^^^^^^^^^^^^^^^^ ...so that the reference type `&'a Nested<L>` does not outlive the data it points at
| |
| the parameter type `L` must be valid for the lifetime `'a` as defined here...
|
help: consider adding an explicit lifetime bound...
|
Expand Down
5 changes: 4 additions & 1 deletion tests/ui/lifetimes/lifetime-errors/issue_74400.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ error[E0310]: the parameter type `T` may not live long enough
--> $DIR/issue_74400.rs:12:5
|
LL | f(data, identity)
| ^^^^^^^^^^^^^^^^^ ...so that the type `T` will meet its required lifetime bounds
| ^^^^^^^^^^^^^^^^^
| |
| the parameter type `T` must be valid for the static lifetime...
| ...so that the type `T` will meet its required lifetime bounds
|
help: consider adding an explicit lifetime bound...
|
Expand Down
Loading

0 comments on commit 996ffcb

Please sign in to comment.