Skip to content

Commit 8772747

Browse files
committedJun 30, 2018
Auto merge of #51862 - estebank:lifetime-spans, r=nikomatsakis
Point to lifetime spans on lifetime errors
2 parents 96b4733 + 8449c5a commit 8772747

27 files changed

+236
-170
lines changed
 

‎src/librustc/hir/map/mod.rs

+28-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc_target::spec::abi::Abi;
2525
use syntax::ast::{self, Name, NodeId, CRATE_NODE_ID};
2626
use syntax::codemap::Spanned;
2727
use syntax::ext::base::MacroKind;
28-
use syntax_pos::Span;
28+
use syntax_pos::{Span, DUMMY_SP};
2929

3030
use hir::*;
3131
use hir::print::Nested;
@@ -664,6 +664,33 @@ impl<'hir> Map<'hir> {
664664
self.as_local_node_id(id).map(|id| self.get(id)) // read recorded by `get`
665665
}
666666

667+
pub fn get_generics(&self, id: DefId) -> Option<&'hir Generics> {
668+
self.get_if_local(id).and_then(|node| {
669+
match node {
670+
NodeImplItem(ref impl_item) => Some(&impl_item.generics),
671+
NodeTraitItem(ref trait_item) => Some(&trait_item.generics),
672+
NodeItem(ref item) => {
673+
match item.node {
674+
ItemFn(_, _, ref generics, _) |
675+
ItemTy(_, ref generics) |
676+
ItemEnum(_, ref generics) |
677+
ItemStruct(_, ref generics) |
678+
ItemUnion(_, ref generics) |
679+
ItemTrait(_, _, ref generics, ..) |
680+
ItemTraitAlias(ref generics, _) |
681+
ItemImpl(_, _, _, ref generics, ..) => Some(generics),
682+
_ => None,
683+
}
684+
}
685+
_ => None,
686+
}
687+
})
688+
}
689+
690+
pub fn get_generics_span(&self, id: DefId) -> Option<Span> {
691+
self.get_generics(id).map(|generics| generics.span).filter(|sp| *sp != DUMMY_SP)
692+
}
693+
667694
/// Retrieve the Node corresponding to `id`, returning None if
668695
/// cannot be found.
669696
pub fn find(&self, id: NodeId) -> Option<Node<'hir>> {

‎src/librustc/hir/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use hir::def_id::{DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX};
3131
use util::nodemap::{NodeMap, FxHashSet};
3232
use mir::mono::Linkage;
3333

34-
use syntax_pos::{Span, DUMMY_SP};
34+
use syntax_pos::{Span, DUMMY_SP, symbol::InternedString};
3535
use syntax::codemap::{self, Spanned};
3636
use rustc_target::spec::abi::Abi;
3737
use syntax::ast::{self, CrateSugar, Ident, Name, NodeId, DUMMY_NODE_ID, AsmDialect};
@@ -547,6 +547,15 @@ impl Generics {
547547

548548
own_counts
549549
}
550+
551+
pub fn get_named(&self, name: &InternedString) -> Option<&GenericParam> {
552+
for param in &self.params {
553+
if *name == param.name.ident().as_interned_str() {
554+
return Some(param);
555+
}
556+
}
557+
None
558+
}
550559
}
551560

552561
/// Synthetic Type Parameters are converted to an other form during lowering, this allows

‎src/librustc/infer/error_reporting/mod.rs

