Skip to content

Commit 8b3ca79

Browse files
authored
Rollup merge of #129388 - cjgillot:region-def-id, r=compiler-errors
Do not rely on names to find lifetimes. For some reason, we were trying to find the lifetime parameter from its name, instead of using the def_id we have. This PR uses it instead. This changes some ui tests, I think to be more sensible.
2 parents d24e6b7 + ca7c55f commit 8b3ca79

File tree

7 files changed

+19
-39
lines changed

7 files changed

+19
-39
lines changed

compiler/rustc_trait_selection/src/error_reporting/infer/region.rs

+4-20
Original file line numberDiff line numberDiff line change
@@ -1079,16 +1079,8 @@ fn msg_span_from_named_region<'tcx>(
10791079
) -> (String, Option<Span>) {
10801080
match *region {
10811081
ty::ReEarlyParam(br) => {
1082-
let scope = tcx
1083-
.parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id)
1084-
.expect_local();
1085-
let span = if let Some(param) =
1086-
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
1087-
{
1088-
param.span
1089-
} else {
1090-
tcx.def_span(scope)
1091-
};
1082+
let param_def_id = tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id;
1083+
let span = tcx.def_span(param_def_id);
10921084
let text = if br.has_name() {
10931085
format!("the lifetime `{}` as defined here", br.name)
10941086
} else {
@@ -1104,16 +1096,8 @@ fn msg_span_from_named_region<'tcx>(
11041096
("the anonymous lifetime defined here".to_string(), Some(ty.span))
11051097
} else {
11061098
match fr.bound_region {
1107-
ty::BoundRegionKind::BrNamed(_, name) => {
1108-
let span = if let Some(param) = tcx
1109-
.hir()
1110-
.get_generics(generic_param_scope)
1111-
.and_then(|generics| generics.get_named(name))
1112-
{
1113-
param.span
1114-
} else {
1115-
tcx.def_span(generic_param_scope)
1116-
};
1099+
ty::BoundRegionKind::BrNamed(param_def_id, name) => {
1100+
let span = tcx.def_span(param_def_id);
11171101
let text = if name == kw::UnderscoreLifetime {
11181102
"the anonymous lifetime as defined here".to_string()
11191103
} else {

tests/ui/borrowck/regions-bound-missing-bound-in-impl.stderr

+4-5
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ note: the lifetime `'c` as defined here...
3030
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
3131
| ^^
3232
note: ...does not necessarily outlive the lifetime `'c` as defined here
33-
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
33+
--> $DIR/regions-bound-missing-bound-in-impl.rs:12:24
3434
|
35-
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
35+
LL | fn wrong_bound1<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
3636
| ^^
3737

3838
error[E0308]: method not compatible with trait
@@ -44,16 +44,15 @@ LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d
4444
= note: expected signature `fn(&'a _, Inv<'c>, Inv<'c>, Inv<'_>)`
4545
found signature `fn(&'a _, Inv<'_>, Inv<'c>, Inv<'_>)`
4646
note: the lifetime `'c` as defined here...
47-
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
47+
--> $DIR/regions-bound-missing-bound-in-impl.rs:12:24
4848
|
49-
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
49+
LL | fn wrong_bound1<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
5050
| ^^
5151
note: ...does not necessarily outlive the lifetime `'c` as defined here
5252
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
5353
|
5454
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
5555
| ^^
56-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
5756

5857
error[E0195]: lifetime parameters or bounds on method `wrong_bound2` do not match the trait declaration
5958
--> $DIR/regions-bound-missing-bound-in-impl.rs:42:20

tests/ui/consts/static-default-lifetime/elided-lifetime.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ LL | const STATIC: &str = "";
4848
= note: expected reference `&'static _`
4949
found reference `&_`
5050
note: the anonymous lifetime as defined here...
51-
--> $DIR/elided-lifetime.rs:15:18
51+
--> $DIR/elided-lifetime.rs:16:19
5252
|
53-
LL | impl Bar for Foo<'_> {
54-
| ^^
53+
LL | const STATIC: &str = "";
54+
| ^
5555
= note: ...does not necessarily outlive the static lifetime
5656

5757
error: aborting due to 3 previous errors

tests/ui/consts/static-default-lifetime/static-trait-impl.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ LL | const STATIC: &str = "";
3030
= note: expected reference `&_`
3131
found reference `&_`
3232
note: the anonymous lifetime as defined here...
33-
--> $DIR/static-trait-impl.rs:8:10
33+
--> $DIR/static-trait-impl.rs:9:19
3434
|
35-
LL | impl Bar<'_> for A {
36-
| ^^
35+
LL | const STATIC: &str = "";
36+
| ^
3737
note: ...does not necessarily outlive the anonymous lifetime as defined here
3838
--> $DIR/static-trait-impl.rs:8:10
3939
|

tests/ui/implied-bounds/impl-implied-bounds-compatibility-unnormalized.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: first, the lifetime cannot outlive the lifetime `'s` as defined here...
8-
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:12
8+
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:8:12
99
|
10-
LL | fn get<'s>(s: &'s str, _: <&'static &'s () as Project>::Ty) -> &'static str {
10+
LL | fn get<'s>(s: &'s str, _: ()) -> &'static str;
1111
| ^^
1212
note: ...so that the method type is compatible with trait
1313
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5

tests/ui/issues/issue-20831-debruijn.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
note: first, the lifetime cannot outlive the anonymous lifetime as defined here...
8-
--> $DIR/issue-20831-debruijn.rs:28:18
8+
--> $DIR/issue-20831-debruijn.rs:28:67
99
|
1010
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
11-
| ^
11+
| ^^^^^^^^^
1212
note: ...but the lifetime must also be valid for the lifetime `'a` as defined here...
1313
--> $DIR/issue-20831-debruijn.rs:26:6
1414
|

tests/ui/issues/issue-37884.stderr

+1-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@ LL | fn next(&'a mut self) -> Option<Self::Item>
77
= note: expected signature `fn(&mut RepeatMut<'_, _>) -> Option<_>`
88
found signature `fn(&'a mut RepeatMut<'_, _>) -> Option<_>`
99
note: the anonymous lifetime as defined here...
10-
--> $DIR/issue-37884.rs:6:5
11-
|
12-
LL | fn next(&'a mut self) -> Option<Self::Item>
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
1411
note: ...does not necessarily outlive the lifetime `'a` as defined here
1512
--> $DIR/issue-37884.rs:3:6
1613
|

0 commit comments

Comments
 (0)