Skip to content

Commit ee1d5fe

Browse files
committed
Use LifetimeRes during lowering.
1 parent a37adbb commit ee1d5fe

File tree

9 files changed

+383
-518
lines changed

9 files changed

+383
-518
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3595,6 +3595,7 @@ dependencies = [
35953595
name = "rustc_ast_lowering"
35963596
version = "0.0.0"
35973597
dependencies = [
3598+
"indexmap",
35983599
"rustc_arena",
35993600
"rustc_ast",
36003601
"rustc_ast_pretty",

compiler/rustc_ast_lowering/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ doctest = false
88

99
[dependencies]
1010
rustc_arena = { path = "../rustc_arena" }
11+
indexmap = "1.5.1"
1112
tracing = "0.1"
1213
rustc_ast_pretty = { path = "../rustc_ast_pretty" }
1314
rustc_hir = { path = "../rustc_hir" }

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
5252
e.span,
5353
seg,
5454
ParamMode::Optional,
55-
0,
5655
ParenthesizedGenericArgs::Err,
5756
ImplTraitContext::disallowed(),
5857
));

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(super) struct NodeCollector<'a, 'hir> {
3030
definitions: &'a definitions::Definitions,
3131
}
3232

33+
#[tracing::instrument(level = "debug", skip(sess, definitions, bodies))]
3334
pub(super) fn index_hir<'hir>(
3435
sess: &Session,
3536
definitions: &definitions::Definitions,
@@ -63,6 +64,7 @@ pub(super) fn index_hir<'hir>(
6364
}
6465

6566
impl<'a, 'hir> NodeCollector<'a, 'hir> {
67+
#[tracing::instrument(level = "debug", skip(self))]
6668
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
6769
debug_assert_eq!(self.owner, hir_id.owner);
6870
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
@@ -142,8 +144,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
142144
});
143145
}
144146

147+
#[tracing::instrument(level = "debug", skip(self))]
145148
fn visit_item(&mut self, i: &'hir Item<'hir>) {
146-
debug!("visit_item: {:?}", i);
147149
debug_assert_eq!(i.def_id, self.owner);
148150
self.with_parent(i.hir_id(), |this| {
149151
if let ItemKind::Struct(ref struct_def, _) = i.kind {
@@ -156,6 +158,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
156158
});
157159
}
158160

161+
#[tracing::instrument(level = "debug", skip(self))]
159162
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
160163
debug_assert_eq!(fi.def_id, self.owner);
161164
self.with_parent(fi.hir_id(), |this| {
@@ -174,13 +177,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
174177
})
175178
}
176179

180+
#[tracing::instrument(level = "debug", skip(self))]
177181
fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) {
178182
debug_assert_eq!(ti.def_id, self.owner);
179183
self.with_parent(ti.hir_id(), |this| {
180184
intravisit::walk_trait_item(this, ti);
181185
});
182186
}
183187

