Skip to content

Commit 7df868a

Browse files
committed
Do not modify resolver outputs during lowering
Co-authored-by: Camille Gillot <gillot.camille@gmail.com>
1 parent 2f201bc commit 7df868a

21 files changed

Lines changed: 132 additions & 219 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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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,
@@ -265,7 +265,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
265265
}
266266

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

271271
// Function parameter count, including C variadic `...` if present.
@@ -414,7 +414,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
414414
&& idx == 0
415415
{
416416
let mut self_resolver = SelfResolver {
417-
resolver: this.resolver,
417+
ctxt: this,
418418
path_id: delegation.id,
419419
self_param_id: pat_node_id,
420420
};
@@ -667,25 +667,25 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
667667
}
668668
}
669669

670-
struct SelfResolver<'a, R> {
671-
resolver: &'a mut R,
670+
struct SelfResolver<'a, 'hir> {
671+
ctxt: &'a mut LoweringContext<'hir>,
672672
path_id: NodeId,
673673
self_param_id: NodeId,
674674
}
675675

676-
impl<'tcx, R: ResolverAstLoweringExt<'tcx>> SelfResolver<'_, R> {
676+
impl SelfResolver<'_, '_> {
677677
fn try_replace_id(&mut self, id: NodeId) {
678-
if let Some(res) = self.resolver.get_partial_res(id)
678+
if let Some(res) = self.ctxt.get_partial_res(id)
679679
&& let Some(Res::Local(sig_id)) = res.full_res()
680680
&& sig_id == self.path_id
681681
{
682682
let new_res = PartialRes::new(Res::Local(self.self_param_id));
683-
self.resolver.insert_partial_res(id, new_res);
683+
self.ctxt.partial_res_overrides.insert(id, new_res);
684684
}
685685
}
686686
}
687687

688-
impl<'ast, 'tcx, R: ResolverAstLoweringExt<'tcx>> Visitor<'ast> for SelfResolver<'_, R> {
688+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
689689
fn visit_id(&mut self, id: NodeId) {
690690
self.try_replace_id(id);
691691
}

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};
1010

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

