Skip to content

Commit 34c6408

Browse files
committed
Auto merge of #153656 - aerooneqq:split-ast-lowering-resolver, r=<try>
Split AST lowering resolver into mutable and readonly parts
2 parents 3bc6ea5 + abdb010 commit 34c6408

18 files changed

Lines changed: 222 additions & 112 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::{
2323
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt,
2424
};
2525

26-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
26+
impl<'hir> LoweringContext<'_, '_, 'hir> {
2727
pub(crate) fn lower_inline_asm(
2828
&mut self,
2929
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<'a, 'hir> LoweringContext<'a, '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<'a, 'hir> LoweringContext<'a, '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: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,16 @@ use rustc_hir as hir;
5151
use rustc_hir::attrs::{AttributeKind, InlineAttr};
5252
use rustc_hir::def_id::{DefId, LocalDefId};
5353
use rustc_middle::span_bug;
54-
use rustc_middle::ty::{Asyncness, DelegationAttrs, DelegationFnSigAttrs, ResolverAstLowering};
54+
use rustc_middle::ty::{Asyncness, DelegationAttrs, DelegationFnSigAttrs};
5555
use rustc_span::symbol::kw;
5656
use rustc_span::{DUMMY_SP, Ident, Span, Symbol};
5757
use smallvec::SmallVec;
5858

5959
use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationResults};
6060
use crate::errors::{CycleInDelegationSignatureResolution, UnresolvedDelegationCallee};
6161
use crate::{
62-
AllowReturnTypeNotation, GenericArgsMode, ImplTraitContext, ImplTraitPosition, LoweringContext,
63-
ParamMode, ResolverAstLoweringExt,
62+
AllowReturnTypeNotation, CombinedResolverAstLowering, GenericArgsMode, ImplTraitContext,
63+
ImplTraitPosition, LoweringContext, ParamMode, ResolverAstLoweringExt,
6464
};
6565

6666
mod generics;
@@ -134,16 +134,14 @@ impl DelegationIds {
134134
}
135135
}
136136

