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.
This commit is contained in:
Matthias Krüger 2024-08-22 08:17:23 +02:00 committed by GitHub
commit 8b3ca7918c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 19 additions and 39 deletions

View File

@ -1079,16 +1079,8 @@ fn msg_span_from_named_region<'tcx>(
) -> (String, Option<Span>) { ) -> (String, Option<Span>) {
match *region { match *region {
ty::ReEarlyParam(br) => { ty::ReEarlyParam(br) => {
let scope = tcx let param_def_id = tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id;
.parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id) let span = tcx.def_span(param_def_id);
.expect_local();
let span = if let Some(param) =
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(br.name))
{
param.span
} else {
tcx.def_span(scope)
};
let text = if br.has_name() { let text = if br.has_name() {
format!("the lifetime `{}` as defined here", br.name) format!("the lifetime `{}` as defined here", br.name)
} else { } else {
@ -1104,16 +1096,8 @@ fn msg_span_from_named_region<'tcx>(
("the anonymous lifetime defined here".to_string(), Some(ty.span)) ("the anonymous lifetime defined here".to_string(), Some(ty.span))
} else { } else {
match fr.bound_region { match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => { ty::BoundRegionKind::BrNamed(param_def_id, name) => {
let span = if let Some(param) = tcx let span = tcx.def_span(param_def_id);
.hir()
.get_generics(generic_param_scope)
.and_then(|generics| generics.get_named(name))
{
param.span
} else {
tcx.def_span(generic_param_scope)
};
let text = if name == kw::UnderscoreLifetime { let text = if name == kw::UnderscoreLifetime {
"the anonymous lifetime as defined here".to_string() "the anonymous lifetime as defined here".to_string()
} else { } else {

View File

@ -30,9 +30,9 @@ note: the lifetime `'c` as defined here...
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) { LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^ | ^^
note: ...does not necessarily outlive the lifetime `'c` as defined here note: ...does not necessarily outlive the lifetime `'c` as defined here
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24 --> $DIR/regions-bound-missing-bound-in-impl.rs:12:24
| |
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) { LL | fn wrong_bound1<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
| ^^ | ^^
error[E0308]: method not compatible with trait 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
= note: expected signature `fn(&'a _, Inv<'c>, Inv<'c>, Inv<'_>)` = note: expected signature `fn(&'a _, Inv<'c>, Inv<'c>, Inv<'_>)`
found signature `fn(&'a _, Inv<'_>, Inv<'c>, Inv<'_>)` found signature `fn(&'a _, Inv<'_>, Inv<'c>, Inv<'_>)`
note: the lifetime `'c` as defined here... note: the lifetime `'c` as defined here...
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24 --> $DIR/regions-bound-missing-bound-in-impl.rs:12:24
| |
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) { LL | fn wrong_bound1<'b,'c,'d:'a+'b>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>);
| ^^ | ^^
note: ...does not necessarily outlive the lifetime `'c` as defined here note: ...does not necessarily outlive the lifetime `'c` as defined here
--> $DIR/regions-bound-missing-bound-in-impl.rs:27:24 --> $DIR/regions-bound-missing-bound-in-impl.rs:27:24
| |
LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) { LL | fn wrong_bound1<'b,'c,'d:'a+'c>(self, b: Inv<'b>, c: Inv<'c>, d: Inv<'d>) {
| ^^ | ^^
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
error[E0195]: lifetime parameters or bounds on method `wrong_bound2` do not match the trait declaration error[E0195]: lifetime parameters or bounds on method `wrong_bound2` do not match the trait declaration
--> $DIR/regions-bound-missing-bound-in-impl.rs:42:20 --> $DIR/regions-bound-missing-bound-in-impl.rs:42:20

View File

@ -48,10 +48,10 @@ LL | const STATIC: &str = "";
= note: expected reference `&'static _` = note: expected reference `&'static _`
found reference `&_` found reference `&_`
note: the anonymous lifetime as defined here... note: the anonymous lifetime as defined here...
--> $DIR/elided-lifetime.rs:15:18 --> $DIR/elided-lifetime.rs:16:19
| |
LL | impl Bar for Foo<'_> { LL | const STATIC: &str = "";
| ^^ | ^
= note: ...does not necessarily outlive the static lifetime = note: ...does not necessarily outlive the static lifetime
error: aborting due to 3 previous errors error: aborting due to 3 previous errors

View File

@ -30,10 +30,10 @@ LL | const STATIC: &str = "";
= note: expected reference `&_` = note: expected reference `&_`
found reference `&_` found reference `&_`
note: the anonymous lifetime as defined here... note: the anonymous lifetime as defined here...
--> $DIR/static-trait-impl.rs:8:10 --> $DIR/static-trait-impl.rs:9:19
| |
LL | impl Bar<'_> for A { LL | const STATIC: &str = "";
| ^^ | ^
note: ...does not necessarily outlive the anonymous lifetime as defined here note: ...does not necessarily outlive the anonymous lifetime as defined here
--> $DIR/static-trait-impl.rs:8:10 --> $DIR/static-trait-impl.rs:8:10
| |

View File

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

View File

@ -5,10 +5,10 @@ LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
| |
note: first, the lifetime cannot outlive the anonymous lifetime as defined here... note: first, the lifetime cannot outlive the anonymous lifetime as defined here...
--> $DIR/issue-20831-debruijn.rs:28:18 --> $DIR/issue-20831-debruijn.rs:28:67
| |
LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) { LL | fn subscribe(&mut self, t : Box<dyn Subscriber<Input=<Self as Publisher>::Output> + 'a>) {
| ^ | ^^^^^^^^^
note: ...but the lifetime must also be valid for the lifetime `'a` as defined here... note: ...but the lifetime must also be valid for the lifetime `'a` as defined here...
--> $DIR/issue-20831-debruijn.rs:26:6 --> $DIR/issue-20831-debruijn.rs:26:6
| |

View File

@ -7,10 +7,7 @@ LL | fn next(&'a mut self) -> Option<Self::Item>
= note: expected signature `fn(&mut RepeatMut<'_, _>) -> Option<_>` = note: expected signature `fn(&mut RepeatMut<'_, _>) -> Option<_>`
found signature `fn(&'a mut RepeatMut<'_, _>) -> Option<_>` found signature `fn(&'a mut RepeatMut<'_, _>) -> Option<_>`
note: the anonymous lifetime as defined here... note: the anonymous lifetime as defined here...
--> $DIR/issue-37884.rs:6:5 --> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
|
LL | fn next(&'a mut self) -> Option<Self::Item>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: ...does not necessarily outlive the lifetime `'a` as defined here note: ...does not necessarily outlive the lifetime `'a` as defined here
--> $DIR/issue-37884.rs:3:6 --> $DIR/issue-37884.rs:3:6
| |