Skip to content

Commit 0e9f931

Browse files
committed
Steal ResolverAstLowering when indexing AST.
1 parent 5464994 commit 0e9f931

17 files changed

Lines changed: 108 additions & 92 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

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

24-
impl<'hir> LoweringContext<'hir> {
24+
impl<'hir> LoweringContext<'_, 'hir> {
2525
pub(crate) fn lower_inline_asm(
2626
&mut self,
2727
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
@@ -120,7 +120,7 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
120120
},
121121
];
122122

123-
impl<'hir> LoweringContext<'hir> {
123+
impl<'hir> LoweringContext<'_, 'hir> {
124124
fn is_method(&self, def_id: DefId, span: Span) -> bool {
125125
match self.tcx.def_kind(def_id) {
126126
DefKind::Fn => false,
@@ -748,13 +748,13 @@ impl<'hir> LoweringContext<'hir> {
748748
}
749749
}
750750

751-
struct SelfResolver<'a, 'hir> {
752-
ctxt: &'a mut LoweringContext<'hir>,
751+
struct SelfResolver<'a, 'b, 'hir> {
752+
ctxt: &'a mut LoweringContext<'b, 'hir>,
753753
path_id: NodeId,
754754
self_param_id: NodeId,
755755
}
756756

757-
impl SelfResolver<'_, '_> {
757+
impl SelfResolver<'_, '_, '_> {
758758
fn try_replace_id(&mut self, id: NodeId) {
759759
if let Some(res) = self.ctxt.get_partial_res(id)
760760
&& let Some(Res::Local(sig_id)) = res.full_res()
@@ -765,7 +765,7 @@ impl SelfResolver<'_, '_> {
765765
}
766766
}
767767

768-
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
768+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
769769
fn visit_id(&mut self, id: NodeId) {
770770
self.try_replace_id(id);
771771
}

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 3 additions & 3 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 {
@@ -203,7 +203,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
203203
}
204204
}
205205

206-
impl<'hir> LoweringContext<'hir> {
206+
impl<'hir> LoweringContext<'_, 'hir> {
207207
pub(super) fn uplift_delegation_generics(
208208
&mut self,
209209
delegation: &Delegation,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
115115
}
116116
}
117117

118-
impl<'hir> LoweringContext<'hir> {
118+
impl<'hir> LoweringContext<'_, 'hir> {
119119
fn with_move_expr_bindings<T>(
120120
&mut self,
121121
state: Option<MoveExprState<'hir>>,
@@ -1171,7 +1171,7 @@ impl<'hir> LoweringContext<'hir> {
11711171
whole_span: Span,
11721172
) -> hir::ExprKind<'hir> {
11731173
// Return early in case of an ordinary assignment.
1174-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
1174+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
11751175
match &lhs.kind {
11761176
ExprKind::Array(..)
11771177
| ExprKind::Struct(..)

compiler/rustc_ast_lowering/src/expr/closure.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::{LoweringContext, MoveExprInitializerFinder, MoveExprState};
99
use crate::FnDeclKind;
1010
use crate::diagnostics::{ClosureCannotBeStatic, CoroutineTooManyParameters};
1111

12-
impl<'hir> LoweringContext<'hir> {
12+
impl<'hir> LoweringContext<'_, 'hir> {
1313
// Entry point for `ExprKind::Closure`. Plain closures go through
1414
// `lower_expr_plain_closure_with_move_exprs`, which can wrap the lowered
1515
// closure in `let` initializers for `move(...)`. Coroutine closures keep the

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
@@ -28,10 +28,9 @@ use super::{
2828
};
2929
use crate::diagnostics::ConstComptimeFn;
3030

31-
pub(super) struct ItemLowerer<'hir> {
31+
pub(super) struct ItemLowerer<'a, 'hir> {
3232
pub(super) tcx: TyCtxt<'hir>,
33-
pub(super) resolver: &'hir ResolverAstLowering<'hir>,
34-
pub(super) next_node_id: NodeId,
33+
pub(super) resolver: &'a ResolverAstLowering<'hir>,
3534
}
3635

3736
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -53,13 +52,13 @@ fn add_ty_alias_where_clause(
5352
if before.0 || !after.0 { before } else { after };
5453
}
5554

56-
impl<'hir> ItemLowerer<'hir> {
55+
impl<'hir> ItemLowerer<'_, 'hir> {
5756
fn with_lctx(
5857
&mut self,
5958
owner: NodeId,
60-
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
59+
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
6160
) -> hir::MaybeOwner<'hir> {
62-
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner, self.next_node_id);
61+
let mut lctx = LoweringContext::new(self.tcx, self.resolver, owner);
6362

6463
let item = f(&mut lctx);
6564
debug_assert_eq!(lctx.current_hir_id_owner, item.def_id());
@@ -97,7 +96,7 @@ impl<'hir> ItemLowerer<'hir> {
9796
}
9897
}
9998

100-
impl<'hir> LoweringContext<'hir> {
99+
impl<'hir> LoweringContext<'_, 'hir> {
101100
pub(super) fn lower_mod(
102101
&mut self,
103102
items: &[Box<Item>],
@@ -1475,7 +1474,7 @@ impl<'hir> LoweringContext<'hir> {
14751474
pub(crate) fn lower_coroutine_body_with_moved_arguments(
14761475
&mut self,
14771476
decl: &FnDecl,
1478-
lower_body: impl FnOnce(&mut LoweringContext<'hir>) -> hir::Expr<'hir>,
1477+
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
14791478
fn_decl_span: Span,
14801479
body_span: Span,
14811480
coroutine_kind: CoroutineKind,
@@ -1612,7 +1611,7 @@ impl<'hir> LoweringContext<'hir> {
16121611
parameters.push(new_parameter);
16131612
}
16141613

1615-
let mkbody = |this: &mut LoweringContext<'hir>| {
1614+
let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
16161615
// Create a block from the user's function body:
16171616
let user_body = lower_body(this);
16181617

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 59 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ pub fn provide(providers: &mut Providers) {
9797
providers.lower_to_hir = lower_to_hir;
9898
}
9999

100-
struct LoweringContext<'hir> {
100+
struct LoweringContext<'a, 'hir> {
101101
tcx: TyCtxt<'hir>,
102-
resolver: &'hir ResolverAstLowering<'hir>,
102+
resolver: &'a ResolverAstLowering<'hir>,
103103
current_disambiguator: PerParentDisambiguatorState,
104104

105105
/// Used to allocate HIR nodes.
@@ -132,7 +132,7 @@ struct LoweringContext<'hir> {
132132
is_in_dyn_type: bool,
133133

134134
current_hir_id_owner: hir::OwnerId,
135-
owner: &'hir PerOwnerResolverData<'hir>,
135+
owner: &'a PerOwnerResolverData<'hir>,
136136
item_local_id_counter: hir::ItemLocalId,
137137
trait_map: ItemLocalMap<&'hir [TraitCandidate<'hir>]>,
138138

@@ -174,13 +174,8 @@ struct LoweringContext<'hir> {
174174
attribute_parser: AttributeParser<'hir>,
175175
}
176176

177-
impl<'hir> LoweringContext<'hir> {
178-
fn new(
179-
tcx: TyCtxt<'hir>,
180-
resolver: &'hir ResolverAstLowering<'hir>,
181-
owner: NodeId,
182-
next_node_id: NodeId,
183-
) -> Self {
177+
impl<'a, 'hir> LoweringContext<'a, 'hir> {
178+
fn new(tcx: TyCtxt<'hir>, resolver: &'a ResolverAstLowering<'hir>, owner: NodeId) -> Self {
184179
let current_ast_owner = &resolver.owners[&owner];
185180
let current_hir_id_owner = hir::OwnerId { def_id: current_ast_owner.def_id };
186181
let current_disambiguator = resolver
@@ -210,7 +205,7 @@ impl<'hir> LoweringContext<'hir> {
210205
#[cfg(debug_assertions)]
211206
node_id_to_local_id: Default::default(),
212207
trait_map: Default::default(),
213-
next_node_id,
208+
next_node_id: resolver.next_node_id,
214209
node_id_to_def_id: NodeMap::default(),
215210
partial_res_overrides: NodeMap::default(),
216211

@@ -459,14 +454,18 @@ enum TryBlockScope {
459454
Heterogeneous(HirId),
460455
}
461456

462-
fn index_ast<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> (IndexVec<LocalDefId, Steal<AstOwner>>, NodeId) {
457+
fn index_ast<'tcx>(
458+
tcx: TyCtxt<'tcx>,
459+
(): (),
460+
) -> IndexVec<LocalDefId, Steal<(Arc<ResolverAstLowering<'tcx>>, AstOwner)>> {
463461
// Queries that borrow `resolver_for_lowering`.
464462
tcx.ensure_done().output_filenames(());
465463
tcx.ensure_done().early_lint_checks(());
466464
tcx.ensure_done().get_lang_items(());
467465
tcx.ensure_done().debugger_visualizers(LOCAL_CRATE);
468466

469467
let (resolver, krate) = tcx.resolver_for_lowering();
468+
let mut resolver = resolver.steal();
470469
let mut krate = krate.steal();
471470

472471
let mut indexer = Indexer {
@@ -476,19 +475,24 @@ fn index_ast<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> (IndexVec<LocalDefId, Steal<Ast
476475
};
477476
indexer.visit_crate(&mut krate);
478477
indexer.insert(CRATE_NODE_ID, AstOwner::Crate(Box::new(krate)));
479-
return (indexer.index, indexer.next_node_id);
478+
resolver.next_node_id = indexer.next_node_id;
480479

481-
struct Indexer<'s, 'ast> {
482-
owners: &'s NodeMap<PerOwnerResolverData<'ast>>,
483-
index: IndexVec<LocalDefId, Steal<AstOwner>>,
480+
let index = indexer.index;
481+
let resolver = Arc::new(resolver);
482+
let index = index.into_iter().map(|owner| Steal::new((Arc::clone(&resolver), owner))).collect();
483+
return index;
484+
485+
struct Indexer<'s, 'hir> {
486+
owners: &'s NodeMap<PerOwnerResolverData<'hir>>,
487+
index: IndexVec<LocalDefId, AstOwner>,
484488
next_node_id: NodeId,
485489
}
486490

487491
impl Indexer<'_, '_> {
488492
fn insert(&mut self, id: NodeId, node: AstOwner) {
489493
let def_id = self.owners[&id].def_id;
490-
self.index.ensure_contains_elem(def_id, || Steal::new(AstOwner::NonOwner));
491-
self.index[def_id] = Steal::new(node);
494+
self.index.ensure_contains_elem(def_id, || AstOwner::NonOwner);
495+
self.index[def_id] = node;
492496
}
493497

494498
fn make_dummy<K>(
@@ -617,43 +621,46 @@ fn lower_to_hir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> hir::MaybeOwner<'_> {
617621
tcx.ensure_done().early_lint_checks(());
618622
tcx.ensure_done().debugger_visualizers(LOCAL_CRATE);
619623
tcx.ensure_done().get_lang_items(());
620-
let (resolver, _) = tcx.resolver_for_lowering();
621-
let (ast_index, next_node_id) = tcx.index_ast(());
622-
let node = ast_index.get(def_id).map(Steal::steal);
623-
624-
let mut item_lowerer = item::ItemLowerer { tcx, resolver, next_node_id: *next_node_id };
625-
626-
// The item existed in the AST.
627-
let parent_id = match node.as_ref() {
628-
Some(AstOwner::Crate(c)) => return item_lowerer.lower_crate(&c),
629-
Some(AstOwner::Item(item)) => return item_lowerer.lower_item(&item),
630-
Some(AstOwner::TraitItem(item)) => {
631-
return item_lowerer.lower_trait_item(&item);
632-
}
633-
Some(AstOwner::ImplItem(item)) => {
634-
return item_lowerer.lower_impl_item(&item);
624+
let ast_index = tcx.index_ast(());
625+
let resolver_and_node = ast_index.get(def_id).map(Steal::steal);
626+
627+
let fallback_to_parent = |parent_id| {
628+
// The item did not exist in the AST, it was created by its parent.
629+
let mut parent_info = tcx.lower_to_hir(parent_id);
630+
if let hir::MaybeOwner::NonOwner(hir_id) = parent_info {
631+
parent_info = tcx.lower_to_hir(hir_id.owner);
635632
}
636-
Some(AstOwner::ForeignItem(item)) => return item_lowerer.lower_foreign_item(&item),
637-
Some(AstOwner::Synthetic(parent_id)) => *parent_id,
638-
Some(AstOwner::NonOwner) | None => tcx.local_parent(def_id),
633+
634+
let parent_info = parent_info.unwrap();
635+
*parent_info.children.get(&def_id).unwrap_or_else(|| {
636+
panic!(
637+
"{:?} does not appear in children of {:?}",
638+
def_id,
639+
parent_info.nodes.node().def_id()
640+
)
641+
})
639642
};
640643

641-
tcx.sess.time("drop_ast", || std::mem::drop(node));
644+
let Some((resolver, node)) = resolver_and_node else {
645+
return fallback_to_parent(tcx.local_parent(def_id));
646+
};
642647

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

649-
let parent_info = parent_info.unwrap();
650-
*parent_info.children.get(&def_id).unwrap_or_else(|| {
651-
panic!(
652-
"{:?} does not appear in children of {:?}",
653-
def_id,
654-
parent_info.nodes.node().def_id()
655-
)
656-
})
661+
tcx.sess.time("drop_ast", || std::mem::drop(node));
662+
663+
item
657664
}
658665

659666
#[derive(Copy, Clone, PartialEq, Debug)]
@@ -683,7 +690,7 @@ enum GenericArgsMode {
683690
Silence,
684691
}
685692

686-
impl<'hir> LoweringContext<'hir> {
693+
impl<'hir> LoweringContext<'_, 'hir> {
687694
fn create_def(
688695
&mut self,
689696
node_id: NodeId,
@@ -3138,7 +3145,7 @@ impl<'hir> GenericArgsCtor<'hir> {
31383145
&& self.parenthesized == hir::GenericArgsParentheses::No
31393146
}
31403147

3141-
fn into_generic_args(self, this: &LoweringContext<'hir>) -> &'hir hir::GenericArgs<'hir> {
3148+
fn into_generic_args(self, this: &LoweringContext<'_, 'hir>) -> &'hir hir::GenericArgs<'hir> {
31423149
let ga = hir::GenericArgs {
31433150
args: this.arena.alloc_from_iter(self.args),
31443151
constraints: self.constraints,

0 commit comments

Comments
 (0)