Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 334ce3d

Browse files
committedSep 17, 2023
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

Lines changed: 23 additions & 37 deletions
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

Lines changed: 3 additions & 1 deletion
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
|

0 commit comments

Comments
 (0)
Please sign in to comment.