Skip to content

Commit f385f85

Browse files
committed
Use LifetimeRes during lowering.
1 parent 69985f0 commit f385f85

File tree

9 files changed

+708
-798
lines changed

9 files changed

+708
-798
lines changed

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
5353
e.span,
5454
seg,
5555
ParamMode::Optional,
56-
0,
5756
ParenthesizedGenericArgs::Err,
5857
ImplTraitContext::Disallowed(ImplTraitPosition::Path),
5958
));

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,
@@ -65,6 +66,7 @@ pub(super) fn index_hir<'hir>(
6566
}
6667

6768
impl<'a, 'hir> NodeCollector<'a, 'hir> {
69+
#[tracing::instrument(level = "debug", skip(self))]
6870
fn insert(&mut self, span: Span, hir_id: HirId, node: Node<'hir>) {
6971
debug_assert_eq!(self.owner, hir_id.owner);
7072
debug_assert_ne!(hir_id.local_id.as_u32(), 0);
@@ -138,8 +140,8 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
138140
});
139141
}
140142

143+
#[tracing::instrument(level = "debug", skip(self))]
141144
fn visit_item(&mut self, i: &'hir Item<'hir>) {
142-
debug!("visit_item: {:?}", i);
143145
debug_assert_eq!(i.def_id, self.owner);
144146
self.with_parent(i.hir_id(), |this| {
145147
if let ItemKind::Struct(ref struct_def, _) = i.kind {
@@ -152,6 +154,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
152154
});
153155
}
154156

157+
#[tracing::instrument(level = "debug", skip(self))]
155158
fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) {
156159
debug_assert_eq!(fi.def_id, self.owner);
157160
self.with_parent(fi.hir_id(), |this| {
@@ -170,13 +173,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
170173
})
171174
}
172175

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

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

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 42 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
use super::{AnonymousLifetimeMode, LoweringContext, ParamMode};
21
use super::{AstOwner, ImplTraitContext, ImplTraitPosition, ResolverAstLowering};
2+
use super::{LoweringContext, ParamMode};
33
use crate::{Arena, FnDeclKind};
44

55
use rustc_ast::ptr::P;
@@ -81,13 +81,13 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
8181
is_in_loop_condition: false,
8282
is_in_trait_impl: false,
8383
is_in_dyn_type: false,
84-
anonymous_lifetime_mode: AnonymousLifetimeMode::PassThrough,
8584
generator_kind: None,
8685
task_context: None,
8786
current_item: None,
88-
lifetimes_to_define: Vec::new(),
87+
lifetimes_to_define: Default::default(),
8988
is_collecting_anonymous_lifetimes: None,
9089
in_scope_lifetimes: Vec::new(),
90+
captured_lifetimes: None,
9191
allow_try_trait: Some([sym::try_trait_v2][..].into()),
9292
allow_gen_future: Some([sym::gen_future][..].into()),
9393
allow_into_future: Some([sym::into_future][..].into()),
@@ -143,12 +143,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
143143
LocalDefId { local_def_index }
144144
};
145145

