Skip to content

Commit

Permalink
Rollup merge of rust-lang#99728 - cjgillot:ast-lifetimes-anon-clean, …
Browse files Browse the repository at this point in the history
…r=petrochenkov

Clean up HIR-based lifetime resolution

Based on rust-lang#97313.

Fixes rust-lang#98932.

r? `@petrochenkov`
  • Loading branch information
GuillaumeGomez authored Jul 27, 2022
2 parents a7f4eb9 + 4b2f06b commit c37ee1a
Show file tree
Hide file tree
Showing 15 changed files with 202 additions and 747 deletions.
24 changes: 1 addition & 23 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,29 +1883,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
}
hir::LifetimeName::Param(param, ParamName::Fresh)
}
LifetimeRes::Anonymous { binder, elided } => {
let mut l_name = None;
if let Some(mut captured_lifetimes) = self.captured_lifetimes.take() {
if !captured_lifetimes.binders_to_ignore.contains(&binder) {
let p_id = self.next_node_id();
let p_def_id = self.create_def(
captured_lifetimes.parent_def_id,
p_id,
DefPathData::LifetimeNs(kw::UnderscoreLifetime),
);
captured_lifetimes
.captures
.insert(p_def_id, (span, p_id, ParamName::Fresh, res));
l_name = Some(hir::LifetimeName::Param(p_def_id, ParamName::Fresh));
}
self.captured_lifetimes = Some(captured_lifetimes);
};
l_name.unwrap_or(if elided {
hir::LifetimeName::Implicit
} else {
hir::LifetimeName::Underscore
})
}
LifetimeRes::Infer => hir::LifetimeName::Infer,
LifetimeRes::Static => hir::LifetimeName::Static,
LifetimeRes::Error => hir::LifetimeName::Error,
res => panic!("Unexpected lifetime resolution {:?} for {:?} at {:?}", res, ident, span),
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_borrowck/src/diagnostics/region_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,8 +589,7 @@ impl<'tcx> MirBorrowckCtxt<'_, 'tcx> {