+23-5
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,8 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
189189
self,
190190
region: ty::Region<'tcx>,
191191
) -> (String, Option<Span>) {
192+
let cm = self.sess.codemap();
193+
192194
let scope = region.free_region_binding_scope(self);
193195
let node = self.hir.as_local_node_id(scope).unwrap_or(DUMMY_NODE_ID);
194196
let unknown;
@@ -219,10 +221,26 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
219221
}
220222
};
221223
let (prefix, span) = match *region {
222-
ty::ReEarlyBound(ref br) => (
223-
format!("the lifetime {} as defined on", br.name),
224-
self.sess.codemap().def_span(self.hir.span(node)),
225-
),
224+
ty::ReEarlyBound(ref br) => {
225+
let mut sp = cm.def_span(self.hir.span(node));
226+
if let Some(param) = self.hir.get_generics(scope).and_then(|generics| {
227+
generics.get_named(&br.name)
228+
}) {
229+
sp = param.span;
230+
}
231+
(format!("the lifetime {} as defined on", br.name), sp)
232+
}
233+
ty::ReFree(ty::FreeRegion {
234+
bound_region: ty::BoundRegion::BrNamed(_, ref name), ..
235+
}) => {
236+
let mut sp = cm.def_span(self.hir.span(node));
237+
if let Some(param) = self.hir.get_generics(scope).and_then(|generics| {
238+
generics.get_named(&name)
239+
}) {
240+
sp = param.span;
241+
}
242+
(format!("the lifetime {} as defined on", name), sp)
243+
}
226244
ty::ReFree(ref fr) => match fr.bound_region {
227245
ty::BrAnon(idx) => (
228246
format!("the anonymous lifetime #{} defined on", idx + 1),
@@ -234,7 +252,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
234252
),
235253
_ => (
236254
format!("the lifetime {} as defined on", fr.bound_region),
237-
self.sess.codemap().def_span(self.hir.span(node)),
255+
cm.def_span(self.hir.span(node)),
238256
),
239257
},
240258
_ => bug!(),

‎src/librustc_typeck/check/compare_method.rs

+12-9
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ fn check_region_bounds_on_impl_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
356356
impl_generics: &ty::Generics,
357357
trait_to_skol_substs: &Substs<'tcx>)
358358
-> Result<(), ErrorReported> {
359-
let span = tcx.sess.codemap().def_span(span);
360359
let trait_params = trait_generics.own_counts().lifetimes;
361360
let impl_params = impl_generics.own_counts().lifetimes;
362361

@@ -378,16 +377,20 @@ fn check_region_bounds_on_impl_method<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
378377
// are zero. Since I don't quite know how to phrase things at
379378
// the moment, give a kind of vague error message.
380379
if trait_params != impl_params {
381-
let mut err = struct_span_err!(tcx.sess,
382-
span,
383-
E0195,
384-
"lifetime parameters or bounds on method `{}` do not match \
385-
the trait declaration",
386-
impl_m.ident);
380+
let def_span = tcx.sess.codemap().def_span(span);
381+
let span = tcx.hir.get_generics_span(impl_m.def_id).unwrap_or(def_span);
382+
let mut err = struct_span_err!(
383+
tcx.sess,
384+
span,
385+
E0195,
386+
"lifetime parameters or bounds on method `{}` do not match the trait declaration",
387+
impl_m.ident,
388+
);
387389
err.span_label(span, "lifetimes do not match method in trait");
388390
if let Some(sp) = tcx.hir.span_if_local(trait_m.def_id) {
389-
err.span_label(tcx.sess.codemap().def_span(sp),
390-
"lifetimes in impl do not match this method in trait");
391+
let def_sp = tcx.sess.codemap().def_span(sp);
392+
let sp = tcx.hir.get_generics_span(trait_m.def_id).unwrap_or(def_sp);
393+
err.span_label(sp, "lifetimes in impl do not match this method in trait");
391394
}
392395
err.emit();
393396
return Err(ErrorReported);

‎src/test/ui/associated-const-impl-wrong-lifetime.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | const NAME: &'a str = "unit";
66
|
77
= note: expected type `&'static str`
88
found type `&'a str`
9-
note: the lifetime 'a as defined on the impl at 17:1...
10-
--> $DIR/associated-const-impl-wrong-lifetime.rs:17:1
9+
note: the lifetime 'a as defined on the impl at 17:6...
10+
--> $DIR/associated-const-impl-wrong-lifetime.rs:17:6
1111
|
1212
LL | impl<'a> Foo for &'a () {
13-
| ^^^^^^^^^^^^^^^^^^^^^^^
13+
| ^^
1414
= note: ...does not necessarily outlive the static lifetime
1515

1616
error: aborting due to previous error

‎src/test/ui/borrowck/borrowck-escaping-closure-error-2.nll.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ LL | //~^ ERROR E0373
77
LL | }
88
| - borrowed value only lives until here
99
|
10-
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 19:1...
11-
--> $DIR/borrowck-escaping-closure-error-2.rs:19:1
10+
note: borrowed value must be valid for the lifetime 'a as defined on the function body at 19:8...
11+
--> $DIR/borrowck-escaping-closure-error-2.rs:19:8
1212
|
1313
LL | fn foo<'a>(x: &'a i32) -> Box<FnMut()+'a> {
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
14+
| ^^
1515

1616
error: aborting due to previous error
1717

‎src/test/ui/borrowck/regions-bound-missing-bound-in-impl.rs

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub trait Foo<'a, 't> {
2020
fn no_bound<'b>(self, b: Inv<'b>);
2121
fn has_bound<'b:'a>(self, b: Inv<'b>);
2222
fn wrong_bound1<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
23+
fn wrong_bound2<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
2324
fn okay_bound<'b,'c,'d:'a+'b+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
2425
fn another_bound<'x: 'a>(self, x: Inv<'x>, y: Inv<'t>);
2526
}
@@ -47,6 +48,10 @@ impl<'a, 't> Foo<'a, 't> for &'a isize {
4748
// cases.
4849
}
4950

51+
fn wrong_bound2(self, b: Inv, c: Inv, d: Inv) {
52+
//~^ ERROR lifetime parameters or bounds on method `wrong_bound2` do not match the trait
53+
}
54+
5055
fn okay_bound<'b,'c,'e:'b+'c>(self, b: Inv<'b>, c: Inv<'c>, e: Inv<'e>) {
5156
}
5257