188+
#[tracing::instrument(level = "debug", skip(self))]
184189
fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) {
185190
debug_assert_eq!(ii.def_id, self.owner);
186191
self.with_parent(ii.hir_id(), |this| {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 24 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_data_structures::fx::FxHashSet;
99
use rustc_errors::struct_span_err;
1010
use rustc_hir as hir;
1111
use rustc_hir::def::{DefKind, Res};
12-
use rustc_hir::def_id::LocalDefId;
1312
use rustc_index::vec::Idx;
1413
use rustc_span::source_map::{respan, DesugaringKind};
1514
use rustc_span::symbol::{kw, sym, Ident};
@@ -19,7 +18,6 @@ use smallvec::{smallvec, SmallVec};
1918
use tracing::debug;
2019

2120
use std::iter;
22-
use std::mem;
2321

2422
pub(super) struct ItemLowerer<'a, 'lowering, 'hir> {
2523
pub(super) lctx: &'a mut LoweringContext<'lowering, 'hir>,
@@ -46,20 +44,14 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
4644
}
4745

4846
fn visit_item(&mut self, item: &'a Item) {
49-
let hir_id = self.lctx.with_hir_id_owner(item.id, |lctx| {
50-
let node = lctx.without_in_scope_lifetime_defs(|lctx| lctx.lower_item(item));
51-
hir::OwnerNode::Item(node)
52-
});
47+
self.lctx.with_hir_id_owner(item.id, |lctx| hir::OwnerNode::Item(lctx.lower_item(item)));
5348

54-
self.lctx.with_parent_item_lifetime_defs(hir_id, |this| {
55-
let this = &mut ItemLowerer { lctx: this };
56-
match item.kind {
57-
ItemKind::Impl(box Impl { ref of_trait, .. }) => {
58-
this.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
59-
}
60-
_ => visit::walk_item(this, item),
49+
match item.kind {
50+
ItemKind::Impl(box Impl { ref of_trait, .. }) => {
51+
self.with_trait_impl_ref(of_trait, |this| visit::walk_item(this, item));
6152
}
62-
});
53+
_ => visit::walk_item(self, item),
54+
}
6355
}
6456

6557
fn visit_fn(&mut self, fk: FnKind<'a>, sp: Span, _: NodeId) {
@@ -94,55 +86,6 @@ impl<'a> Visitor<'a> for ItemLowerer<'a, '_, '_> {
9486
}
9587

9688
impl<'hir> LoweringContext<'_, 'hir> {
97-
// Same as the method above, but accepts `hir::GenericParam`s
98-
// instead of `ast::GenericParam`s.
99-
// This should only be used with generics that have already had their
100-
// in-band lifetimes added. In practice, this means that this function is
101-
// only used when lowering a child item of a trait or impl.
102-
fn with_parent_item_lifetime_defs<T>(
103-
&mut self,
104-
parent_hir_id: LocalDefId,
105-
f: impl FnOnce(&mut Self) -> T,
106-
) -> T {
107-
let old_len = self.in_scope_lifetimes.len();
108-
109-
let parent_generics =
110-
match self.owners[parent_hir_id].as_ref().unwrap().node().expect_item().kind {
111-
hir::ItemKind::Impl(hir::Impl { ref generics, .. })
112-
| hir::ItemKind::Trait(_, _, ref generics, ..) => generics.params,
113-
_ => &[],
114-
};
115-
let lt_def_names = parent_generics.iter().filter_map(|param| match param.kind {
116-
hir::GenericParamKind::Lifetime { .. } => Some(param.name.normalize_to_macros_2_0()),
117-
_ => None,
118-
});
119-
self.in_scope_lifetimes.extend(lt_def_names);
120-
121-
let res = f(self);
122-
123-
self.in_scope_lifetimes.truncate(old_len);
124-
res
125-
}
126-
127-
// Clears (and restores) the `in_scope_lifetimes` field. Used when
128-
// visiting nested items, which never inherit in-scope lifetimes
129-
// from their surrounding environment.
130-
fn without_in_scope_lifetime_defs<T>(&mut self, f: impl FnOnce(&mut Self) -> T) -> T {
131-
let old_in_scope_lifetimes = mem::replace(&mut self.in_scope_lifetimes, vec![]);
132-
133-
// this vector is only used when walking over impl headers,
134-
// input types, and the like, and should not be non-empty in
135-
// between items
136-
assert!(self.lifetimes_to_define.is_empty());
137-
138-
let res = f(self);
139-
140-
assert!(self.in_scope_lifetimes.is_empty());
141-
self.in_scope_lifetimes = old_in_scope_lifetimes;
142-
143-
res
144-
}
145-
14689
pub(super) fn lower_mod(&mut self, items: &[P<Item>], inner: Span) -> hir::Mod<'hir> {
14790
hir::Mod {
14891
inner: self.lower_span(inner),
@@ -248,7 +191,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
248191
AnonymousLifetimeMode::PassThrough,
249192
|this, idty| {
250193
let ret_id = asyncness.opt_return_id();
251-
this.lower_fn_decl(&decl, Some((fn_def_id, idty)), true, ret_id)
194+
this.lower_fn_decl(&decl, Some((id, idty)), true, ret_id)
252195
},
253196
);
254197
let sig = hir::FnSig {
@@ -358,12 +301,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
358301
},
359302
);
360303

361-
let new_impl_items =
362-
self.with_in_scope_lifetime_defs(&ast_generics.params, |this| {
363-
this.arena.alloc_from_iter(
364-
impl_items.iter().map(|item| this.lower_impl_item_ref(item)),
365-
)
366-
});
304+
let new_impl_items = self
305+
.arena
306+
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
367307

368308
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
369309
// to not cause an assertion failure inside the `lower_defaultness` function.
@@ -775,21 +715,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
775715
}
776716
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
777717
let names = self.lower_fn_params_to_names(&sig.decl);
778-
let (generics, sig) =
779-
self.lower_method_sig(generics, sig, trait_item_def_id, false, None);
718+
let (generics, sig) = self.lower_method_sig(generics, sig, i.id, false, None);
780719
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
781720
}
782721
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
783722
let asyncness = sig.header.asyncness;
784723
let body_id =
785724
self.lower_maybe_async_body(i.span, &sig.decl, asyncness, Some(&body));
786-
let (generics, sig) = self.lower_method_sig(
787-
generics,
788-
sig,
789-
trait_item_def_id,
790-
false,
791-
asyncness.opt_return_id(),
792-
);
725+
let (generics, sig) =
726+
self.lower_method_sig(generics, sig, i.id, false, asyncness.opt_return_id());
793727
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
794728
}
795729
AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. }) => {
@@ -844,8 +778,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
844778
}
845779

