Skip to content

Commit fb76025

Browse files
committed
Auto merge of #155460 - cjgillot:unmut-resolver-ast, r=oli-obk
Do not modify resolver outputs during lowering Split from #142830 I believe this achieves the same thing as #153656 but in a much simpler way. This PR forces AST->HIR lowering to stop mutating resolver outputs. Instead, it manages a few override maps that only live during lowering and are dropped afterwards. r? @petrochenkov cc @aerooneqq
2 parents 7e0430f + 4433512 commit fb76025

15 files changed

Lines changed: 128 additions & 241 deletions

File tree

compiler/rustc_ast_lowering/src/asm.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ use super::errors::{
1818
InvalidRegisterClass, RegisterClassOnlyClobber, RegisterClassOnlyClobberStable,
1919
RegisterConflict,
2020
};
21-
use crate::{
22-
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode, ResolverAstLoweringExt,
23-
};
21+
use crate::{AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode};
2422

25-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
23+
impl<'hir> LoweringContext<'_, 'hir> {
2624
pub(crate) fn lower_inline_asm(
2725
&mut self,
2826
sp: Span,
@@ -203,7 +201,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
203201
},
204202
InlineAsmOperand::Sym { sym } => {
205203
let static_def_id = self
206-
.resolver
207204
.get_partial_res(sym.id)
208205
.and_then(|res| res.full_res())
209206
.and_then(|res| match res {

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use rustc_hir::Target;
44
use rustc_span::sym;
55
use smallvec::SmallVec;
66

7-
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext, ResolverAstLoweringExt};
7+
use crate::{ImplTraitContext, ImplTraitPosition, LoweringContext};
88

9-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use std::sync::Arc;
22

33
use thin_vec::thin_vec;
44

5-
use crate::{LoweringContext, ResolverAstLoweringExt};
5+
use crate::LoweringContext;
66

7-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
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: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
use std::iter;
4040

4141
use ast::visit::Visitor;
42-
use hir::def::{DefKind, PartialRes, Res};
42+
use hir::def::{DefKind, Res};
4343
use hir::{BodyId, HirId};
4444
use rustc_abi::ExternAbi;
4545
use rustc_ast as ast;
@@ -105,7 +105,7 @@ static ATTRS_ADDITIONS: &[AttrAdditionInfo] = &[
105105
},
106106
];
107107

108-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
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,
@@ -266,7 +266,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
266266
}
267267

268268
fn get_resolution_id(&self, node_id: NodeId) -> Option<DefId> {
269-
self.resolver.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
269+
self.get_partial_res(node_id).and_then(|r| r.expect_full_res().opt_def_id())
270270
}
271271

272272
// Function parameter count, including C variadic `...` if present.
@@ -417,7 +417,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
417417
&& idx == 0
418418
{
419419
let mut self_resolver = SelfResolver {
420-
resolver: this.resolver,
420+
ctxt: this,
421421
path_id: delegation.id,
422422
self_param_id: pat_node_id,
423423
};
@@ -665,25 +665,24 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
665665
}
666666
}
667667

668-
struct SelfResolver<'a, R> {
669-
resolver: &'a mut R,
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<'tcx, R: ResolverAstLoweringExt<'tcx>> SelfResolver<'_, R> {
674+
impl SelfResolver<'_, '_, '_> {
675675
fn try_replace_id(&mut self, id: NodeId) {
676-
if let Some(res) = self.resolver.get_partial_res(id)
676+
if let Some(res) = self.ctxt.get_partial_res(id)
677677
&& let Some(Res::Local(sig_id)) = res.full_res()
678678
&& sig_id == self.path_id
679679
{
680-
let new_res = PartialRes::new(Res::Local(self.self_param_id));
681-
self.resolver.insert_partial_res(id, new_res);
680+
self.ctxt.partial_res_overrides.insert(id, self.self_param_id);
682681
}
683682
}
684683
}
685684

686-
impl<'ast, 'tcx, R: ResolverAstLoweringExt<'tcx>> Visitor<'ast> for SelfResolver<'_, R> {
685+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_, '_> {
687686
fn visit_id(&mut self, id: NodeId) {
688687
self.try_replace_id(id);
689688
}

compiler/rustc_ast_lowering/src/delegation/generics.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_middle::{bug, ty};
88
use rustc_span::symbol::kw;
99
use rustc_span::{Ident, Span, sym};
1010

11-
use crate::{LoweringContext, ResolverAstLoweringExt};
11+
use crate::LoweringContext;
1212