Original file line numberDiff line numberDiff line change
@@ -1,50 +1,59 @@
11
error[E0195]: lifetime parameters or bounds on method `no_bound` do not match the trait declaration
2-
--> $DIR/regions-bound-missing-bound-in-impl.rs:28:5
2+
--> $DIR/regions-bound-missing-bound-in-impl.rs:29:16
33
|
44
LL | fn no_bound<'b>(self, b: Inv<'b>);
5-
| ---------------------------------- lifetimes in impl do not match this method in trait
5+
| ---- lifetimes in impl do not match this method in trait
66
...
77
LL | fn no_bound<'b:'a>(self, b: Inv<'b>) {
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait
8+
| ^^^^^^^ lifetimes do not match method in trait
99

1010
error[E0195]: lifetime parameters or bounds on method `has_bound` do not match the trait declaration
11-
--> $DIR/regions-bound-missing-bound-in-impl.rs:32:5
11+
--> $DIR/regions-bound-missing-bound-in-impl.rs:33:17
1212
|
1313
LL | fn has_bound<'b:'a>(self, b: Inv<'b>);
14-
| -------------------------------------- lifetimes in impl do not match this method in trait
14+
| ------- lifetimes in impl do not match this method in trait
1515
...
1616
LL | fn has_bound<'b>(self, b: Inv<'b>) {
17-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait
17+
| ^^^^ lifetimes do not match method in trait
1818

1919
error[E0308]: method not compatible with trait
20-
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
20+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:5
2121
|
2222
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetime mismatch
2424
|
2525
= note: expected type `fn(&'a isize, Inv<'c>, Inv<'c>, Inv<'d>)`
2626
found type `fn(&'a isize, Inv<'_>, Inv<'c>, Inv<'d>)`
27-
note: the lifetime 'c as defined on the method body at 36:5...
28-
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
27+
note: the lifetime 'c as defined on the method body at 37:24...
28+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
2929
|
3030
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
31-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32-
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 36:5
33-
--> $DIR/regions-bound-missing-bound-in-impl.rs:36:5
31+
| ^^
32+
note: ...does not necessarily outlive the lifetime 'c as defined on the method body at 37:24
33+
--> $DIR/regions-bound-missing-bound-in-impl.rs:37:24
3434
|
3535
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
36-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
36+
| ^^
37+
38+
error[E0195]: lifetime parameters or bounds on method `wrong_bound2` do not match the trait declaration
39+
--> $DIR/regions-bound-missing-bound-in-impl.rs:51:5
40+
|
41+
LL | fn wrong_bound2<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
42+
| ---------------- lifetimes in impl do not match this method in trait
43+
...
44+
LL | fn wrong_bound2(self, b: Inv, c: Inv, d: Inv) {
45+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait
3746

3847
error[E0276]: impl has stricter requirements than trait
39-
--> $DIR/regions-bound-missing-bound-in-impl.rs:53:5
48+
--> $DIR/regions-bound-missing-bound-in-impl.rs:58:5
4049
|
4150
LL | fn another_bound<'x: 'a>(self, x: Inv<'x>, y: Inv<'t>);
4251
| ------------------------------------------------------- definition of `another_bound` from trait
4352
...
4453
LL | fn another_bound<'x: 't>(self, x: Inv<'x>, y: Inv<'t>) {
4554
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl has extra requirement `'x: 't`
4655

47-
error: aborting due to 4 previous errors
56+
error: aborting due to 5 previous errors
4857

4958
Some errors occurred: E0195, E0276, E0308.
5059
For more information about an error, try `rustc --explain E0195`.

‎src/test/ui/closure-expected-type/expect-region-supply-region.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ LL | |
3838
LL | | //~^ ERROR borrowed data cannot be stored outside of its closure
3939
LL | | });
4040
| |_____^
41-
note: ...does not necessarily outlive the lifetime 'x as defined on the function body at 42:1
42-
--> $DIR/expect-region-supply-region.rs:42:1
41+
note: ...does not necessarily outlive the lifetime 'x as defined on the function body at 42:30
42+
--> $DIR/expect-region-supply-region.rs:42:30
4343
|
4444
LL | fn expect_bound_supply_named<'x>() {
45-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
45+
| ^^
4646

4747
error[E0308]: mismatched types
4848
--> $DIR/expect-region-supply-region.rs:47:33
@@ -52,11 +52,11 @@ LL | closure_expecting_bound(|x: &'x u32| {
5252
|
5353
= note: expected type `&u32`
5454
found type `&'x u32`
55-
note: the lifetime 'x as defined on the function body at 42:1...
56-
--> $DIR/expect-region-supply-region.rs:42:1
55+
note: the lifetime 'x as defined on the function body at 42:30...
56+
--> $DIR/expect-region-supply-region.rs:42:30
5757
|
5858
LL | fn expect_bound_supply_named<'x>() {
59-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
59+
| ^^
6060
note: ...does not necessarily outlive the anonymous lifetime #2 defined on the body at 47:29
6161
--> $DIR/expect-region-supply-region.rs:47:29
6262
|

‎src/test/ui/error-codes/E0195.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error[E0195]: lifetime parameters or bounds on method `bar` do not match the trait declaration
2-
--> $DIR/E0195.rs:19:5
2+
--> $DIR/E0195.rs:19:11
33
|
44
LL | fn bar<'a,'b:'a>(x: &'a str, y: &'b str);
5-
| ----------------------------------------- lifetimes in impl do not match this method in trait
5+
| ---------- lifetimes in impl do not match this method in trait
66
...
77
LL | fn bar<'a,'b>(x: &'a str, y: &'b str) { //~ ERROR E0195
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait
8+
| ^^^^^^^ lifetimes do not match method in trait
99

1010
error: aborting due to previous error
1111

‎src/test/ui/error-codes/E0478.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ error[E0478]: lifetime bound not satisfied
44
LL | child: Box<Wedding<'kiss> + 'SnowWhite>, //~ ERROR E0478
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
7-
note: lifetime parameter instantiated with the lifetime 'SnowWhite as defined on the struct at 13:1
8-
--> $DIR/E0478.rs:13:1
7+
note: lifetime parameter instantiated with the lifetime 'SnowWhite as defined on the struct at 13:22
8+
--> $DIR/E0478.rs:13:22
99
|
1010
LL | struct Prince<'kiss, 'SnowWhite> {
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12-
note: but lifetime parameter must outlive the lifetime 'kiss as defined on the struct at 13:1
13-
--> $DIR/E0478.rs:13:1
11+
| ^^^^^^^^^^
12+
note: but lifetime parameter must outlive the lifetime 'kiss as defined on the struct at 13:15
13+
--> $DIR/E0478.rs:13:15
1414
|
1515
LL | struct Prince<'kiss, 'SnowWhite> {
16-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
16+
| ^^^^^
1717

1818
error: aborting due to previous error
1919

‎src/test/ui/impl-trait/region-escape-via-bound.stderr

+4-9
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,11 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea
44
LL | fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
55
| ^^^^^^^^^^^^^^
66
|
7-
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 26:1
8-
--> $DIR/region-escape-via-bound.rs:26:1
7+
note: hidden type `std::cell::Cell<&'x u32>` captures the lifetime 'x as defined on the function body at 28:7
8+
--> $DIR/region-escape-via-bound.rs:28:7
99
|
10-
LL | / fn foo(x: Cell<&'x u32>) -> impl Trait<'y>
11-
LL | | //~^ ERROR hidden type for `impl Trait` captures lifetime that does not appear in bounds [E0700]
12-
LL | | where 'x: 'y
13-
LL | | {
14-
LL | | x
15-
LL | | }
16-
| |_^
10+
LL | where 'x: 'y
11+
| ^^
1712

1813
error: aborting due to previous error
1914

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ LL | self.x.iter().map(|a| a.0)
3030
| |
3131
| ...but this borrow...
3232
|
33-
note: ...can't outlive the lifetime 'a as defined on the method body at 20:5
34-
--> $DIR/static-return-lifetime-infered.rs:20:5
33+
note: ...can't outlive the lifetime 'a as defined on the method body at 20:20
34+
--> $DIR/static-return-lifetime-infered.rs:20:20
3535
|
3636
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> {
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
38-
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime 'a as defined on the method body at 20:5
37+
| ^^
38+
help: you can add a constraint to the return type to make it last less than `'static` and match the lifetime 'a as defined on the method body at 20:20
3939
|
4040
LL | fn iter_values<'a>(&'a self) -> impl Iterator<Item=u32> + 'a {
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

‎src/test/ui/in-band-lifetimes/impl/dyn-trait.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ error[E0495]: cannot infer an appropriate lifetime due to conflicting requiremen
44
LL | static_val(x); //~ ERROR cannot infer
55
| ^
66
|
7-
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 31:1...
8-
--> $DIR/dyn-trait.rs:31:1
7+
note: first, the lifetime cannot outlive the lifetime 'a as defined on the function body at 31:26...
8+
--> $DIR/dyn-trait.rs:31:26
99
|
1010
LL | fn with_dyn_debug_static<'a>(x: Box<dyn Debug + 'a>) {
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11+
| ^^
1212
= note: ...so that the expression is assignable:
1313
expected std::boxed::Box<dyn std::fmt::Debug>
1414
found std::boxed::Box<(dyn std::fmt::Debug + 'a)>

0 commit comments

Comments
 (0)
Please sign in to comment.