Skip to content

Commit c37ee1a

Browse files
Rollup merge of rust-lang#99728 - cjgillot:ast-lifetimes-anon-clean, r=petrochenkov
Clean up HIR-based lifetime resolution Based on rust-lang#97313. Fixes rust-lang#98932. r? `@petrochenkov`
2 parents a7f4eb9 + 4b2f06b commit c37ee1a

File tree

15 files changed

+202
-747
lines changed

15 files changed

+202
-747
lines changed

compiler/rustc_ast_lowering/src/lib.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -1883,29 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
18831883
}
18841884
hir::LifetimeName::Param(param, ParamName::Fresh)
18851885
}
1886-
LifetimeRes::Anonymous { binder, elided } => {
1887-
let mut l_name = None;
1888-
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
1889-
if !captured_lifetimes.binders_to_ignore.contains(&binder) {
1890-
let p_id = self.next_node_id();
1891-
let p_def_id = self.create_def(
1892-
captured_lifetimes.parent_def_id,
1893-
p_id,
1894-
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
1895-
);
1896-
captured_lifetimes
1897-
.captures
1898-
.insert(p_def_id, (span, p_id, ParamName::Fresh, res));
1899-
l_name = Some(hir::LifetimeName::Param(p_def_id, ParamName::Fresh));
1900-
}
1901-
self.captured_lifetimes = Some(captured_lifetimes);
1902-
};
1903-
l_name.unwrap_or(if elided {
1904-
hir::LifetimeName::Implicit
1905-
} else {
1906-
hir::LifetimeName::Underscore
1907-
})
1908-
}
1886+
LifetimeRes::Infer => hir::LifetimeName::Infer,
19091887
LifetimeRes::Static => hir::LifetimeName::Static,
19101888
LifetimeRes::Error => hir::LifetimeName::Error,
19111889
res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),