846780
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
847-
let impl_item_def_id = self.resolver.local_def_id(i.id);
848-
849781
let (generics, kind) = match &i.kind {
850782
AssocItemKind::Const(_, ty, expr) => {
851783
let ty = self.lower_ty(ty, ImplTraitContext::disallowed());
@@ -863,7 +795,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
863795
let (generics, sig) = self.lower_method_sig(
864796
generics,
865797
sig,
866-
impl_item_def_id,
798+
i.id,
867799
impl_trait_return_allow,
868800
asyncness.opt_return_id(),
869801
);
@@ -1248,22 +1180,18 @@ impl<'hir> LoweringContext<'_, 'hir> {
12481180
&mut self,
12491181
generics: &Generics,
12501182
sig: &FnSig,
1251-
fn_def_id: LocalDefId,
1183+
id: NodeId,
12521184
impl_trait_return_allow: bool,
12531185
is_async: Option<NodeId>,
12541186
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1187+
let fn_def_id = self.resolver.local_def_id(id);
12551188
let header = self.lower_fn_header(sig.header);
12561189
let (generics, decl) = self.add_in_band_defs(
12571190
generics,
12581191
fn_def_id,
12591192
AnonymousLifetimeMode::PassThrough,
12601193
|this, idty| {
1261-
this.lower_fn_decl(
1262-
&sig.decl,
1263-
Some((fn_def_id, idty)),
1264-
impl_trait_return_allow,
1265-
is_async,
1266-
)
1194+
this.lower_fn_decl(&sig.decl, Some((id, idty)), impl_trait_return_allow, is_async)
12671195
},
12681196
);
12691197
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
@@ -1408,17 +1336,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
14081336
ref bounded_ty,
14091337
ref bounds,
14101338
span,
1411-
}) => self.with_in_scope_lifetime_defs(&bound_generic_params, |this| {
1339+
}) => {
14121340
hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate {
1413-
bound_generic_params: this
1341+
bound_generic_params: self
14141342
.lower_generic_params(bound_generic_params, ImplTraitContext::disallowed()),
1415-
bounded_ty: this.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
1416-
bounds: this.arena.alloc_from_iter(bounds.iter().map(|bound| {
1417-
this.lower_param_bound(bound, ImplTraitContext::disallowed())
1343+
bounded_ty: self.lower_ty(bounded_ty, ImplTraitContext::disallowed()),
1344+
bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| {
1345+
self.lower_param_bound(bound, ImplTraitContext::disallowed())
14181346
})),
1419-
span: this.lower_span(span),
1347+
span: self.lower_span(span),
14201348
})
1421-
}),
1349+
}
14221350
WherePredicate::RegionPredicate(WhereRegionPredicate {
14231351
ref lifetime,
14241352
ref bounds,

0 commit comments

Comments
 (0)