Skip to content

Commit 3e45c29

Browse files
committed
Replace extension trait with wrapper struct
1 parent 4096bdf commit 3e45c29

File tree

7 files changed

+42
-36
lines changed

7 files changed

+42
-36
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::errors::{
2020
};
2121
use crate::{
2222
AllowReturnTypeNotation, ImplTraitContext, ImplTraitPosition, ParamMode,
23-
ResolverAstLoweringExt, fluent_generated as fluent,
23+
fluent_generated as fluent,
2424
};
2525

2626
impl<'a, 'hir> LoweringContext<'a, 'hir> {

compiler/rustc_ast_lowering/src/delegation.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ use rustc_ast::*;
4646
use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
49-
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
49+
use rustc_middle::ty::Asyncness;
5050
use rustc_span::{Ident, Span};
5151
use {rustc_ast as ast, rustc_hir as hir};
5252

5353
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
54-
use crate::{AllowReturnTypeNotation, ImplTraitPosition, ResolverAstLoweringExt};
54+
use crate::{AllowReturnTypeNotation, ImplTraitPosition, PerOwnerResolver};
5555

5656
pub(crate) struct DelegationResults<'hir> {
5757
pub body_id: hir::BodyId,
@@ -82,6 +82,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
8282
DefKind::AssocFn => match def_id.as_local() {
8383
Some(local_def_id) => self
8484
.resolver
85+
.general
8586
.delegation_fn_sigs
8687
.get(&local_def_id)
8788
.is_some_and(|sig| sig.has_self),
@@ -150,7 +151,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
150151
if let Some(local_sig_id) = sig_id.as_local() {
151152
// Map may be filled incorrectly due to recursive delegation.
152153
// Error will be emitted later during HIR ty lowering.
153-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
154+
match self.resolver.general.delegation_fn_sigs.get(&local_sig_id) {
154155
Some(sig) => (sig.param_count, sig.c_variadic),
155156
None => (0, false),
156157
}
@@ -198,7 +199,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
198199
span: Span,
199200
) -> hir::FnSig<'hir> {
200201
let header = if let Some(local_sig_id) = sig_id.as_local() {
201-
match self.resolver.delegation_fn_sigs.get(&local_sig_id) {
202+
match self.resolver.general.delegation_fn_sigs.get(&local_sig_id) {
202203
Some(sig) => {
203204
let parent = self.tcx.parent(sig_id);
204205
// HACK: we override the default safety instead of generating attributes from the ether.
@@ -280,7 +281,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
280281
&& idx == 0
281282
{
282283
let mut self_resolver = SelfResolver {
283-
resolver: this.resolver,
284+
resolver: &mut this.resolver,
284285
path_id: delegation.id,
285286
self_param_id: pat_node_id,
286287
};
@@ -426,25 +427,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
426427
}
427428
}
428429

429-
struct SelfResolver<'a> {
430-
resolver: &'a mut ResolverAstLowering,
430+
struct SelfResolver<'a, 'b> {
431+
resolver: &'b mut PerOwnerResolver<'a>,
431432
path_id: NodeId,
432433
self_param_id: NodeId,
433434
}
434435

435-
impl<'a> SelfResolver<'a> {
436+
impl SelfResolver<'_, '_> {
436437
fn try_replace_id(&mut self, id: NodeId) {
437-
if let Some(res) = self.resolver.partial_res_map.get(&id)
438+
if let Some(res) = self.resolver.general.partial_res_map.get(&id)
438439
&& let Some(Res::Local(sig_id)) = res.full_res()
439440
&& sig_id == self.path_id
440441
{
441442
let new_res = PartialRes::new(Res::Local(self.self_param_id));
442-
self.resolver.partial_res_map.insert(id, new_res);
443+
self.resolver.general.partial_res_map.insert(id, new_res);
443444
}
444445
}
445446
}
446447

447-
impl<'ast, 'a> Visitor<'ast> for SelfResolver<'a> {
448+
impl<'ast> Visitor<'ast> for SelfResolver<'_, '_> {
448449
fn visit_path(&mut self, path: &'ast Path, id: NodeId) {
449450
self.try_replace_id(id);
450451
visit::walk_path(self, path);

compiler/rustc_ast_lowering/src/expr.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ use super::errors::{
2323
InclusiveRangeWithNoEnd, MatchArmWithNoBody, NeverPatternWithBody, NeverPatternWithGuard,
2424
UnderscoreExprLhsAssign,
2525
};
26-
use super::{
27-
GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode, ResolverAstLoweringExt,
28-
};
26+
use super::{GenericArgsMode, ImplTraitContext, LoweringContext, ParamMode};
2927
use crate::errors::{InvalidLegacyConstGenericArg, UseConstGenericArg, YieldInClosure};
3028
use crate::{AllowReturnTypeNotation, FnDeclKind, ImplTraitPosition, fluent_generated};
3129

compiler/rustc_ast_lowering/src/item.rs

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use super::errors::{
2020
use super::stability::{enabled_names, gate_unstable_abi};
2121
use super::{
2222
AstOwner, FnDeclKind, ImplTraitContext, ImplTraitPosition, LoweringContext, ParamMode,
23-
ResolverAstLoweringExt,
2423
};
2524

2625
pub(super) struct ItemLowerer<'a, 'hir> {

compiler/rustc_ast_lowering/src/lib.rs

+26-18
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use rustc_hir::{
5959
TraitCandidate,
6060
};
6161
use rustc_index::{Idx, IndexSlice, IndexVec};
62-
use rustc_macros::extension;
6362
use rustc_middle::span_bug;
6463
use rustc_middle::ty::{ResolverAstLowering, TyCtxt};
6564
use rustc_session::parse::{add_feature_diagnostics, feature_err};
@@ -93,7 +92,7 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
9392

9493
struct LoweringContext<'a, 'hir> {
9594
tcx: TyCtxt<'hir>,
96-
resolver: &'a mut ResolverAstLowering,
95+
resolver: PerOwnerResolver<'a>,
9796

9897
/// Used to allocate HIR nodes.
9998
arena: &'hir hir::Arena<'hir>,
@@ -153,7 +152,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
153152
Self {
154153
// Pseudo-globals.
155154
tcx,
156-
resolver,
155+
resolver: PerOwnerResolver { general: resolver },
157156
arena: tcx.hir_arena,
158157

159158
// HirId handling.
@@ -201,8 +200,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
201200
}
202201
}
203202

204-
#[extension(trait ResolverAstLoweringExt)]
205-
impl ResolverAstLowering {
203+
pub(crate) struct PerOwnerResolver<'a> {
204+
pub general: &'a mut ResolverAstLowering,
205+
}
206+
207+
impl PerOwnerResolver<'_> {
206208
fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>> {
207209
if let ExprKind::Path(None, path) = &expr.kind {
208210
// Don't perform legacy const generics rewriting if the path already
@@ -211,15 +213,17 @@ impl ResolverAstLowering {
211213
return None;
212214
}
213215

214-
if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? {
216+
if let Res::Def(DefKind::Fn, def_id) =
217+
self.general.partial_res_map.get(&expr.id)?.full_res()?
218+
{
215219
// We only support cross-crate argument rewriting. Uses
216220
// within the same crate should be updated to use the new
217221
// const generics style.
218222
if def_id.is_local() {
219223
return None;
220224
}
221225

222-
if let Some(v) = self.legacy_const_generic_args.get(&def_id) {
226+
if let Some(v) = self.general.legacy_const_generic_args.get(&def_id) {
223227
return v.clone();
224228
}
225229
}
@@ -229,22 +233,22 @@ impl ResolverAstLowering {
229233
}
230234

231235
fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> {
232-
self.partial_res_map.get(&id).copied()
236+
self.general.partial_res_map.get(&id).copied()
233237
}
234238

235239
/// Obtains per-namespace resolutions for `use` statement with the given `NodeId`.
236240
fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>> {
237-
self.import_res_map.get(&id).copied().unwrap_or_default()
241+
self.general.import_res_map.get(&id).copied().unwrap_or_default()
238242
}
239243

240244
/// Obtains resolution for a label with the given `NodeId`.
241245
fn get_label_res(&self, id: NodeId) -> Option<NodeId> {
242-
self.label_res_map.get(&id).copied()
246+
self.general.label_res_map.get(&id).copied()
243247
}
244248

245249
/// Obtains resolution for a lifetime with the given `NodeId`.
246250
fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes> {
247-
self.lifetimes_res_map.get(&id).copied()
251+
self.general.lifetimes_res_map.get(&id).copied()
248252
}
249253

250254
/// Obtain the list of lifetimes parameters to add to an item.
@@ -255,7 +259,7 @@ impl ResolverAstLowering {
255259
/// The extra lifetimes that appear from the parenthesized `Fn`-trait desugaring
256260
/// should appear at the enclosing `PolyTraitRef`.
257261
fn extra_lifetime_params(&self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)> {
258-
self.extra_lifetime_params_map.get(&id).cloned().unwrap_or_default()
262+
self.general.extra_lifetime_params_map.get(&id).cloned().unwrap_or_default()
259263
}
260264
}
261265

@@ -514,22 +518,22 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
514518
self.tcx.at(span).create_def(self.current_hir_id_owner.def_id, name, def_kind).def_id();
515519

516520
debug!("create_def: def_id_to_node_id[{:?}] <-> {:?}", def_id, node_id);
517-
self.resolver.node_id_to_def_id.insert(node_id, def_id);
521+
self.resolver.general.node_id_to_def_id.insert(node_id, def_id);
518522

519523
def_id
520524
}
521525

522526
fn next_node_id(&mut self) -> NodeId {
523-
let start = self.resolver.next_node_id;
527+
let start = self.resolver.general.next_node_id;
524528
let next = start.as_u32().checked_add(1).expect("input too large; ran out of NodeIds");
525-
self.resolver.next_node_id = ast::NodeId::from_u32(next);
529+
self.resolver.general.next_node_id = ast::NodeId::from_u32(next);
526530
start
527531
}
528532

529533
/// Given the id of some node in the AST, finds the `LocalDefId` associated with it by the name
530534
/// resolver (if any).
531535
fn opt_local_def_id(&self, node: NodeId) -> Option<LocalDefId> {
532-
self.resolver.node_id_to_def_id.get(&node).copied()
536+
self.resolver.general.node_id_to_def_id.get(&node).copied()
533537
}
534538

535539
fn local_def_id(&self, node: NodeId) -> LocalDefId {
@@ -653,7 +657,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
653657
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
654658
}
655659

656-
if let Some(traits) = self.resolver.trait_map.remove(&ast_node_id) {
660+
if let Some(traits) = self.resolver.general.trait_map.remove(&ast_node_id) {
657661
self.trait_map.insert(hir_id.local_id, traits.into_boxed_slice());
658662
}
659663

@@ -1600,7 +1604,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
16001604
inputs,
16011605
output,
16021606
c_variadic,
1603-
lifetime_elision_allowed: self.resolver.lifetime_elision_allowed.contains(&fn_node_id),
1607+
lifetime_elision_allowed: self
1608+
.resolver
1609+
.general
1610+
.lifetime_elision_allowed
1611+
.contains(&fn_node_id),
16041612
implicit_self: decl.inputs.get(0).map_or(hir::ImplicitSelfKind::None, |arg| {
16051613
let is_mutable_pat = matches!(
16061614
arg.pat.kind,

compiler/rustc_ast_lowering/src/pat.rs

+1-1
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<'a, 'hir> LoweringContext<'a, 'hir> {

compiler/rustc_ast_lowering/src/path.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use super::errors::{
1717
};
1818
use super::{
1919
AllowReturnTypeNotation, GenericArgsCtor, GenericArgsMode, ImplTraitContext, ImplTraitPosition,
20-
LifetimeRes, LoweringContext, ParamMode, ResolverAstLoweringExt,
20+
LifetimeRes, LoweringContext, ParamMode,
2121
};
2222

2323
impl<'a, 'hir> LoweringContext<'a, 'hir> {

0 commit comments

Comments
 (0)