compiler/rustc_borrowck/src/diagnostics/region_name.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -589,8 +589,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {
589589

590590
hir::LifetimeName::Param(_, hir::ParamName::Fresh)
591591
| hir::LifetimeName::ImplicitObjectLifetimeDefault
592-
| hir::LifetimeName::Implicit
593-
| hir::LifetimeName::Underscore => {
592+
| hir::LifetimeName::Infer => {
594593
// In this case, the user left off the lifetime; so
595594
// they wrote something like:
596595
//

compiler/rustc_hir/src/def.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -738,13 +738,8 @@ pub enum LifetimeRes {
738738
binder: NodeId,
739739
},
740740
/// This variant is used for anonymous lifetimes that we did not resolve during
741-
/// late resolution. Shifting the work to the HIR lifetime resolver.
742-
Anonymous {
743-
/// Id of the introducing place. See `Param`.
744-
binder: NodeId,
745-
/// Whether this lifetime was spelled or elided.
746-
elided: bool,
747-
},
741+
/// late resolution. Those lifetimes will be inferred by typechecking.
742+
Infer,
748743
/// Explicit `'static` lifetime.
749744
Static,
750745
/// Resolution failure.

compiler/rustc_hir/src/hir.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,6 @@ pub enum LifetimeName {
9090
/// User-given names or fresh (synthetic) names.
9191
Param(LocalDefId, ParamName),
9292

93-
/// User wrote nothing (e.g., the lifetime in `&u32`).
94-
Implicit,
95-
9693
/// Implicit lifetime in a context like `dyn Foo`. This is
9794
/// distinguished from implicit lifetimes elsewhere because the
9895
/// lifetime that they default to must appear elsewhere within the
@@ -110,8 +107,9 @@ pub enum LifetimeName {
110107
/// that was already reported.
111108
Error,
112109

113-
/// User wrote specifies `'_`.
114-
Underscore,
110+
/// User wrote an anonymous lifetime, either `'_` or nothing.
111+
/// The semantics of this lifetime should be inferred by typechecking code.
112+
Infer,
115113

116114
/// User wrote `'static`.
117115
Static,
@@ -120,10 +118,8 @@ pub enum LifetimeName {
120118
impl LifetimeName {
121119
pub fn ident(&self) -> Ident {
122120
match *self {
123-
LifetimeName::ImplicitObjectLifetimeDefault
124-
| LifetimeName::Implicit
125-
| LifetimeName::Error => Ident::empty(),
126-
LifetimeName::Underscore => Ident::with_dummy_span(kw::UnderscoreLifetime),
121+
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Error => Ident::empty(),
122+
LifetimeName::Infer => Ident::with_dummy_span(kw::UnderscoreLifetime),
127123
LifetimeName::Static => Ident::with_dummy_span(kw::StaticLifetime),
128124
LifetimeName::Param(_, param_name) => param_name.ident(),
129125
}
@@ -132,8 +128,7 @@ impl LifetimeName {
132128
pub fn is_anonymous(&self) -> bool {
133129
match *self {
134130
LifetimeName::ImplicitObjectLifetimeDefault
135-
| LifetimeName::Implicit
136-
| LifetimeName::Underscore
131+
| LifetimeName::Infer
137132
| LifetimeName::Param(_, ParamName::Fresh)
138133
| LifetimeName::Error => true,
139134
LifetimeName::Static | LifetimeName::Param(..) => false,
@@ -142,9 +137,7 @@ impl LifetimeName {
142137

143138
pub fn is_elided(&self) -> bool {
144139
match self {
145-
LifetimeName::ImplicitObjectLifetimeDefault
146-
| LifetimeName::Implicit
147-
| LifetimeName::Underscore => true,
140+
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,
148141

149142
// It might seem surprising that `Fresh` counts as
150143
// *not* elided -- but this is because, as far as the code

compiler/rustc_hir/src/intravisit.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -496,9 +496,8 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
496496
| LifetimeName::Param(_, ParamName::Error)
497497
| LifetimeName::Static
498498
| LifetimeName::Error
499-
| LifetimeName::Implicit
500499
| LifetimeName::ImplicitObjectLifetimeDefault
501-
| LifetimeName::Underscore => {}
500+
| LifetimeName::Infer => {}
502501
}
503502
}
504503

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/find_anon_type.rs

+1-30
Original file line numberDiff line numberDiff line change
@@ -100,23 +100,6 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
100100
// the lifetime of the TyRptr
101101
let hir_id = lifetime.hir_id;
102102
match (self.tcx.named_region(hir_id), self.bound_region) {
103-
// Find the index of the anonymous region that was part of the
104-
// error. We will then search the function parameters for a bound
105-
// region at the right depth with the same index
106-
(
107-
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
108-
ty::BrAnon(br_index),
109-
) => {
110-
debug!(
111-
"LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
112-
debruijn_index, anon_index, br_index
113-
);
114-
if debruijn_index == self.current_index && anon_index == br_index {
115-
self.found_type = Some(arg);
116-
return; // we can stop visiting now
117-
}
118-
}
119-
120103
// Find the index of the named region that was part of the
121104
// error. We will then search the function parameters for a bound
122105
// region at the right depth with the same index
@@ -151,8 +134,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
151134
rl::Region::Static
152135
| rl::Region::Free(_, _)
153136
| rl::Region::EarlyBound(_, _)
154-
| rl::Region::LateBound(_, _, _)
155-
| rl::Region::LateBoundAnon(_, _, _),
137+
| rl::Region::LateBound(_, _, _),
156138
)
157139
| None,
158140
_,
@@ -206,16 +188,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
206188
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
207189
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
208190
// the lifetime of the TyPath!
209-
(
210-
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
211-
ty::BrAnon(br_index),
212-
) => {
213-
if debruijn_index == self.current_index && anon_index == br_index {
214-
self.found_it = true;
215-
return;
216-
}
217-
}
218-
219191
(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
220192
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
221193
if id == def_id {
@@ -239,7 +211,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
239211
rl::Region::Static
240212
| rl::Region::EarlyBound(_, _)
241213
| rl::Region::LateBound(_, _, _)
242-
| rl::Region::LateBoundAnon(_, _, _)
243214
| rl::Region::Free(_, _),
244215
)
245216
| None,

compiler/rustc_middle/src/middle/resolve_lifetime.rs

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ pub enum Region {
1212
Static,
1313
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
1414
LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
15-
LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
1615
Free(DefId, /* lifetime decl */ DefId),
1716
}
1817

0 commit comments

Comments
 (0)