Skip to content

Commit 3162b33

Browse files
committed
Handle anonymous lifetimes properly in diagnostics.
1 parent 7b86c6f commit 3162b33

File tree

3 files changed

+39
-37
lines changed

3 files changed

+39
-37
lines changed

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -567,15 +567,17 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
567567
let lifetime =
568568
self.try_match_adt_and_generic_args(substs, needle_fr, args, search_stack)?;
569569
match lifetime.name {
570-
hir::LifetimeName::Param(_)
570+
hir::LifetimeName::Param(hir::ParamName::Plain(_) | hir::ParamName::Error)
571571
| hir::LifetimeName::Error
572-
| hir::LifetimeName::Static
573-
| hir::LifetimeName::Underscore => {
572+
| hir::LifetimeName::Static => {
574573
let lifetime_span = lifetime.span;
575574
Some(RegionNameHighlight::MatchedAdtAndSegment(lifetime_span))
576575
}
577576

578-
hir::LifetimeName::ImplicitObjectLifetimeDefault | hir::LifetimeName::Implicit => {
577+
hir::LifetimeName::Param(hir::ParamName::Fresh(_))
578+
| hir::LifetimeName::ImplicitObjectLifetimeDefault
579+
| hir::LifetimeName::Implicit
580+
| hir::LifetimeName::Underscore => {
579581
// In this case, the user left off the lifetime; so
580582
// they wrote something like:
581583
//

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+31-31
Original file line numberDiff line numberDiff line change
@@ -161,45 +161,45 @@ fn msg_span_from_early_bound_and_free_regions<'tcx>(
161161
{
162162
sp = param.span;
163163
}
164-
let text = if br.name == kw::UnderscoreLifetime {
165-
format!("the anonymous lifetime as defined here")
166-
} else {
164+
let text = if br.has_name() {
167165
format!("the lifetime `{}` as defined here", br.name)
166+
} else {
167+
format!("the anonymous lifetime as defined here")
168168
};
169169
(text, sp)
170170
}
171-
ty::ReFree(ty::FreeRegion {
172-
bound_region: ty::BoundRegionKind::BrNamed(_, name), ..
173-
}) => {
174-
let mut sp = sm.guess_head_span(tcx.def_span(scope));
175-
if let Some(param) =
176-
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
171+
ty::ReFree(ref fr) => {
172+
if !fr.bound_region.is_named()
173+
&& let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region)
177174
{
178-
sp = param.span;
179-
}
180-
let text = if name == kw::UnderscoreLifetime {
181-
format!("the anonymous lifetime as defined here")
175+
("the anonymous lifetime defined here".to_string(), ty.span)
182176
} else {
183-
format!("the lifetime `{}` as defined here", name)
184-
};
185-
(text, sp)
186-
}
187-
ty::ReFree(ref fr) => match fr.bound_region {
188-
ty::BrAnon(idx) => {
189-
if let Some((ty, _)) = find_anon_type(tcx, region, &fr.bound_region) {
190-
("the anonymous lifetime defined here".to_string(), ty.span)
191-
} else {
192-
(
177+
match fr.bound_region {
178+
ty::BoundRegionKind::BrNamed(_, name) => {
179+
let mut sp = sm.guess_head_span(tcx.def_span(scope));
180+
if let Some(param) =
181+
tcx.hir().get_generics(scope).and_then(|generics| generics.get_named(name))
182+
{
183+
sp = param.span;
184+
}
185+
let text = if name == kw::UnderscoreLifetime {
186+
format!("the anonymous lifetime as defined here")
187+
} else {
188+
format!("the lifetime `{}` as defined here", name)
189+
};
190+
(text, sp)
191+
}
192+
ty::BrAnon(idx) => (
193193
format!("the anonymous lifetime #{} defined here", idx + 1),
194-
tcx.def_span(scope),
195-
)
194+
tcx.def_span(scope)
195+
),
196+
_ => (
197+
format!("the lifetime `{}` as defined here", region),
198+
sm.guess_head_span(tcx.def_span(scope)),
199+
),
196200
}
197201
}
198-
_ => (
199-
format!("the lifetime `{}` as defined here", region),
200-
sm.guess_head_span(tcx.def_span(scope)),
201-
),
202-
},
202+
}
203203
_ => bug!(),
204204
}
205205
}
@@ -2555,7 +2555,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
25552555
ty::ReEarlyBound(ty::EarlyBoundRegion { name, .. })
25562556
| ty::ReFree(ty::FreeRegion { bound_region: ty::BrNamed(_, name), .. }),
25572557
_,
2558-
) => {
2558+
) if name != kw::UnderscoreLifetime => {
25592559
// Does the required lifetime have a nice name we can print?
25602560
let mut err = struct_span_err!(
25612561
self.tcx.sess,

src/test/ui/self/arbitrary_self_types_pin_lifetime_impl_trait-async.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error[E0700]: hidden type for `impl Trait` captures lifetime that does not appea
22
--> $DIR/arbitrary_self_types_pin_lifetime_impl_trait-async.rs:8:48
33
|
44
LL | async fn f(self: Pin<&Self>) -> impl Clone { self }
5-
| - ^^^^^^^^
5+
| ----- ^^^^^^^^
66
| |
7-
| hidden type `Pin<&Foo>` captures the anonymous lifetime as defined here
7+
| hidden type `Pin<&Foo>` captures the anonymous lifetime defined here
88
|
99
help: to declare that the `impl Trait` captures `'_`, you can add an explicit `'_` lifetime bound
1010
|

0 commit comments

Comments
 (0)