Skip to content

Commit 7e031b0

Browse files
committed
Auto merge of #52973 - davidtwco:issue-52663-lifetimes-not-included-in-span, r=pnkfelix
NLL mentions lifetimes that are not included in printed span(s). Part of #52663. r? @pnkfelix
2 parents 59fa6bd + 2488cb6 commit 7e031b0

10 files changed

+103
-5
lines changed

src/librustc_mir/borrow_check/nll/region_infer/error_reporting/region_name.rs

+50-4
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use rustc::hir::def_id::DefId;
1515
use rustc::infer::InferCtxt;
1616
use rustc::mir::Mir;
1717
use rustc::ty::subst::{Substs, UnpackedKind};
18-
use rustc::ty::{self, RegionVid, Ty, TyCtxt};
18+
use rustc::ty::{self, RegionKind, RegionVid, Ty, TyCtxt};
1919
use rustc::util::ppaux::with_highlight_region;
2020
use rustc_errors::DiagnosticBuilder;
21-
use syntax::ast::Name;
21+
use syntax::ast::{Name, DUMMY_NODE_ID};
2222
use syntax::symbol::keywords;
2323
use syntax_pos::symbol::InternedString;
2424

@@ -90,14 +90,21 @@ impl<'tcx> RegionInferenceContext<'tcx> {
9090
diag: &mut DiagnosticBuilder<'_>,
9191
) -> Option<InternedString> {
9292
let error_region = self.to_error_region(fr)?;
93+
9394
debug!("give_region_a_name: error_region = {:?}", error_region);
9495
match error_region {
95-
ty::ReEarlyBound(ebr) => Some(ebr.name),
96+
ty::ReEarlyBound(ebr) => {
97+
self.highlight_named_span(tcx, error_region, &ebr.name, diag);
98+
Some(ebr.name)
99+
},
96100

97101
ty::ReStatic => Some(keywords::StaticLifetime.name().as_interned_str()),
98102

99103
ty::ReFree(free_region) => match free_region.bound_region {
100-
ty::BoundRegion::BrNamed(_, name) => Some(name),
104+
ty::BoundRegion::BrNamed(_, name) => {
105+
self.highlight_named_span(tcx, error_region, &name, diag);
106+
Some(name)
107+
},
101108

102109
ty::BoundRegion::BrEnv => {
103110
let closure_span = tcx.hir.span_if_local(mir_def_id).unwrap();
@@ -123,6 +130,45 @@ impl<'tcx> RegionInferenceContext<'tcx> {
123130
}
124131
}
125132

133+
/// Highlight a named span to provide context for error messages that
134+
/// mention that span, for example:
135+
///
136+
/// ```
137+
/// |
138+
/// | fn two_regions<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
139+
/// | -- -- lifetime `'b` defined here
140+
/// | |
141+
/// | lifetime `'a` defined here
142+
/// |
143+
/// | with_signature(cell, t, |cell, t| require(cell, t));
144+
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must
145+
/// | outlive `'a`
146+
/// ```
147+
fn highlight_named_span(
148+
&self,
149+
tcx: TyCtxt<'_, '_, 'tcx>,
150+
error_region: &RegionKind,
151+
name: &InternedString,
152+
diag: &mut DiagnosticBuilder<'_>,
153+
) {
154+
let cm = tcx.sess.codemap();
155+
156+
let scope = error_region.free_region_binding_scope(tcx);
157+
let node = tcx.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID);
158+
159+
let mut sp = cm.def_span(tcx.hir.span(node));
160+
if let Some(param) = tcx.hir.get_generics(scope).and_then(|generics| {
161+
generics.get_named(name)
162+
}) {
163+
sp = param.span;
164+
}
165+
166+
diag.span_label(
167+
sp,
168+
format!("lifetime `{}` defined here", name),
169+
);
170+
}
171+
126172
/// Find an argument that contains `fr` and label it with a fully
127173
/// elaborated type, returning something like `'1`. Result looks
128174
/// like:

src/test/ui/impl-trait/static-return-lifetime-infered.nll.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ LL | self.x.iter().map(|a| a.0)
2121
error: unsatisfied lifetime constraints
2222
--> $DIR/static-return-lifetime-infered.rs:21:9
2323
|
24+
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
25+
| -- lifetime `'a` defined here
2426
LL | self.x.iter().map(|a| a.0)
2527
| ^^^^^^^^^^^^^ requires that `'a` must outlive `'static`
2628

src/test/ui/issue-10291.nll.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | x //~ ERROR E0312
77
error: unsatisfied lifetime constraints
88
--> $DIR/issue-10291.rs:12:5
99
|
10+
LL | fn test<'x>(x: &'x isize) {
11+
| -- lifetime `'x` defined here
1012
LL | drop::<Box<for<'z> FnMut(&'z isize) -> &'z isize>>(Box::new(|z| {
1113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'x` must outlive `'static`
1214

src/test/ui/issue-52213.nll.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ LL | match (&t,) { //~ ERROR cannot infer an appropriate lifetime
77
error: unsatisfied lifetime constraints
88
--> $DIR/issue-52213.rs:13:11
99
|
10+
LL | fn transmute_lifetime<'a, 'b, T>(t: &'a (T,)) -> &'b T {
11+
| -- -- lifetime `'b` defined here
12+
| |
13+
| lifetime `'a` defined here
14+
LL | match (&t,) { //~ ERROR cannot infer an appropriate lifetime
1015
LL | ((u,),) => u,
1116
| ^ requires that `'a` must outlive `'b`
1217

src/test/ui/nll/closure-requirements/region-lbr-named-does-not-outlive-static.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LL | &*x
77
error: unsatisfied lifetime constraints
88
--> $DIR/region-lbr-named-does-not-outlive-static.rs:19:5
99
|
10+
LL | fn foo<'a>(x: &'a u32) -> &'static u32 {
11+
| -- lifetime `'a` defined here
1012
LL | &*x
1113
| ^^^ requires that `'a` must outlive `'static`
1214

src/test/ui/nll/mir_check_cast_closure.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ LL | g
77
error: unsatisfied lifetime constraints
88
--> $DIR/mir_check_cast_closure.rs:16:28
99
|
10+
LL | fn bar<'a, 'b>() -> fn(&'a u32, &'b u32) -> &'a u32 {
11+
| -- -- lifetime `'b` defined here
12+
| |
13+
| lifetime `'a` defined here
1014
LL | let g: fn(_, _) -> _ = |_x, y| y;
1115
| ^^^^^^^^^ cast requires that `'b` must outlive `'a`
1216

src/test/ui/nll/mir_check_cast_unsize.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ error: unsatisfied lifetime constraints
88
--> $DIR/mir_check_cast_unsize.rs:17:46
99
|
1010
LL | fn bar<'a>(x: &'a u32) -> &'static dyn Debug {
11-
| ______________________________________________^
11+
| ________--____________________________________^
12+
| | |
13+
| | lifetime `'a` defined here
1214
LL | | //~^ ERROR unsatisfied lifetime constraints
1315
LL | | x
1416
LL | | //~^ WARNING not reporting region error due to nll