137-
impl<'hir> LoweringContext<'_, 'hir> {
137+
impl<'hir> LoweringContext<'_, '_, 'hir> {
138138
fn is_method(&self, def_id: DefId, span: Span) -> bool {
139139
match self.tcx.def_kind(def_id) {
140140
DefKind::Fn => false,
141141
DefKind::AssocFn => match def_id.as_local() {
142-
Some(local_def_id) => self
143-
.resolver
144-
.delegation_fn_sigs
145-
.get(&local_def_id)
146-
.is_some_and(|sig| sig.has_self),
142+
Some(local_def_id) => {
143+
self.resolver.delegation_fn_sig(local_def_id).is_some_and(|sig| sig.has_self)
144+
}
147145
None => self.tcx.associated_item(def_id).is_method(),
148146
},
149147
_ => span_bug!(span, "unexpected DefKind for delegation item"),
@@ -159,7 +157,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
159157

160158
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
161159
let ids = if let Some(delegation_info) =
162-
self.resolver.delegation_infos.get(&self.local_def_id(item_id))
160+
self.resolver.delegation_info(self.local_def_id(item_id))
163161
{
164162
self.get_delegation_ids(delegation_info.resolution_node, span)
165163
} else {
@@ -344,10 +342,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
344342

345343
fn get_attrs(&self, local_id: LocalDefId) -> &DelegationAttrs {
346344
// local_id can correspond either to a function or other delegation
347-
if let Some(fn_sig) = self.resolver.delegation_fn_sigs.get(&local_id) {
345+
if let Some(fn_sig) = self.resolver.delegation_fn_sig(local_id) {
348346
&fn_sig.attrs
349347
} else {
350-
&self.resolver.delegation_infos[&local_id].attrs
348+
&self.resolver.delegation_info(local_id).unwrap().attrs
351349
}
352350
}
353351

@@ -378,7 +376,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
378376
// it means that we refer to another delegation as a callee, so in order to obtain
379377
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
380378
if let Some(local_id) = def_id.as_local()
381-
&& let Some(delegation_info) = self.resolver.delegation_infos.get(&local_id)
379+
&& let Some(delegation_info) = self.resolver.delegation_info(local_id)
382380
{
383381
node_id = delegation_info.resolution_node;
384382
if visited.contains(&node_id) {
@@ -402,7 +400,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
402400
// Function parameter count, including C variadic `...` if present.
403401
fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
404402
if let Some(local_sig_id) = def_id.as_local() {
405-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
403+
match self.resolver.delegation_fn_sig(local_sig_id) {
406404
Some(sig) => (sig.param_count, sig.c_variadic),
407405
None => (0, false),
408406
}
@@ -457,7 +455,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
457455
span: Span,
458456
) -> hir::FnSig<'hir> {
459457
let header = if let Some(local_sig_id) = sig_id.as_local() {
460-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
458+
match self.resolver.delegation_fn_sig(local_sig_id) {
461459
Some(sig) => {
462460
let parent = self.tcx.parent(sig_id);
463461
// HACK: we override the default safety instead of generating attributes from the ether.
@@ -816,25 +814,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
816814
}
817815
}
818816

819-
struct SelfResolver<'a, 'tcx> {
820-
resolver: &'a mut ResolverAstLowering<'tcx>,
817+
struct SelfResolver<'a, 'b, 'tcx> {
818+
resolver: &'a mut CombinedResolverAstLowering<'b, 'tcx>,
821819
path_id: NodeId,
822820
self_param_id: NodeId,
823821
}
824822

825-
impl SelfResolver<'_, '_> {
823+
impl SelfResolver<'_, '_, '_> {
826824
fn try_replace_id(&mut self, id: NodeId) {
827-
if let Some(res) = self.resolver.partial_res_map.get(&id)
825+
if let Some(res) = self.resolver.get_partial_res(id)
828826
&& let Some(Res::Local(sig_id)) = res.full_res()
829827
&& sig_id == self.path_id
830828
{
831829
let new_res = PartialRes::new(Res::Local(self.self_param_id));
832-
self.resolver.partial_res_map.insert(id, new_res);
830+
self.resolver.mut_part.partial_res_map.insert(id, new_res);
833831
}
834832
}
835833
}
836834

837-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_> {
835+
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_, '_> {
838836
fn visit_id(&mut self, id: NodeId) {
839837
self.try_replace_id(id);
840838
}

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_span::symbol::kw;
1010
use rustc_span::{DUMMY_SP, Ident, Span};
1111
use thin_vec::{ThinVec, thin_vec};
1212

13-
use crate::{AstOwner, LoweringContext};
13+
use crate::{AstOwner, LoweringContext, ResolverAstLoweringExt};
1414

1515
pub(super) enum DelegationGenerics<T> {
1616
/// User-specified args are present: `reuse foo::<String>;`.
@@ -60,7 +60,7 @@ impl<T> DelegationGenerics<T> {
6060
impl<'hir> HirOrAstGenerics<'hir> {
6161
pub(super) fn into_hir_generics(
6262
&mut self,
63-
ctx: &mut LoweringContext<'_, 'hir>,
63+
ctx: &mut LoweringContext<'_, '_, 'hir>,
6464
item_id: NodeId,
6565
span: Span,
6666
) -> &mut HirOrAstGenerics<'hir> {
@@ -100,7 +100,7 @@ impl<'hir> HirOrAstGenerics<'hir> {
100100

101101
pub(super) fn into_generic_args(
102102
&self,
103-
ctx: &mut LoweringContext<'_, 'hir>,
103+
ctx: &mut LoweringContext<'_, '_, 'hir>,
104104
add_lifetimes: bool,
105105
span: Span,
106106
) -> Option<&'hir hir::GenericArgs<'hir>> {
@@ -140,7 +140,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
140140
&mut self,
141141
item_id: NodeId,
142142
span: Span,
143-
ctx: &mut LoweringContext<'_, 'hir>,
143+
ctx: &mut LoweringContext<'_, '_, 'hir>,
144144
) -> impl Iterator<Item = hir::GenericParam<'hir>> {
145145
// Now we always call `into_hir_generics` both on child and parent,
146146
// however in future we would not do that, when scenarios like
@@ -182,7 +182,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
182182
&mut self,
183183
item_id: NodeId,
184184
span: Span,
185-
ctx: &mut LoweringContext<'_, 'hir>,
185+
ctx: &mut LoweringContext<'_, '_, 'hir>,
186186
) -> impl Iterator<Item = hir::WherePredicate<'hir>> {
187187
// Now we always call `into_hir_generics` both on child and parent,
188188
// however in future we would not do that, when scenarios like
@@ -207,7 +207,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
207207
}
208208
}
209209

210-
impl<'hir> LoweringContext<'_, 'hir> {
210+
impl<'hir> LoweringContext<'_, '_, 'hir> {
211211
pub(super) fn lower_delegation_generics(
212212
&mut self,
213213
delegation: &Delegation,
@@ -289,11 +289,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
289289

290290
// Note that we use self.disambiguator here, if we will create new every time
291291
// we will get ICE if params have the same name.
292-
self.resolver.node_id_to_def_id.insert(
292+
self.resolver.mut_part.node_id_to_def_id.insert(
293293
p.id,
294294
self.tcx
295295
.create_def(
296-
self.resolver.node_id_to_def_id[&item_id],
296+
self.resolver.def_id(item_id).unwrap(),
297297
Some(p.ident.name),
298298
match p.kind {
299299
GenericParamKind::Lifetime => DefKind::LifetimeParam,
@@ -509,7 +509,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
509509

510510
let node_id = self.next_node_id();
511511

512-
self.resolver.partial_res_map.insert(node_id, hir::def::PartialRes::new(res));
512+
self.resolver.mut_part.partial_res_map.insert(node_id, hir::def::PartialRes::new(res));
513513

514514
GenericParamKind::Const {
515515
ty: Box::new(Ty {

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5353
}
5454
}
5555

56-
impl<'hir> LoweringContext<'_, 'hir> {
56+
impl<'hir> LoweringContext<'_, '_, 'hir> {
5757
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5858
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5959
}
@@ -1232,7 +1232,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12321232
whole_span: Span,
12331233
) -> hir::ExprKind<'hir> {
12341234
// Return early in case of an ordinary assignment.
1235-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1235+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_, '_>, lhs: &Expr) -> bool {
12361236
match &lhs.kind {
12371237
ExprKind::Array(..)
12381238
| 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: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_hir::{
1010
};
1111
use rustc_index::{IndexSlice, IndexVec};
1212
use rustc_middle::span_bug;
13-
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
13+
use rustc_middle::ty::TyCtxt;
1414
use rustc_span::def_id::DefId;
1515
use rustc_span::edit_distance::find_best_match_for_name;
1616
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
@@ -24,10 +24,11 @@ use super::{
2424
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
2525
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
2626
};
27+
use crate::CombinedResolverAstLowering;
2728

28-
pub(super) struct ItemLowerer<'a, 'hir> {
29+
pub(super) struct ItemLowerer<'a, 'b, 'hir> {
2930
pub(super) tcx: TyCtxt<'hir>,
30-
pub(super) resolver: &'a mut ResolverAstLowering<'hir>,
31+
pub(super) resolver: &'a mut CombinedResolverAstLowering<'b, 'hir>,
3132
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
3233
pub(super) owners: &'a mut IndexVec<LocalDefId, hir::MaybeOwner<'hir>>,
3334
}
@@ -51,13 +52,13 @@ fn add_ty_alias_where_clause(
5152
if before.0 || !after.0 { before } else { after };
5253
}
5354

54-
impl<'a, 'hir> ItemLowerer<'a, 'hir> {
55+
impl<'hir> ItemLowerer<'_, '_, 'hir> {
5556
fn with_lctx(
5657
&mut self,
5758
owner: NodeId,
58-
f: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::OwnerNode<'hir>,
59+
f: impl FnOnce(&mut LoweringContext<'_, '_, 'hir>) -> hir::OwnerNode<'hir>,
5960
) {
60-
let mut lctx = LoweringContext::new(self.tcx, self.ast_index, self.resolver);
61+
let mut lctx = LoweringContext::new(self.tcx, self.ast_index, &mut self.resolver);
6162
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
6263

6364
for (def_id, info) in lctx.children {
@@ -77,7 +78,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
7778
match node {
7879
AstOwner::NonOwner => {}
7980
AstOwner::Crate(c) => {
80-
assert_eq!(self.resolver.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
81+
assert_eq!(self.resolver.def_id(CRATE_NODE_ID).unwrap(), CRATE_DEF_ID);
8182
self.with_lctx(CRATE_NODE_ID, |lctx| {
8283
let module = lctx.lower_mod(&c.items, &c.spans);
8384
// FIXME(jdonszelman): is dummy span ever a problem here?
@@ -99,7 +100,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> {
99100
}
100101
}
101102

102-
impl<'hir> LoweringContext<'_, 'hir> {
103+
impl<'hir> LoweringContext<'_, '_, 'hir> {
103104
pub(super) fn lower_mod(
104105
&mut self,
105106
items: &[Box<Item>],
@@ -1475,7 +1476,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14751476
pub(crate) fn lower_coroutine_body_with_moved_arguments(
14761477
&mut self,
14771478
decl: &FnDecl,
1478-
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
1479+
lower_body: impl FnOnce(&mut LoweringContext<'_, '_, 'hir>) -> hir::Expr<'hir>,
14791480
fn_decl_span: Span,
14801481
body_span: Span,
14811482
coroutine_kind: CoroutineKind,
@@ -1612,7 +1613,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16121613
parameters.push(new_parameter);
16131614
}
16141615

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

@@ -1830,7 +1831,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
18301831
.collect();
18311832

18321833
// Introduce extra lifetimes if late resolution tells us to.
1833-
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1834+
let extra_lifetimes =
1835+
self.resolver.extra_lifetime_params(parent_node_id).cloned().unwrap_or_default();
18341836
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
18351837
self.lifetime_res_to_generic_param(
18361838
ident,

0 commit comments

Comments
 (0)