1313
#[derive(Clone, Copy)]
1414
pub(super) enum DelegationGenericsKind {
@@ -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, impl ResolverAstLoweringExt<'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, impl ResolverAstLoweringExt<'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, impl ResolverAstLoweringExt<'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, impl ResolverAstLoweringExt<'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, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
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: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl<'v> rustc_ast::visit::Visitor<'v> for WillCreateDefIdsVisitor {
5151
}
5252
}
5353

54-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
54+
impl<'hir> LoweringContext<'_, 'hir> {
5555
fn lower_exprs(&mut self, exprs: &[Box<Expr>]) -> &'hir [hir::Expr<'hir>] {
5656
self.arena.alloc_from_iter(exprs.iter().map(|x| self.lower_expr_mut(x)))
5757
}
@@ -1235,10 +1235,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
12351235
whole_span: Span,
12361236
) -> hir::ExprKind<'hir> {
12371237
// Return early in case of an ordinary assignment.
1238-
fn is_ordinary<'hir>(
1239-
lower_ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
1240-
lhs: &Expr,
1241-
) -> bool {
1238+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_, '_>, lhs: &Expr) -> bool {
12421239
match &lhs.kind {
12431240
ExprKind::Array(..)
12441241
| ExprKind::Struct(..)
@@ -1293,7 +1290,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
12931290
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
12941291
if let ExprKind::Path(qself, path) = &expr.kind {
12951292
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
1296-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1293+
if let Some(partial_res) = self.get_partial_res(expr.id) {
12971294
if let Some(res) = partial_res.full_res()
12981295
&& !res.expected_in_tuple_struct_pat()
12991296
{
@@ -1315,7 +1312,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
13151312
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
13161313
if let ExprKind::Path(qself, path) = &expr.kind {
13171314
// Does the path resolve to something disallowed in a unit struct/variant pattern?
1318-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1315+
if let Some(partial_res) = self.get_partial_res(expr.id) {
13191316
if let Some(res) = partial_res.full_res()
13201317
&& !res.expected_in_unit_struct_pat()
13211318
{

compiler/rustc_ast_lowering/src/format.rs

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

99
use super::LoweringContext;
10-
use crate::ResolverAstLoweringExt;
1110

12-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
11+
impl<'hir> LoweringContext<'_, 'hir> {
1312
pub(crate) fn lower_format_args(&mut self, sp: Span, fmt: &FormatArgs) -> hir::ExprKind<'hir> {
1413
// Never call the const constructor of `fmt::Arguments` if the
1514
// format_args!() had any arguments _before_ flattening/inlining.
@@ -231,7 +230,7 @@ enum ArgumentType {
231230
/// <core::fmt::Argument>::new_…(arg)
232231
/// ```
233232
fn make_argument<'hir>(
234-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
233+
ctx: &mut LoweringContext<'_, 'hir>,
235234
sp: Span,
236235
arg: &'hir hir::Expr<'hir>,
237236
ty: ArgumentType,
@@ -278,7 +277,7 @@ fn make_count(
278277
}
279278

280279
fn expand_format_args<'hir>(
281-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
280+
ctx: &mut LoweringContext<'_, 'hir>,
282281
macsp: Span,
283282
fmt: &FormatArgs,
284283
allow_const: bool,

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
use std::mem;
2-
use std::sync::Arc;
32

43
use rustc_abi::ExternAbi;
54
use rustc_ast::visit::AssocCtxt;
65
use rustc_ast::*;
76
use rustc_data_structures::fx::FxIndexMap;
8-
use rustc_data_structures::steal::Steal;
97
use rustc_errors::{E0570, ErrorGuaranteed, struct_span_code_err};
108
use rustc_hir::attrs::{AttributeKind, EiiImplResolution};
119
use rustc_hir::def::{DefKind, PerNS, Res};
12-
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId, LocalDefIdMap};
13-
use rustc_hir::definitions::PerParentDisambiguatorState;
10+
use rustc_hir::def_id::{CRATE_DEF_ID, LocalDefId};
1411
use rustc_hir::{
1512
self as hir, HirId, ImplItemImplKind, LifetimeSource, PredicateOrigin, Target, find_attr,
1613
};
1714
use rustc_index::{IndexSlice, IndexVec};
1815
use rustc_middle::span_bug;
19-
use rustc_middle::ty::TyCtxt;
16+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
2017
use rustc_span::def_id::DefId;
2118
use rustc_span::edit_distance::find_best_match_for_name;
2219
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
@@ -51,21 +48,11 @@ impl<'hir> Owners<'_, 'hir> {
5148
}
5249
}
5350

54-
/// Default disambiguators are used during default lowering, when we lower
55-
/// AST owners in a loop we can use the whole map, in contrast delayed lowering
56-
/// lowers each AST owner separately, so we use readonly disambiguators map
57-
/// with `Steal`s to get disambiguators.
58-
pub(super) enum Disambiguators {
59-
Default(LocalDefIdMap<PerParentDisambiguatorState>),
60-
Delayed(Arc<LocalDefIdMap<Steal<PerParentDisambiguatorState>>>),
61-
}
62-
63-
pub(super) struct ItemLowerer<'a, 'hir, R> {
51+
pub(super) struct ItemLowerer<'a, 'hir> {
6452
pub(super) tcx: TyCtxt<'hir>,
65-
pub(super) resolver: &'a mut R,
53+
pub(super) resolver: &'a ResolverAstLowering<'hir>,
6654
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
6755
pub(super) owners: Owners<'a, 'hir>,
68-
pub(super) disambiguators: &'a mut Disambiguators,
6956
}
7057

7158
/// When we have a ty alias we *may* have two where clauses. To give the best diagnostics, we set the span
@@ -87,13 +74,13 @@ fn add_ty_alias_where_clause(
8774
if before.0 || !after.0 { before } else { after };
8875
}
8976

90-
impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
77+
impl<'hir> ItemLowerer<'_, 'hir> {
9178
fn with_lctx(
9279
&mut self,
9380
owner: NodeId,
94-
f: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::OwnerNode<'hir>,
81+
f: impl for<'a> FnOnce(&mut LoweringContext<'a, 'hir>) -> hir::OwnerNode<'hir>,
9582
) {
96-
let mut lctx = LoweringContext::new(self.tcx, self.resolver, self.disambiguators);
83+
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
9784
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
9885

9986
for (def_id, info) in lctx.children {
@@ -135,7 +122,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
135122
}
136123
}
137124

138-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
125+
impl<'hir> LoweringContext<'_, 'hir> {
139126
pub(super) fn lower_mod(
140127
&mut self,
141128
items: &[Box<Item>],
@@ -648,7 +635,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
648635
}
649636

650637
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> Option<DefId> {
651-
let res = self.resolver.get_partial_res(id)?;
638+
let res = self.get_partial_res(id)?;
652639
let Some(did) = res.expect_full_res().opt_def_id() else {
653640
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
654641
return None;
@@ -1349,7 +1336,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
13491336
ImplItemImplKind::Trait {
13501337
defaultness,
13511338
trait_item_def_id: self
1352-
.resolver
13531339
.get_partial_res(i.id)
13541340
.and_then(|r| r.expect_full_res().opt_def_id())
13551341
.ok_or_else(|| {
@@ -1545,7 +1531,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
15451531
pub(crate) fn lower_coroutine_body_with_moved_arguments(
15461532
&mut self,
15471533
decl: &FnDecl,
1548-
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::Expr<'hir>,
1534+
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir>) -> hir::Expr<'hir>,
15491535
fn_decl_span: Span,
15501536
body_span: Span,
15511537
coroutine_kind: CoroutineKind,
@@ -1682,7 +1668,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
16821668
parameters.push(new_parameter);
16831669
}
16841670

1685-
let mkbody = |this: &mut LoweringContext<'_, 'hir, R>| {
1671+
let mkbody = |this: &mut LoweringContext<'_, 'hir>| {
16861672
// Create a block from the user's function body:
16871673
let user_body = lower_body(this);
16881674

@@ -1867,7 +1853,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
18671853
let kind = match &r.kind {
18681854
RestrictionKind::Unrestricted => hir::RestrictionKind::Unrestricted,
18691855
RestrictionKind::Restricted { path, id, shorthand: _ } => {
1870-
let res = self.resolver.get_partial_res(*id);
1856+
let res = self.get_partial_res(*id);
18711857
if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) {
18721858
hir::RestrictionKind::Restricted(self.arena.alloc(hir::Path {
18731859
res: did,
@@ -1933,7 +1919,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19331919

19341920
// Introduce extra lifetimes if late resolution tells us to.
19351921
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1936-
params.extend(extra_lifetimes.into_iter().filter_map(|(ident, node_id, res)| {
1922+
params.extend(extra_lifetimes.into_iter().filter_map(|&(ident, node_id, res)| {
19371923
self.lifetime_res_to_generic_param(
19381924
ident,
19391925
node_id,
@@ -1975,7 +1961,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19751961
return;
19761962
};
19771963
let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
1978-
let res = self.resolver.get_partial_res(*id);
1964+
let res = self.get_partial_res(*id);
19791965
let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) else {
19801966
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
19811967
return None;

0 commit comments

Comments
 (0)