1313
#[derive(Clone, Copy)]
1414
pub(super) enum DelegationGenericsKind {
@@ -97,7 +97,7 @@ impl DelegationGenericsKind {
9797
impl<'hir> HirOrTyGenerics<'hir> {
9898
pub(super) fn into_hir_generics(
9999
&mut self,
100-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
100+
ctx: &mut LoweringContext<'hir>,
101101
span: Span,
102102
) -> &mut HirOrTyGenerics<'hir> {
103103
if let HirOrTyGenerics::Ty(ty) = self {
@@ -117,7 +117,7 @@ impl<'hir> HirOrTyGenerics<'hir> {
117117

118118
pub(super) fn into_generic_args(
119119
&self,
120-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
120+
ctx: &mut LoweringContext<'hir>,
121121
add_lifetimes: bool,
122122
span: Span,
123123
) -> &'hir hir::GenericArgs<'hir> {
@@ -151,7 +151,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
151151
pub(super) fn all_params(
152152
&mut self,
153153
span: Span,
154-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
154+
ctx: &mut LoweringContext<'hir>,
155155
) -> impl Iterator<Item = hir::GenericParam<'hir>> {
156156
// Now we always call `into_hir_generics` both on child and parent,
157157
// however in future we would not do that, when scenarios like
@@ -185,7 +185,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
185185
pub(super) fn all_predicates(
186186
&mut self,
187187
span: Span,
188-
ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
188+
ctx: &mut LoweringContext<'hir>,
189189
) -> impl Iterator<Item = hir::WherePredicate<'hir>> {
190190
// Now we always call `into_hir_generics` both on child and parent,
191191
// however in future we would not do that, when scenarios like
@@ -203,7 +203,7 @@ impl<'hir> GenericsGenerationResults<'hir> {
203203
}
204204
}
205205

206-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
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: 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
}
@@ -1237,10 +1237,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
12371237
whole_span: Span,
12381238
) -> hir::ExprKind<'hir> {
12391239
// Return early in case of an ordinary assignment.
1240-
fn is_ordinary<'hir>(
1241-
lower_ctx: &mut LoweringContext<'_, 'hir, impl ResolverAstLoweringExt<'hir>>,
1242-
lhs: &Expr,
1243-
) -> bool {
1240+
fn is_ordinary(lower_ctx: &mut LoweringContext<'_>, lhs: &Expr) -> bool {
12441241
match &lhs.kind {
12451242
ExprKind::Array(..)
12461243
| ExprKind::Struct(..)
@@ -1295,7 +1292,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
12951292
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
12961293
if let ExprKind::Path(qself, path) = &expr.kind {
12971294
// Does the path resolve to something disallowed in a tuple struct/variant pattern?
1298-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1295+
if let Some(partial_res) = self.get_partial_res(expr.id) {
12991296
if let Some(res) = partial_res.full_res()
13001297
&& !res.expected_in_tuple_struct_pat()
13011298
{
@@ -1317,7 +1314,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
13171314
) -> Option<(&'a Option<Box<QSelf>>, &'a Path)> {
13181315
if let ExprKind::Path(qself, path) = &expr.kind {
13191316
// Does the path resolve to something disallowed in a unit struct/variant pattern?
1320-
if let Some(partial_res) = self.resolver.get_partial_res(expr.id) {
1317+
if let Some(partial_res) = self.get_partial_res(expr.id) {
13211318
if let Some(res) = partial_res.full_res()
13221319
&& !res.expected_in_unit_struct_pat()
13231320
{

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: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_hir::{
1313
};
1414
use rustc_index::{IndexSlice, IndexVec};
1515
use rustc_middle::span_bug;
16-
use rustc_middle::ty::TyCtxt;
16+
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
1717
use rustc_span::def_id::DefId;
1818
use rustc_span::edit_distance::find_best_match_for_name;
1919
use rustc_span::{DUMMY_SP, DesugaringKind, Ident, Span, Symbol, kw, sym};
@@ -48,9 +48,9 @@ impl<'hir> Owners<'_, 'hir> {
4848
}
4949
}
5050

51-
pub(super) struct ItemLowerer<'a, 'hir, R> {
51+
pub(super) struct ItemLowerer<'a, 'hir> {
5252
pub(super) tcx: TyCtxt<'hir>,
53-
pub(super) resolver: &'a mut R,
53+
pub(super) resolver: &'hir ResolverAstLowering<'hir>,
5454
pub(super) ast_index: &'a IndexSlice<LocalDefId, AstOwner<'a>>,
5555
pub(super) owners: Owners<'a, 'hir>,
5656
}
@@ -74,11 +74,11 @@ fn add_ty_alias_where_clause(
7474
if before.0 || !after.0 { before } else { after };
7575
}
7676

77-
impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
77+
impl<'hir> ItemLowerer<'_, 'hir> {
7878
fn with_lctx(
7979
&mut self,
8080
owner: NodeId,
81-
f: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::OwnerNode<'hir>,
81+
f: impl FnOnce(&mut LoweringContext<'hir>) -> hir::OwnerNode<'hir>,
8282
) {
8383
let mut lctx = LoweringContext::new(self.tcx, self.resolver);
8484
lctx.with_hir_id_owner(owner, |lctx| f(lctx));
@@ -122,7 +122,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> ItemLowerer<'_, 'hir, R> {
122122
}
123123
}
124124

125-
impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
125+
impl<'hir> LoweringContext<'hir> {
126126
pub(super) fn lower_mod(
127127
&mut self,
128128
items: &[Box<Item>],
@@ -635,7 +635,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
635635
}
636636

637637
fn lower_path_simple_eii(&mut self, id: NodeId, path: &Path) -> Option<DefId> {
638-
let res = self.resolver.get_partial_res(id)?;
638+
let res = self.get_partial_res(id)?;
639639
let Some(did) = res.expect_full_res().opt_def_id() else {
640640
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
641641
return None;
@@ -1336,7 +1336,6 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
13361336
ImplItemImplKind::Trait {
13371337
defaultness,
13381338
trait_item_def_id: self
1339-
.resolver
13401339
.get_partial_res(i.id)
13411340
.and_then(|r| r.expect_full_res().opt_def_id())
13421341
.ok_or_else(|| {
@@ -1532,7 +1531,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
15321531
pub(crate) fn lower_coroutine_body_with_moved_arguments(
15331532
&mut self,
15341533
decl: &FnDecl,
1535-
lower_body: impl FnOnce(&mut LoweringContext<'_, 'hir, R>) -> hir::Expr<'hir>,
1534+
lower_body: impl FnOnce(&mut LoweringContext<'hir>) -> hir::Expr<'hir>,
15361535
fn_decl_span: Span,
15371536
body_span: Span,
15381537
coroutine_kind: CoroutineKind,
@@ -1669,7 +1668,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
16691668
parameters.push(new_parameter);
16701669
}
16711670

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

@@ -1854,7 +1853,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
18541853
let kind = match &r.kind {
18551854
RestrictionKind::Unrestricted => hir::RestrictionKind::Unrestricted,
18561855
RestrictionKind::Restricted { path, id, shorthand: _ } => {
1857-
let res = self.resolver.get_partial_res(*id);
1856+
let res = self.get_partial_res(*id);
18581857
if let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) {
18591858
hir::RestrictionKind::Restricted(self.arena.alloc(hir::Path {
18601859
res: did,
@@ -1920,7 +1919,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19201919

19211920
// Introduce extra lifetimes if late resolution tells us to.
19221921
let extra_lifetimes = self.resolver.extra_lifetime_params(parent_node_id);
1923-
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)| {
19241923
self.lifetime_res_to_generic_param(
19251924
ident,
19261925
node_id,
@@ -1962,7 +1961,7 @@ impl<'hir, R: ResolverAstLoweringExt<'hir>> LoweringContext<'_, 'hir, R> {
19621961
return;
19631962
};
19641963
let define_opaque = define_opaque.iter().filter_map(|(id, path)| {
1965-
let res = self.resolver.get_partial_res(*id);
1964+
let res = self.get_partial_res(*id);
19661965
let Some(did) = res.and_then(|res| res.expect_full_res().opt_def_id()) else {
19671966
self.dcx().span_delayed_bug(path.span, "should have errored in resolve");
19681967
return None;

0 commit comments

Comments
 (0)