Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not rely on names to find lifetimes. #129388

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 4 additions & 20 deletions compiler/rustc_trait_selection/src/error_reporting/infer/region.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1079,16 +1079,8 @@ fn msg_span_from_named_region<'tcx>(
) -> (String, Option<Span>) {
match *region {
ty::ReEarlyParam(br) => {
let scope = tcx
.parent(tcx.generics_of(generic_param_scope).region_param(br, tcx).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 param_def_id = tcx.generics_of(generic_param_scope).region_param(br, tcx).def_id;
let span = tcx.def_span(param_def_id);
let text = if br.has_name() {
format!("the lifetime `{}` as defined here", br.name)
} else {
Expand All @@ -1104,16 +1096,8 @@ fn msg_span_from_named_region<'tcx>(
("the anonymous lifetime defined here".to_string(), Some(ty.span))
} else {
match fr.bound_region {
ty::BoundRegionKind::BrNamed(_, name) => {
let span = if let Some(param) = tcx
.hir()
.get_generics(generic_param_scope)
.and_then(|generics| generics.get_named(name))
{
param.span
} else {
tcx.def_span(generic_param_scope)
};
ty::BoundRegionKind::BrNamed(param_def_id, name) => {
let span = tcx.def_span(param_def_id);
let text = if name == kw::UnderscoreLifetime {
"the anonymous lifetime as defined here".to_string()
} else {
Expand Down
9 changes: 4 additions & 5 deletions tests/ui/borrowck/regions-bound-missing-bound-in-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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>) {
| ^^
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
Expand All @@ -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<'_>)`
found signature `fn(&'a _, Inv<'_>, Inv<'c>, Inv<'_>)`
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
--> $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>) {
| ^^
= 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
--> $DIR/regions-bound-missing-bound-in-impl.rs:42:20
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ LL | const STATIC: &str = "";
= note: expected reference `&'static _`
found reference `&_`
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

error: aborting due to 3 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ LL | const STATIC: &str = "";
= note: expected reference `&_`
found reference `&_`
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
--> $DIR/static-trait-impl.rs:8:10
|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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...
--> $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
--> $DIR/impl-implied-bounds-compatibility-unnormalized.rs:11:5
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/issues/issue-20831-debruijn.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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...
--> $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>) {
| ^
| ^^^^^^^^^
note: ...but the lifetime must also be valid for the lifetime `'a` as defined here...
--> $DIR/issue-20831-debruijn.rs:26:6
|
Expand Down
5 changes: 1 addition & 4 deletions tests/ui/issues/issue-37884.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ LL | fn next(&'a mut self) -> Option<Self::Item>
= note: expected signature `fn(&mut RepeatMut<'_, _>) -> Option<_>`
found signature `fn(&'a mut RepeatMut<'_, _>) -> Option<_>`
note: the anonymous lifetime as defined here...
--> $DIR/issue-37884.rs:6:5
|
LL | fn next(&'a mut self) -> Option<Self::Item>
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--> $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
note: ...does not necessarily outlive the lifetime `'a` as defined here
--> $DIR/issue-37884.rs:3:6
|
Expand Down
Loading