Skip to content

Commit fd90094

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 d1c7945 + a864db9 commit fd90094

18 files changed

Lines changed: 168 additions & 130 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@ use super::errors::{
1919
InvalidRegisterClass, RegisterClassOnlyClobber, RegisterClassOnlyClobberStable,
2020
RegisterConflict,
2121
};
22-
use crate::{
23-
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt,
24-
};
22+
use crate::{AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode};
2523

26-
impl<'a, 'hir> LoweringContext<'a, 'hir> {
24+
impl<'hir> LoweringContext<'_, '_, 'hir> {
2725
pub(crate) fn lower_inline_asm(
2826
&mut self,
2927
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: 17 additions & 16 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,
6464
};
6565

6666
mod generics;
@@ -134,13 +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() {
142142
Some(local_def_id) => self
143143
.resolver
144+
.base
144145
.delegation_fn_sigs
145146
.get(&local_def_id)
146147
.is_some_and(|sig| sig.has_self),
@@ -159,7 +160,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
159160

160161
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
161162
let ids = if let Some(delegation_info) =
162-
self.resolver.delegation_infos.get(&self.local_def_id(item_id))
163+
self.resolver.base.delegation_infos.get(&self.local_def_id(item_id))
163164
{
164165
self.get_delegation_ids(delegation_info.resolution_node, span)
165166
} else {
@@ -344,10 +345,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
344345

345346
fn get_attrs(&self, local_id: LocalDefId) -> &DelegationAttrs {
346347
// 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) {
348+
if let Some(fn_sig) = self.resolver.base.delegation_fn_sigs.get(&local_id) {
348349
&fn_sig.attrs
349350
} else {
350-
&self.resolver.delegation_infos[&local_id].attrs
351+
&self.resolver.base.delegation_infos[&local_id].attrs
351352
}
352353
}
353354

@@ -378,7 +379,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
378379
// it means that we refer to another delegation as a callee, so in order to obtain
379380
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
380381
if let Some(local_id) = def_id.as_local()
381-
&& let Some(delegation_info) = self.resolver.delegation_infos.get(&local_id)
382+
&& let Some(delegation_info) = self.resolver.base.delegation_infos.get(&local_id)
382383
{
383384
node_id = delegation_info.resolution_node;
384385
if visited.contains(&node_id) {
@@ -402,7 +403,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
402403
// Function parameter count, including C variadic `...` if present.
403404
fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
404405
if let Some(local_sig_id) = def_id.as_local() {
405-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
406+
match self.resolver.base.delegation_fn_sigs.get(&local_sig_id) {
406407
Some(sig) => (sig.param_count, sig.c_variadic),
407408
None => (0, false),
408409
}
@@ -457,7 +458,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
457458
span: Span,
458459
) -> hir::FnSig<'hir> {
459460
let header = if let Some(local_sig_id) = sig_id.as_local() {
460-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
461+
match self.resolver.base.delegation_fn_sigs.get(&local_sig_id) {
461462
Some(sig) => {
462463
let parent = self.tcx.parent(sig_id);
463464
// HACK: we override the default safety instead of generating attributes from the ether.
@@ -816,25 +817,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
816817
}
817818
}
818819

819-
struct SelfResolver<'a, 'tcx> {
820-
resolver: &'a mut ResolverAstLowering<'tcx>,
820+
struct SelfResolver<'a, 'b, 'tcx> {
821+
resolver: &'a mut CombinedResolverAstLowering<'b, 'tcx>,
821822
path_id: NodeId,
822823
self_param_id: NodeId,
823824
}
824825

825-
impl SelfResolver<'_, '_> {
826+
impl SelfResolver<'_, '_, '_> {
826827
fn try_replace_id(&mut self, id: NodeId) {
827-
if let Some(res) = self.resolver.partial_res_map.get(&id)
828+
if let Some(res) = self.resolver.get_partial_res(id)
828829
&& let Some(Res::Local(sig_id)) = res.full_res()
829830
&& sig_id == self.path_id
830831
{
831832
let new_res = PartialRes::new(Res::Local(self.self_param_id));
832-
self.resolver.partial_res_map.insert(id, new_res);
833+
self.resolver.mut_part.partial_res_map.insert(id, new_res);
833834
}
834835
}
835836
}
836837

837-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_> {
838+
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a, '_, '_> {
838839
fn visit_id(&mut self, id: NodeId) {
839840
self.try_replace_id(id);
840841
}

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.local_def_id(item_id),
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: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@ use super::errors::{
2424
InclusiveRangeWithNoEnd, MatchArmWithNoBody, NeverPatternWithBody, NeverPatternWithGuard,
2525
UnderscoreExprLhsAssign,
2626
};
27-
use super::{
28-
GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
29-
};
27+
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
3028
use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
3129
use crate::{AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, TryBlockScope};
3230

@@ -53,7 +51,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5351
}
5452
}
5553

56-
impl<'hir> LoweringContext<'_, 'hir> {
54+
impl<'hir> LoweringContext<'_, '_, 'hir> {
5755
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5856
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5957
}
@@ -1232,7 +1230,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
12321230
whole_span: Span,
12331231
) -> hir::ExprKind<'hir> {
12341232
// Return early in case of an ordinary assignment.
1235-
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
1233+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_, '_>, lhs: &Expr) -> bool {
12361234
match &lhs.kind {
12371235
ExprKind::Array(..)
12381236
| ExprKind::Struct(..)
@@ -1612,8 +1610,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
16121610
fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination {
16131611
let target_id = match destination {
16141612
Some((id, _)) => {
1615-
if let Some(loop_id) = self.resolver.get_label_res(id) {
1616-
let local_id = self.ident_and_label_to_local_id[&loop_id];
1613+
if let Some(loop_id) = self.resolver.base.label_res_map.get(&id) {
1614+
let local_id = self.ident_and_label_to_local_id[loop_id];
16171615
let loop_hir_id = HirId { owner: self.current_hir_id_owner, local_id };
16181616
Ok(loop_hir_id)
16191617
} else {

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: 11 additions & 10 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};
@@ -22,12 +22,13 @@ use super::errors::{InvalidAbi, InvalidAbiSuggestion, TupleStructWithDefault, Un
2222
use super::stability::{enabled_names, gate_unstable_abi};
2323
use super::{
2424
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
25-
RelaxedBoundForbiddenReason, RelaxedBoundPolicy, ResolverAstLoweringExt,
25+
RelaxedBoundForbiddenReason, RelaxedBoundPolicy,
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,11 +52,11 @@ 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
) {
6061
let mut lctx = LoweringContext::new(self.tcx, self.ast_index, self.resolver);
6162
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
@@ -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.base.node_id_to_def_id[&CRATE_NODE_ID], 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

0 commit comments

Comments
 (0)