src/test/ui/nll/ty-outlives/projection-one-region-closure.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ LL | | }
5252
error: unsatisfied lifetime constraints
5353
--> $DIR/projection-one-region-closure.rs:55:5
5454
|
55+
LL | fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
56+
| -- -- lifetime `'b` defined here
57+
| |
58+
| lifetime `'a` defined here
59+
...
5560
LL | with_signature(cell, t, |cell, t| require(cell, t));
5661
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
5762

@@ -101,6 +106,11 @@ LL | | }
101106
error: unsatisfied lifetime constraints
102107
--> $DIR/projection-one-region-closure.rs:67:5
103108
|
109+
LL | fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
110+
| -- -- lifetime `'b` defined here
111+
| |
112+
| lifetime `'a` defined here
113+
...
104114
LL | with_signature(cell, t, |cell, t| require(cell, t));
105115
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
106116

@@ -150,6 +160,11 @@ LL | | }
150160
error: unsatisfied lifetime constraints
151161
--> $DIR/projection-one-region-closure.rs:89:5
152162
|
163+
LL | fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
164+
| -- -- lifetime `'b` defined here
165+
| |
166+
| lifetime `'a` defined here
167+
...
153168
LL | with_signature(cell, t, |cell, t| require(cell, t));
154169
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
155170

src/test/ui/nll/ty-outlives/projection-one-region-trait-bound-closure.stderr

+15
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ LL | | }
5151
error: unsatisfied lifetime constraints
5252
--> $DIR/projection-one-region-trait-bound-closure.rs:47:5
5353
|
54+
LL | fn no_relationships_late<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
55+
| -- -- lifetime `'b` defined here
56+
| |
57+
| lifetime `'a` defined here
58+
...
5459
LL | with_signature(cell, t, |cell, t| require(cell, t));
5560
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
5661

@@ -91,6 +96,11 @@ LL | | }
9196
error: unsatisfied lifetime constraints
9297
--> $DIR/projection-one-region-trait-bound-closure.rs:58:5
9398
|
99+
LL | fn no_relationships_early<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
100+
| -- -- lifetime `'b` defined here
101+
| |
102+
| lifetime `'a` defined here
103+
...
94104
LL | with_signature(cell, t, |cell, t| require(cell, t));
95105
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
96106

@@ -131,6 +141,11 @@ LL | | }
131141
error: unsatisfied lifetime constraints
132142
--> $DIR/projection-one-region-trait-bound-closure.rs:79:5
133143
|
144+
LL | fn projection_outlives<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
145+
| -- -- lifetime `'b` defined here
146+
| |
147+
| lifetime `'a` defined here
148+
...
134149
LL | with_signature(cell, t, |cell, t| require(cell, t));
135150
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
136151

src/test/ui/nll/ty-outlives/projection-two-region-trait-bound-closure.stderr

+5
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,11 @@ LL | | }
259259
error: unsatisfied lifetime constraints
260260
--> $DIR/projection-two-region-trait-bound-closure.rs:108:5
261261
|
262+
LL | fn two_regions<'a, 'b, T>(cell: Cell<&'a ()>, t: T)
263+
| -- -- lifetime `'b` defined here
264+
| |
265+
| lifetime `'a` defined here
266+
...
262267
LL | with_signature(cell, t, |cell, t| require(cell, t));
263268
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ argument requires that `'b` must outlive `'a`
264269

0 commit comments

Comments
 (0)