146-
let parent_hir = self.lower_node(parent_id).unwrap().node().expect_item();
146+
let parent_hir = self.lower_node(parent_id).unwrap();
147147
self.with_lctx(item.id, |lctx| {
148148
// Evaluate with the lifetimes in `params` in-scope.
149149
// This is used to track which lifetimes have already been defined,
150150
// and which need to be replicated when lowering an async fn.
151-
match parent_hir.kind {
151+
match parent_hir.node().expect_item().kind {
152152
hir::ItemKind::Impl(hir::Impl { ref of_trait, ref generics, .. }) => {
153153
lctx.is_in_trait_impl = of_trait.is_some();
154154
lctx.in_scope_lifetimes = generics
@@ -157,7 +157,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
157157
.filter(|param| {
158158
matches!(param.kind, hir::GenericParamKind::Lifetime { .. })
159159
})
160-
.map(|param| param.name)
160+
.map(|param| {
161+
let def_id =
162+
parent_hir.nodes.local_id_to_def_id[&param.hir_id.local_id];
163+
let name = param.name;
164+
(name, def_id)
165+
})
161166
.collect();
162167
}
163168
hir::ItemKind::Trait(_, _, ref generics, ..) => {
@@ -167,7 +172,12 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
167172
.filter(|param| {
168173
matches!(param.kind, hir::GenericParamKind::Lifetime { .. })
169174
})
170-
.map(|param| param.name)
175+
.map(|param| {
176+
let def_id =
177+
parent_hir.nodes.local_id_to_def_id[&param.hir_id.local_id];
178+
let name = param.name;
179+
(name, def_id)
180+
})
171181
.collect();
172182
}
173183
_ => {}
@@ -288,20 +298,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
288298
let body_id =
289299
this.lower_maybe_async_body(span, &decl, asyncness, body.as_deref());
290300

291-
let (generics, decl) = this.add_in_band_defs(
292-
generics,
293-
fn_def_id,
294-
AnonymousLifetimeMode::PassThrough,
295-
|this, idty| {
301+
let (generics, decl) =
302+
this.add_in_band_defs(generics, fn_def_id, |this, idty| {
296303
let ret_id = asyncness.opt_return_id();
297-
this.lower_fn_decl(
298-
&decl,
299-
Some((fn_def_id, idty)),
300-
FnDeclKind::Fn,
301-
ret_id,
302-
)
303-
},
304-
);
304+
this.lower_fn_decl(&decl, Some((id, idty)), FnDeclKind::Fn, ret_id)
305+
});
305306
let sig = hir::FnSig {
306307
decl,
307308
header: this.lower_fn_header(header),
@@ -420,11 +421,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
420421
// lifetime to be added, but rather a reference to a
421422
// parent lifetime.
422423
let lowered_trait_def_id = hir_id.expect_owner();
423-
let (generics, (trait_ref, lowered_ty)) = self.add_in_band_defs(
424-
ast_generics,
425-
lowered_trait_def_id,
426-
AnonymousLifetimeMode::CreateParameter,
427-
|this, _| {
424+
let (generics, (trait_ref, lowered_ty)) =
425+
self.add_in_band_defs(ast_generics, lowered_trait_def_id, |this, _| {
428426
let trait_ref = trait_ref.as_ref().map(|trait_ref| {
429427
this.lower_trait_ref(
430428
trait_ref,
@@ -436,8 +434,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
436434
.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
437435

438436
(trait_ref, lowered_ty)
439-
},
440-
);
437+
});
441438

442439
let new_impl_items =
443440
self.with_in_scope_lifetime_defs(&ast_generics.params, |this| {
@@ -750,18 +747,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
750747
kind: match i.kind {
751748
ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => {
752749
let fdec = &sig.decl;
753-
let (generics, (fn_dec, fn_args)) = self.add_in_band_defs(
754-
generics,
755-
def_id,
756-
AnonymousLifetimeMode::PassThrough,
757-
|this, _| {
750+
let (generics, (fn_dec, fn_args)) =
751+
self.add_in_band_defs(generics, def_id, |this, _| {
758752
(
759753
// Disallow `impl Trait` in foreign items.
760754
this.lower_fn_decl(fdec, None, FnDeclKind::ExternFn, None),
761755
this.lower_fn_params_to_names(fdec),
762756
)
763-
},
764-
);
757+
});
765758

766759
hir::ForeignItemKind::Fn(fn_dec, fn_args, generics)
767760
}
@@ -868,13 +861,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
868861
}
869862
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
870863
let names = self.lower_fn_params_to_names(&sig.decl);
871-
let (generics, sig) = self.lower_method_sig(
872-
generics,
873-
sig,
874-
trait_item_def_id,
875-
FnDeclKind::Trait,
876-
None,
877-
);
864+
let (generics, sig) =
865+
self.lower_method_sig(generics, sig, i.id, FnDeclKind::Trait, None);
878866
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
879867
}
880868
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
@@ -884,7 +872,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
884872
let (generics, sig) = self.lower_method_sig(
885873
generics,
886874
sig,
887-
trait_item_def_id,
875+
i.id,
888876
FnDeclKind::Trait,
889877
asyncness.opt_return_id(),
890878
);
@@ -958,8 +946,6 @@ impl<'hir> LoweringContext<'_, 'hir> {
958946
}
959947

960948
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
961-
let impl_item_def_id = self.resolver.local_def_id(i.id);
962-
963949
let (generics, kind) = match &i.kind {
964950
AssocItemKind::Const(_, ty, expr) => {
965951
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
@@ -976,7 +962,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
976962
let (generics, sig) = self.lower_method_sig(
977963
generics,
978964
sig,
979-
impl_item_def_id,
965+
i.id,
980966
if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
981967
asyncness.opt_return_id(),
982968
);
@@ -1363,17 +1349,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
13631349
&mut self,
13641350
generics: &Generics,
13651351
sig: &FnSig,
1366-
fn_def_id: LocalDefId,
1352+
id: NodeId,
13671353
kind: FnDeclKind,
13681354
is_async: Option<NodeId>,
13691355
) -> (hir::Generics<'hir>, hir::FnSig<'hir>) {
1356+
let fn_def_id = self.resolver.local_def_id(id);
13701357
let header = self.lower_fn_header(sig.header);
1371-
let (generics, decl) = self.add_in_band_defs(
1372-
generics,
1373-
fn_def_id,
1374-
AnonymousLifetimeMode::PassThrough,
1375-
|this, idty| this.lower_fn_decl(&sig.decl, Some((fn_def_id, idty)), kind, is_async),
1376-
);
1358+
let (generics, decl) = self.add_in_band_defs(generics, fn_def_id, |this, idty| {
1359+
this.lower_fn_decl(&sig.decl, Some((id, idty)), kind, is_async)
1360+
});
13771361
(generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) })
13781362
}
13791363

@@ -1498,14 +1482,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
14981482
}
14991483

15001484
fn lower_where_clause(&mut self, wc: &WhereClause) -> hir::WhereClause<'hir> {
1501-
self.with_anonymous_lifetime_mode(AnonymousLifetimeMode::ReportError, |this| {
1502-
hir::WhereClause {
1503-
predicates: this.arena.alloc_from_iter(
1504-
wc.predicates.iter().map(|predicate| this.lower_where_predicate(predicate)),
1505-
),
1506-
span: this.lower_span(wc.span),
1507-
}
1508-
})
1485+
hir::WhereClause {
1486+
predicates: self.arena.alloc_from_iter(
1487+
wc.predicates.iter().map(|predicate| self.lower_where_predicate(predicate)),
1488+
),
1489+
span: self.lower_span(wc.span),
1490+
}
15091491
}
15101492

15111493
fn lower_where_predicate(&mut self, pred: &WherePredicate) -> hir::WherePredicate<'hir> {

0 commit comments

Comments
 (0)