hir::LifetimeName::Param(_, hir::ParamName::Fresh)
| hir::LifetimeName::ImplicitObjectLifetimeDefault
| hir::LifetimeName::Implicit
| hir::LifetimeName::Underscore => {
| hir::LifetimeName::Infer => {
// In this case, the user left off the lifetime; so
// they wrote something like:
//
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_hir/src/def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,8 @@ pub enum LifetimeRes {
binder: NodeId,
},
/// This variant is used for anonymous lifetimes that we did not resolve during
/// late resolution. Shifting the work to the HIR lifetime resolver.
Anonymous {
/// Id of the introducing place. See `Param`.
binder: NodeId,
/// Whether this lifetime was spelled or elided.
elided: bool,
},
/// late resolution. Those lifetimes will be inferred by typechecking.
Infer,
/// Explicit `'static` lifetime.
Static,
/// Resolution failure.
Expand Down
21 changes: 7 additions & 14 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ pub enum LifetimeName {
/// User-given names or fresh (synthetic) names.
Param(LocalDefId, ParamName),

/// User wrote nothing (e.g., the lifetime in `&u32`).
Implicit,

/// Implicit lifetime in a context like `dyn Foo`. This is
/// distinguished from implicit lifetimes elsewhere because the
/// lifetime that they default to must appear elsewhere within the
Expand All @@ -110,8 +107,9 @@ pub enum LifetimeName {
/// that was already reported.
Error,

/// User wrote specifies `'_`.
Underscore,
/// User wrote an anonymous lifetime, either `'_` or nothing.
/// The semantics of this lifetime should be inferred by typechecking code.
Infer,

/// User wrote `'static`.
Static,
Expand All @@ -120,10 +118,8 @@ pub enum LifetimeName {
impl LifetimeName {
pub fn ident(&self) -> Ident {
match *self {
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Implicit
| LifetimeName::Error => Ident::empty(),
LifetimeName::Underscore => Ident::with_dummy_span(kw::UnderscoreLifetime),
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Error => Ident::empty(),
LifetimeName::Infer => Ident::with_dummy_span(kw::UnderscoreLifetime),
LifetimeName::Static => Ident::with_dummy_span(kw::StaticLifetime),
LifetimeName::Param(_, param_name) => param_name.ident(),
}
Expand All @@ -132,8 +128,7 @@ impl LifetimeName {
pub fn is_anonymous(&self) -> bool {
match *self {
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Implicit
| LifetimeName::Underscore
| LifetimeName::Infer
| LifetimeName::Param(_, ParamName::Fresh)
| LifetimeName::Error => true,
LifetimeName::Static | LifetimeName::Param(..) => false,
Expand All @@ -142,9 +137,7 @@ impl LifetimeName {

pub fn is_elided(&self) -> bool {
match self {
LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Implicit
| LifetimeName::Underscore => true,
LifetimeName::ImplicitObjectLifetimeDefault | LifetimeName::Infer => true,

// It might seem surprising that `Fresh` counts as
// *not* elided -- but this is because, as far as the code
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_hir/src/intravisit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,8 @@ pub fn walk_lifetime<'v, V: Visitor<'v>>(visitor: &mut V, lifetime: &'v Lifetime
| LifetimeName::Param(_, ParamName::Error)
| LifetimeName::Static
| LifetimeName::Error
| LifetimeName::Implicit
| LifetimeName::ImplicitObjectLifetimeDefault
| LifetimeName::Underscore => {}
| LifetimeName::Infer => {}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,6 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
// the lifetime of the TyRptr
let hir_id = lifetime.hir_id;
match (self.tcx.named_region(hir_id), self.bound_region) {
// Find the index of the anonymous region that was part of the
// error. We will then search the function parameters for a bound
// region at the right depth with the same index
(
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
ty::BrAnon(br_index),
) => {
debug!(
"LateBoundAnon depth = {:?} anon_index = {:?} br_index={:?}",
debruijn_index, anon_index, br_index
);
if debruijn_index == self.current_index && anon_index == br_index {
self.found_type = Some(arg);
return; // we can stop visiting now
}
}

// Find the index of the named region that was part of the
// error. We will then search the function parameters for a bound
// region at the right depth with the same index
Expand Down Expand Up @@ -151,8 +134,7 @@ impl<'tcx> Visitor<'tcx> for FindNestedTypeVisitor<'tcx> {
rl::Region::Static
| rl::Region::Free(_, _)
| rl::Region::EarlyBound(_, _)
| rl::Region::LateBound(_, _, _)
| rl::Region::LateBoundAnon(_, _, _),
| rl::Region::LateBound(_, _, _),
)
| None,
_,
Expand Down Expand Up @@ -206,16 +188,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
fn visit_lifetime(&mut self, lifetime: &hir::Lifetime) {
match (self.tcx.named_region(lifetime.hir_id), self.bound_region) {
// the lifetime of the TyPath!
(
Some(rl::Region::LateBoundAnon(debruijn_index, _, anon_index)),
ty::BrAnon(br_index),
) => {
if debruijn_index == self.current_index && anon_index == br_index {
self.found_it = true;
return;
}
}

(Some(rl::Region::EarlyBound(_, id)), ty::BrNamed(def_id, _)) => {
debug!("EarlyBound id={:?} def_id={:?}", id, def_id);
if id == def_id {
Expand All @@ -239,7 +211,6 @@ impl<'tcx> Visitor<'tcx> for TyPathVisitor<'tcx> {
rl::Region::Static
| rl::Region::EarlyBound(_, _)
| rl::Region::LateBound(_, _, _)
| rl::Region::LateBoundAnon(_, _, _)
| rl::Region::Free(_, _),
)
| None,
Expand Down
1 change: 0 additions & 1 deletion compiler/rustc_middle/src/middle/resolve_lifetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ pub enum Region {
Static,
EarlyBound(/* index */ u32, /* lifetime decl */ DefId),
LateBound(ty::DebruijnIndex, /* late-bound index */ u32, /* lifetime decl */ DefId),
LateBoundAnon(ty::DebruijnIndex, /* late-bound index */ u32, /* anon index */ u32),
Free(DefId, /* lifetime decl */ DefId),
}

Expand Down
Loading

0 comments on commit c37ee1a

Please sign in to comment.