Skip to content

Commit 8492e25

Browse files
committed
Steal ResolverAstLowering when indexing AST.
1 parent a3be9ca commit 8492e25

16 files changed

Lines changed: 103 additions & 90 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::errors::{
2020
};
2121
use crate::{AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode};
2222

23-
impl<'hir> LoweringContext<'hir> {
23+
impl<'hir> LoweringContext<'_, 'hir> {
2424
pub(crate) fn lower_inline_asm(
2525
&mut self,
2626
sp: Span,

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use smallvec::SmallVec;
66

77
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
88

9-
impl<'hir> LoweringContext<'hir> {
9+
impl<'hir> LoweringContext<'_, 'hir> {
1010
pub(super) fn lower_block(
1111
&mut self,
1212
b: &Block,

compiler/rustc_ast_lowering/src/contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use thin_vec::thin_vec;
44

55
use crate::LoweringContext;
66

7-
impl<'hir> LoweringContext<'hir> {
7+
impl<'hir> LoweringContext<'_, 'hir> {
88
/// Lowered contracts are guarded with the `contract_checks` compiler flag,
99
/// i.e. the flag turns into a boolean guard in the lowered HIR. The reason
1010
/// for not eliminating the contract code entirely when the `contract_checks`

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
105105
},
106106
];
107107

108-
impl<'hir> LoweringContext<'hir> {
108+
impl<'hir> LoweringContext<'_, 'hir> {
109109
fn is_method(&self, def_id: DefId, span: Span) -> bool {
110110
match self.tcx.def_kind(def_id) {
111111
DefKind::Fn => false,
@@ -665,13 +665,13 @@ impl<'hir> LoweringContext<'hir> {
665665
}
666666
}
667667

668-
struct SelfResolver<'a, 'hir> {
669-
ctxt: &'a mut LoweringContext<'hir>,
668+
struct SelfResolver<'a, 'b, 'hir> {
669+
ctxt: &'a mut LoweringContext<'b, 'hir>,
670670
path_id: NodeId,
671671
self_param_id: NodeId,
672672
}
673673

674-
impl SelfResolver<'_, '_> {
674+
impl SelfResolver<'_, '_, '_> {
675675
fn try_replace_id(&mut self, id: NodeId) {
676676
if let Some(res) = self.ctxt.get_partial_res(id)
677677
&& let Some(Res::Local(sig_id)) = res.full_res()
@@ -682,7 +682,7 @@ impl SelfResolver<'_, '_> {
682682
}
683683
}
684684

685-
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
685+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
686686
fn visit_id(&mut self, id: NodeId) {
687687
self.try_replace_id(id);
688688
}

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ impl DelegationGenericsKind {
114114
impl<'hir> HirOrTyGenerics<'hir> {
115115
pub(super) fn into_hir_generics(
116116
&mut self,
117-
ctx: &mut LoweringContext<'hir>,
117+
ctx: &mut LoweringContext<'_, 'hir>,
118118
span: Span,
119119
) -> &mut HirOrTyGenerics<'hir> {
120120
if let HirOrTyGenerics::Ty(ty) = self {
@@ -140,7 +140,7 @@ impl<'hir> HirOrTyGenerics<'hir> {
140140

141141
pub(super) fn into_generic_args(
142142
&self,
143-
ctx: &mut LoweringContext<'hir>,
143+
ctx: &mut LoweringContext<'_, 'hir>,
144144
span: Span,
145145
) -> &'hir hir::GenericArgs<'hir> {
146146
match self {
@@ -174,7 +174,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
174174
pub(super) fn all_params(
175175
&mut self,
176176
span: Span,
177-
ctx: &mut LoweringContext<'hir>,
177+
ctx: &mut LoweringContext<'_, 'hir>,
178178
) -> impl Iterator<Item = hir::GenericParam<'hir>> {
179179
// Now we always call `into_hir_generics` both on child and parent,
180180
// however in future we would not do that, when scenarios like
@@ -208,7 +208,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
208208
pub(super) fn all_predicates(
209209
&mut self,
210210
span: Span,
211-
ctx: &mut LoweringContext<'hir>,
211+
ctx: &mut LoweringContext<'_, 'hir>,
212212
) -> impl Iterator<Item = hir::WherePredicate<'hir>> {
213213
// Now we always call `into_hir_generics` both on child and parent,
214214
// however in future we would not do that, when scenarios like
@@ -226,7 +226,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
226226
}
227227
}
228228

229-
impl<'hir> LoweringContext<'hir> {
229+
impl<'hir> LoweringContext<'_, 'hir> {
230230
pub(super) fn uplift_delegation_generics(
231231
&mut self,
232232
delegation: &Delegation,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5050
}
5151
}
5252

53-
impl<'hir> LoweringContext<'hir> {
53+
impl<'hir> LoweringContext<'_, 'hir> {
5454
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5555
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5656
}
@@ -1230,7 +1230,7 @@ impl<'hir> LoweringContext<'hir> {
12301230
whole_span: Span,
12311231
) -> hir::ExprKind<'hir> {
12321232
// Return early in case of an ordinary assignment.
1233-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
1233+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
12341234
match &lhs.kind {
12351235
ExprKind::Array(..)
12361236
| ExprKind::Struct(..)

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_span::{ByteSymbol, DesugaringKind, Ident, Span, Symbol, sym};
88

99
use super::LoweringContext;
1010

11-
impl<'hir> LoweringContext<'hir> {
11+
impl<'hir> LoweringContext<'_, 'hir> {
1212
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
1313
// Never call the const constructor of `fmt::Arguments` if the
1414
// format_args!() had any arguments _before_ flattening/inlining.
@@ -230,7 +230,7 @@ enum ArgumentType {
230230
/// <core::fmt::Argument>::new_…(arg)
231231
/// ```
232232
fn make_argument<'hir>(
233-
ctx: &mut LoweringContext<'hir>,
233+
ctx: &mut LoweringContext<'_, 'hir>,
234234
sp: Span,
235235
arg: &'hir hir::Expr<'hir>,
236236
ty: ArgumentType,
@@ -277,7 +277,7 @@ fn make_count(
277277
}
278278

279279
fn expand_format_args<'hir>(
280-
ctx: &mut LoweringContext<'hir>,
280+
ctx: &mut LoweringContext<'_, 'hir>,
281281
macsp: Span,
282282
fmt: &FormatArgs,
283283
allow_const: bool,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ use super::{
2424
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
2525
};
2626

27-
pub(super) struct ItemLowerer<'hir> {
27+
pub(super) struct ItemLowerer<'a, 'hir> {
2828
pub(super) tcx: TyCtxt<'hir>,
29-
pub(super) resolver: &'hir ResolverAstLowering<'hir>,
30-
pub(super) next_node_id: NodeId,
29+
pub(super) resolver: &'a ResolverAstLowering<'hir>,
3130
}
3231

3332
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -49,13 +48,13 @@ fn add_ty_alias_where_clause(
4948
if before.0 || !after.0 { before } else { after };
5049
}
5150

52-
impl<'hir> ItemLowerer<'hir> {
51+
impl<'hir> ItemLowerer<'_, 'hir> {
5352
fn with_lctx(
5453
&mut self,
5554
owner: NodeId,
56-
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
55+
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
5756
) -> hir::MaybeOwner<'hir> {
58-
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner, self.next_node_id);
57+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
5958

6059
let item = f(&mut lctx);
6160
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
@@ -93,7 +92,7 @@ impl<'hir> ItemLowerer<'hir> {
9392
}
9493
}
9594

96-
impl<'hir> LoweringContext<'hir> {
95+
impl<'hir> LoweringContext<'_, 'hir> {
9796
pub(super) fn lower_mod(
9897
&mut self,
9998
items: &[Box<Item>],
@@ -1462,7 +1461,7 @@ impl<'hir> LoweringContext<'hir> {
14621461
pub(crate) fn lower_coroutine_body_with_moved_arguments(
14631462
&mut self,
14641463
decl: &FnDecl,
1465-
lower_body: impl FnOnce(&mut LoweringContext<'hir>) -> hir::Expr<'hir>,
1464+
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
14661465
fn_decl_span: Span,
14671466
body_span: Span,
14681467
coroutine_kind: CoroutineKind,
@@ -1599,7 +1598,7 @@ impl<'hir> LoweringContext<'hir> {
15991598
parameters.push(new_parameter);
16001599
}
16011600

1602-
let mkbody = |this: &mut LoweringContext<'hir>| {
1601+
let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
16031602
// Create a block from the user's function body:
16041603
let user_body = lower_body(this);
16051604

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 53 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ mod pat;
8989
mod path;
9090
pub mod stability;
9191

92-
struct LoweringContext<'hir> {
92+
struct LoweringContext<'a, 'hir> {
9393
tcx: TyCtxt<'hir>,
94-
resolver: &'hir ResolverAstLowering<'hir>,
94+
resolver: &'a ResolverAstLowering<'hir>,
9595
current_disambiguator: PerParentDisambiguatorState,
9696

9797
/// Used to allocate HIR nodes.
@@ -160,13 +160,8 @@ struct LoweringContext<'hir> {
160160
attribute_parser: AttributeParser<'hir>,
161161
}
162162

163-
impl<'hir> LoweringContext<'hir> {
164-
fn new(
165-
tcx: TyCtxt<'hir>,
166-
resolver: &'hir ResolverAstLowering<'hir>,
167-
owner: NodeId,
168-
next_node_id: NodeId,
169-
) -> Self {
163+
impl<'a, 'hir> LoweringContext<'a, 'hir> {
164+
fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>, owner: NodeId) -> Self {
170165
let registered_tools = tcx.registered_tools(()).iter().map(|x| x.name).collect();
171166
let current_hir_id_owner = hir::OwnerId { def_id: resolver.node_id_to_def_id[&owner] };
172167
let current_disambiguator = resolver
@@ -193,7 +188,7 @@ impl<'hir> LoweringContext<'hir> {
193188
#[cfg(debug_assertions)]
194189
node_id_to_local_id: [(owner, hir::ItemLocalId::ZERO)].into_iter().collect(),
195190
trait_map: Default::default(),
196-
next_node_id,
191+
next_node_id: resolver.next_node_id,
197192
node_id_to_def_id: NodeMap::default(),
198193
partial_res_overrides: NodeMap::default(),
199194

@@ -457,14 +452,15 @@ enum TryBlockScope {
457452
pub fn index_ast<'tcx>(
458453
tcx: TyCtxt<'tcx>,
459454
(): (),
460-
) -> (IndexVec<LocalDefId, Steal<AstOwner>>, NodeId) {
455+
) -> IndexVec<LocalDefId, Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>> {
461456
// Queries that borrow `resolver_for_lowering`.
462457
tcx.ensure_done().output_filenames(());
463458
tcx.ensure_done().early_lint_checks(());
464459
tcx.ensure_done().get_lang_items(());
465460
tcx.ensure_done().debugger_visualizers(LOCAL_CRATE);
466461

467462
let (resolver, krate) = tcx.resolver_for_lowering();
463+
let mut resolver = resolver.steal();
468464
let mut krate = krate.steal();
469465

470466
let mut indexer = Indexer {
@@ -474,19 +470,24 @@ pub fn index_ast<'tcx>(
474470
};
475471
indexer.visit_crate(&mut krate);
476472
indexer.insert(CRATE_NODE_ID, AstOwner::Crate(Box::new(krate)));
477-
return (indexer.index, indexer.next_node_id);
473+
resolver.next_node_id = indexer.next_node_id;
474+
475+
let index = indexer.index;
476+
let resolver = Arc::new(resolver);
477+
let index = index.into_iter().map(|owner| Steal::new((resolver.clone(), owner))).collect();
478+
return index;
478479

479480
struct Indexer<'s> {
480481
node_id_to_def_id: &'s NodeMap<LocalDefId>,
481-
index: IndexVec<LocalDefId, Steal<AstOwner>>,
482+
index: IndexVec<LocalDefId, AstOwner>,
482483
next_node_id: NodeId,
483484
}
484485

485486
impl Indexer<'_> {
486487
fn insert(&mut self, id: NodeId, node: AstOwner) {
487488
let def_id = self.node_id_to_def_id[&id];
488-
self.index.ensure_contains_elem(def_id, || Steal::new(AstOwner::NonOwner));
489-
self.index[def_id] = Steal::new(node);
489+
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
490+
self.index[def_id] = node;
490491
}
491492

492493
fn make_dummy<K>(
@@ -611,43 +612,46 @@ pub fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_>
611612
tcx.ensure_done().early_lint_checks(());
612613
tcx.ensure_done().debugger_visualizers(LOCAL_CRATE);
613614
tcx.ensure_done().get_lang_items(());
614-
let (resolver, _) = tcx.resolver_for_lowering();
615-
let (ast_index, next_node_id) = tcx.index_ast(());
616-
let node = ast_index.get(def_id).map(Steal::steal);
617-
618-
let mut item_lowerer = item::ItemLowerer { tcx, resolver, next_node_id: *next_node_id };
619-
620-
// The item existed in the AST.
621-
let parent_id = match node.as_ref() {
622-
Some(AstOwner::Crate(c)) => return item_lowerer.lower_crate(&c),
623-
Some(AstOwner::Item(item)) => return item_lowerer.lower_item(&item),
624-
Some(AstOwner::TraitItem(item)) => {
625-
return item_lowerer.lower_trait_item(&item);
626-
}
627-
Some(AstOwner::ImplItem(item)) => {
628-
return item_lowerer.lower_impl_item(&item);
615+
let ast_index = tcx.index_ast(());
616+
let resolver_and_node = ast_index.get(def_id).map(Steal::steal);
617+
618+
let fallback_to_parent = |parent_id| {
619+
// The item did not exist in the AST, it was created by its parent.
620+
let mut parent_info = tcx.lower_to_hir(parent_id);
621+
if let hir::MaybeOwner::NonOwner(hir_id) = parent_info {
622+
parent_info = tcx.lower_to_hir(hir_id.owner);
629623
}
630-
Some(AstOwner::ForeignItem(item)) => return item_lowerer.lower_foreign_item(&item),
631-
Some(AstOwner::Synthetic(parent_id)) => *parent_id,
632-
Some(AstOwner::NonOwner) | None => tcx.local_parent(def_id),
624+
625+
let parent_info = parent_info.unwrap();
626+
*parent_info.children.get(&def_id).unwrap_or_else(|| {
627+
panic!(
628+
"{:?} does not appear in children of {:?}",
629+
def_id,
630+
parent_info.nodes.node().def_id()
631+
)
632+
})
633633
};
634634

635-
tcx.sess.time("drop_ast", || std::mem::drop(node));
635+
let Some((resolver, node)) = resolver_and_node else {
636+
return fallback_to_parent(tcx.local_parent(def_id));
637+
};
636638

637-
// The item did not exist in the AST, it was created by its parent.
638-
let mut parent_info = tcx.lower_to_hir(parent_id);
639-
if let hir::MaybeOwner::NonOwner(hir_id) = parent_info {
640-
parent_info = tcx.lower_to_hir(hir_id.owner);
641-
}
639+
let mut item_lowerer = item::ItemLowerer { tcx, resolver: &*resolver };
640+
641+
let item = match &node {
642+
// The item existed in the AST.
643+
AstOwner::Crate(c) => item_lowerer.lower_crate(&c),
644+
AstOwner::Item(item) => item_lowerer.lower_item(&item),
645+
AstOwner::TraitItem(item) => item_lowerer.lower_trait_item(&item),
646+
AstOwner::ImplItem(item) => item_lowerer.lower_impl_item(&item),
647+
AstOwner::ForeignItem(item) => item_lowerer.lower_foreign_item(&item),
648+
AstOwner::Synthetic(parent_id) => fallback_to_parent(*parent_id),
649+
AstOwner::NonOwner => fallback_to_parent(tcx.local_parent(def_id)),
650+
};
642651

643-
let parent_info = parent_info.unwrap();
644-
*parent_info.children.get(&def_id).unwrap_or_else(|| {
645-
panic!(
646-
"{:?} does not appear in children of {:?}",
647-
def_id,
648-
parent_info.nodes.node().def_id()
649-
)
650-
})
652+
tcx.sess.time("drop_ast", || std::mem::drop(node));
653+
654+
item
651655
}
652656

653657
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -677,7 +681,7 @@ enum GenericArgsMode {
677681
Silence,
678682
}
679683

680-
impl<'hir> LoweringContext<'hir> {
684+
impl<'hir> LoweringContext<'_, 'hir> {
681685
fn create_def(
682686
&mut self,
683687
node_id: NodeId,
@@ -3109,7 +3113,7 @@ impl<'hir> GenericArgsCtor<'hir> {
31093113
&& self.parenthesized == hir::GenericArgsParentheses::No
31103114
}
31113115

3112-
fn into_generic_args(self, this: &LoweringContext<'hir>) -> &'hir hir::GenericArgs<'hir> {
3116+
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
31133117
let ga = hir::GenericArgs {
31143118
args: this.arena.alloc_from_iter(self.args),
31153119
constraints: self.constraints,

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use super::{
1414
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
1515
};
1616

17-
impl<'hir> LoweringContext<'hir> {
17+
impl<'hir> LoweringContext<'_, 'hir> {
1818
pub(crate) fn lower_pat(&mut self, pattern: &Pat) -> &'hir hir::Pat<'hir> {
1919
self.arena.alloc(self.lower_pat_mut(pattern))
2020
}

0 commit comments

Comments
 (0)