Skip to content

Commit e9aa88f

Browse files
committed
Remove ResolverAstLoweringExt, try to address perf issues
1 parent abdb010 commit e9aa88f

8 files changed

Lines changed: 51 additions & 123 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ 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

2624
impl<'hir> LoweringContext<'_, '_, 'hir> {
2725
pub(crate) fn lower_inline_asm(

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ use crate::delegation::generics::{GenericsGenerationResult, GenericsGenerationRe
6060
use crate::errors::{CycleInDelegationSignatureResolution, UnresolvedDelegationCallee};
6161
use crate::{
6262
AllowReturnTypeNotation, CombinedResolverAstLowering, GenericArgsMode, ImplTraitContext,
63-
ImplTraitPosition, LoweringContext, ParamMode, ResolverAstLoweringExt,
63+
ImplTraitPosition, LoweringContext, ParamMode,
6464
};
6565

6666
mod generics;
@@ -139,9 +139,12 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
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) => {
143-
self.resolver.delegation_fn_sig(local_def_id).is_some_and(|sig| sig.has_self)
144-
}
142+
Some(local_def_id) => self
143+
.resolver
144+
.base
145+
.delegation_fn_sigs
146+
.get(&local_def_id)
147+
.is_some_and(|sig| sig.has_self),
145148
None => self.tcx.associated_item(def_id).is_method(),
146149
},
147150
_ => span_bug!(span, "unexpected DefKind for delegation item"),
@@ -157,7 +160,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
157160

158161
// Delegation can be unresolved in illegal places such as function bodies in extern blocks (see #151356)
159162
let ids = if let Some(delegation_info) =
160-
self.resolver.delegation_info(self.local_def_id(item_id))
163+
self.resolver.base.delegation_infos.get(&self.local_def_id(item_id))
161164
{
162165
self.get_delegation_ids(delegation_info.resolution_node, span)
163166
} else {
@@ -342,10 +345,10 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
342345

343346
fn get_attrs(&self, local_id: LocalDefId) -> &DelegationAttrs {
344347
// local_id can correspond either to a function or other delegation
345-
if let Some(fn_sig) = self.resolver.delegation_fn_sig(local_id) {
348+
if let Some(fn_sig) = self.resolver.base.delegation_fn_sigs.get(&local_id) {
346349
&fn_sig.attrs
347350
} else {
348-
&self.resolver.delegation_info(local_id).unwrap().attrs
351+
&self.resolver.base.delegation_infos[&local_id].attrs
349352
}
350353
}
351354

@@ -376,7 +379,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
376379
// it means that we refer to another delegation as a callee, so in order to obtain
377380
// a signature DefId we obtain NodeId of the callee delegation and try to get signature from it.
378381
if let Some(local_id) = def_id.as_local()
379-
&& let Some(delegation_info) = self.resolver.delegation_info(local_id)
382+
&& let Some(delegation_info) = self.resolver.base.delegation_infos.get(&local_id)
380383
{
381384
node_id = delegation_info.resolution_node;
382385
if visited.contains(&node_id) {
@@ -400,7 +403,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
400403
// Function parameter count, including C variadic `...` if present.
401404
fn param_count(&self, def_id: DefId) -> (usize, bool /*c_variadic*/) {
402405
if let Some(local_sig_id) = def_id.as_local() {
403-
match self.resolver.delegation_fn_sig(local_sig_id) {
406+
match self.resolver.base.delegation_fn_sigs.get(&local_sig_id) {
404407
Some(sig) => (sig.param_count, sig.c_variadic),
405408
None => (0, false),
406409
}
@@ -455,7 +458,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
455458
span: Span,
456459
) -> hir::FnSig<'hir> {
457460
let header = if let Some(local_sig_id) = sig_id.as_local() {
458-
match self.resolver.delegation_fn_sig(local_sig_id) {
461+
match self.resolver.base.delegation_fn_sigs.get(&local_sig_id) {
459462
Some(sig) => {
460463
let parent = self.tcx.parent(sig_id);
461464
// HACK: we override the default safety instead of generating attributes from the ether.

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 2 additions & 2 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, ResolverAstLoweringExt};
13+
use crate::{AstOwner, LoweringContext};
1414

1515
pub(super) enum DelegationGenerics<T> {
1616
/// User-specified args are present: `reuse foo::<String>;`.
@@ -293,7 +293,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
293293
p.id,
294294
self.tcx
295295
.create_def(
296-
self.resolver.def_id(item_id).unwrap(),
296+
self.local_def_id(item_id),
297297
Some(p.ident.name),
298298
match p.kind {
299299
GenericParamKind::Lifetime => DefKind::LifetimeParam,

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 3 additions & 5 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

@@ -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/item.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ 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
};
2727
use crate::CombinedResolverAstLowering;
2828

@@ -78,7 +78,7 @@ impl<'hir> ItemLowerer<'_, '_, 'hir> {
7878
match node {
7979
AstOwner::NonOwner => {}
8080
AstOwner::Crate(c) => {
81-
assert_eq!(self.resolver.def_id(CRATE_NODE_ID).unwrap(), CRATE_DEF_ID);
81+
assert_eq!(self.resolver.base.node_id_to_def_id[&CRATE_NODE_ID], CRATE_DEF_ID);
8282
self.with_lctx(CRATE_NODE_ID, |lctx| {
8383
let module = lctx.lower_mod(&c.items, &c.spans);
8484
// FIXME(jdonszelman): is dummy span ever a problem here?
@@ -1831,8 +1831,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
18311831
.collect();
18321832

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

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 26 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,8 @@ use rustc_hir::{
5656
LifetimeSyntax, ParamName, Target, TraitCandidate, find_attr,
5757
};
5858
use rustc_index::{Idx, IndexSlice, IndexVec};
59-
use rustc_macros::extension;
6059
use rustc_middle::span_bug;
61-
use rustc_middle::ty::{DelegationFnSig, DelegationInfo, ResolverAstLowering, TyCtxt};
60+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6261
use rustc_session::parse::add_feature_diagnostics;
6362
use rustc_span::symbol::{Ident, Symbol, kw, sym};
6463
use rustc_span::{DUMMY_SP, DesugaringKind, Span};
@@ -257,60 +256,20 @@ struct CombinedResolverAstLowering<'a, 'tcx> {
257256
mut_part: MutableResolverAstLowering,
258257
}
259258

260-
impl ResolverAstLoweringExt for CombinedResolverAstLowering<'_, '_> {
259+
impl CombinedResolverAstLowering<'_, '_> {
261260
#[inline]
262-
fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'_>) -> Option<Vec<usize>> {
263-
self.base.legacy_const_generic_args(expr, tcx)
264-
}
265-
266-
#[inline]
267-
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
268-
self.mut_part.partial_res_map.get(&id).cloned().or_else(|| self.base.get_partial_res(id))
269-
}
270-
271-
#[inline]
272-
fn get_import_res(&self, id: NodeId) -> Option<&PerNS<Option<Res<NodeId>>>> {
273-
self.base.get_import_res(id)
274-
}
275-
276-
#[inline]
277-
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
278-
self.base.get_label_res(id)
279-
}
280-
281-
#[inline]
282-
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
283-
self.base.get_lifetime_res(id)
284-
}
261+
fn get_partial_res(&self, id: NodeId) -> Option<&PartialRes> {
262+
let base_res = self.base.partial_res_map.get(&id);
285263

286-
#[inline]
287-
fn extra_lifetime_params(&self, id: NodeId) -> Option<&Vec<(Ident, NodeId, LifetimeRes)>> {
288-
self.base.extra_lifetime_params(id)
289-
}
290-
291-
#[inline]
292-
fn delegation_fn_sig(&self, id: LocalDefId) -> Option<&DelegationFnSig> {
293-
self.base.delegation_fn_sig(id)
294-
}
295-
296-
#[inline]
297-
fn delegation_info(&self, id: LocalDefId) -> Option<&DelegationInfo> {
298-
self.base.delegation_info(id)
299-
}
300-
301-
#[inline]
302-
fn def_id(&self, id: NodeId) -> Option<LocalDefId> {
303-
self.mut_part.node_id_to_def_id.get(&id).copied().or_else(|| self.base.def_id(id))
304-
}
264+
// Partial res map is modified only in delegation for now, so it will
265+
// be empty most of the time.
266+
if self.mut_part.partial_res_map.is_empty() {
267+
return base_res;
268+
}
305269

306-
#[inline]
307-
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
308-
self.base.lifetime_elision_allowed(id)
270+
self.mut_part.partial_res_map.get(&id).or(base_res)
309271
}
310-
}
311272

312-
#[extension(trait ResolverAstLoweringExt)]
313-
impl ResolverAstLowering<'_> {
314273
fn legacy_const_generic_args(&self, expr: &Expr, tcx: TyCtxt<'_>) -> Option<Vec<usize>> {
315274
let ExprKind::Path(None, path) = &expr.kind else {
316275
return None;
@@ -339,27 +298,10 @@ impl ResolverAstLowering<'_> {
339298
.map(|fn_indexes| fn_indexes.iter().map(|(num, _)| *num).collect())
340299
}
341300

342-
#[inline]
343-
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
344-
self.partial_res_map.get(&id).copied()
345-
}
346-
347-
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
348-
#[inline]
349-
fn get_import_res(&self, id: NodeId) -> Option<&PerNS<Option<Res<NodeId>>>> {
350-
self.import_res_map.get(&id)
351-
}
352-
353-
/// Obtains resolution for a label with the given `NodeId`.
354-
#[inline]
355-
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
356-
self.label_res_map.get(&id).copied()
357-
}
358-
359301
/// Obtains resolution for a lifetime with the given `NodeId`.
360302
#[inline]
361303
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
362-
self.lifetimes_res_map.get(&id).copied()
304+
self.base.lifetimes_res_map.get(&id).copied()
363305
}
364306

365307
/// Obtain the list of lifetimes parameters to add to an item.
@@ -370,28 +312,8 @@ impl ResolverAstLowering<'_> {
370312
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
371313
/// should appear at the enclosing `PolyTraitRef`.
372314
#[inline]
373-
fn extra_lifetime_params(&self, id: NodeId) -> Option<&Vec<(Ident, NodeId, LifetimeRes)>> {
374-
self.extra_lifetime_params_map.get(&id)
375-
}
376-
377-
#[inline]
378-
fn delegation_fn_sig(&self, id: LocalDefId) -> Option<&DelegationFnSig> {
379-
self.delegation_fn_sigs.get(&id)
380-
}
381-
382-
#[inline]
383-
fn delegation_info(&self, id: LocalDefId) -> Option<&DelegationInfo> {
384-
self.delegation_infos.get(&id)
385-
}
386-
387-
#[inline]
388-
fn def_id(&self, id: NodeId) -> Option<LocalDefId> {
389-
self.node_id_to_def_id.get(&id).copied()
390-
}
391-
392-
#[inline]
393-
fn lifetime_elision_allowed(&self, id: NodeId) -> bool {
394-
self.lifetime_elision_allowed.contains(&id)
315+
fn extra_lifetime_params(&self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
316+
self.base.extra_lifetime_params_map.get(&id).cloned().unwrap_or_default()
395317
}
396318
}
397319

@@ -712,7 +634,12 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
712634
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
713635
/// resolver (if any).
714636
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
715-
self.resolver.def_id(node)
637+
// We are only inserting new ids, not rewriting already existent.
638+
if let Some(&def_id) = self.resolver.base.node_id_to_def_id.get(&node) {
639+
return Some(def_id);
640+
}
641+
642+
self.resolver.mut_part.node_id_to_def_id.get(&node).copied()
716643
}
717644

718645
fn local_def_id(&self, node: NodeId) -> LocalDefId {
@@ -887,7 +814,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
887814
}
888815

889816
fn lower_import_res(&mut self, id: NodeId, span: Span) -> PerNS<Option<Res>> {
890-
let per_ns = self.resolver.get_import_res(id).copied().unwrap_or_default();
817+
let per_ns = self.resolver.base.import_res_map.get(&id).copied().unwrap_or_default();
891818
let per_ns = per_ns.map(|res| res.map(|res| self.lower_res(res)));
892819
if per_ns.is_empty() {
893820
// Propagate the error to all namespaces, just to be sure.
@@ -1019,8 +946,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
1019946
) -> &'hir [hir::GenericParam<'hir>] {
1020947
// Start by creating params for extra lifetimes params, as this creates the definitions
1021948
// that may be referred to by the AST inside `generic_params`.
1022-
let extra_lifetimes =
1023-
self.resolver.extra_lifetime_params(binder).cloned().unwrap_or_default();
949+
let extra_lifetimes = self.resolver.extra_lifetime_params(binder);
1024950
debug!(?extra_lifetimes);
1025951
let extra_lifetimes: Vec<_> = extra_lifetimes
1026952
.into_iter()
@@ -1859,7 +1785,11 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
18591785
inputs,
18601786
output,
18611787
c_variadic,
1862-
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed(fn_node_id),
1788+
lifetime_elision_allowed: self
1789+
.resolver
1790+
.base
1791+
.lifetime_elision_allowed
1792+
.contains(&fn_node_id),
18631793
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
18641794
let is_mutable_pat = matches!(
18651795
arg.pat.kind,

compiler/rustc_ast_lowering/src/pat.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_span::{DesugaringKind, Ident, Span};
1212
use super::errors::{
1313
ArbitraryExpressionInPattern, ExtraDoubleDot, MisplacedDoubleDot, SubTupleBinding,
1414
};
15-
use super::{ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt};
15+
use super::{ImplTraitContext, LoweringContext, ParamMode};
1616
use crate::{AllowReturnTypeNotation, ImplTraitPosition};
1717

1818
impl<'hir> LoweringContext<'_, '_, 'hir> {

compiler/rustc_ast_lowering/src/path.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use super::errors::{
1616
};
1717
use super::{
1818
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
19-
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
19+
LifetimeRes, LoweringContext, ParamMode,
2020
};
2121

2222
impl<'hir> LoweringContext<'_, '_, 'hir> {
@@ -41,7 +41,7 @@ impl<'hir> LoweringContext<'_, '_, 'hir> {
4141
});
4242

4343
let partial_res =
44-
self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err));
44+
self.resolver.get_partial_res(id).copied().unwrap_or_else(|| PartialRes::new(Res::Err));
4545
let base_res = partial_res.base_res();
4646
let unresolved_segments = partial_res.unresolved_segments();
4747

0 commit comments

Comments
 (0)