Skip to content

Commit 59e3380

Browse files
committed
Avoid some more global state
1 parent 7cdc456 commit 59e3380

File tree

3 files changed

+35
-22
lines changed

3 files changed

+35
-22
lines changed

Diff for: compiler/rustc_ast_lowering/src/delegation.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,14 @@ pub(crate) struct DelegationResults<'hir> {
6161

6262
impl<'hir> LoweringContext<'_, 'hir> {
6363
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
64-
pub(crate) fn delegatee_is_method(&self, item_id: NodeId, path_id: NodeId, span: Span) -> bool {
65-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span);
64+
pub(crate) fn delegatee_is_method(
65+
&self,
66+
item_id: NodeId,
67+
path_id: NodeId,
68+
span: Span,
69+
is_in_trait_impl: bool,
70+
) -> bool {
71+
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
6672
let Ok(sig_id) = sig_id else {
6773
return false;
6874
};
@@ -88,9 +94,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
8894
&mut self,
8995
delegation: &Delegation,
9096
item_id: NodeId,
97+
is_in_trait_impl: bool,
9198
) -> DelegationResults<'hir> {
9299
let span = self.lower_span(delegation.path.segments.last().unwrap().ident.span);
93-
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span);
100+
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
94101
match sig_id {
95102
Ok(sig_id) => {
96103
let (param_count, c_variadic) = self.param_count(sig_id);
@@ -110,8 +117,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
110117
item_id: NodeId,
111118
path_id: NodeId,
112119
span: Span,
120+
is_in_trait_impl: bool,
113121
) -> Result<DefId, ErrorGuaranteed> {
114-
let sig_id = if self.is_in_trait_impl { item_id } else { path_id };
122+
let sig_id = if is_in_trait_impl { item_id } else { path_id };
115123
self.get_resolution_id(sig_id, span)
116124
}
117125

Diff for: compiler/rustc_ast_lowering/src/item.rs

+23-16
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
404404
(trait_ref, lowered_ty)
405405
});
406406

407-
self.is_in_trait_impl = trait_ref.is_some();
408-
let new_impl_items = self
409-
.arena
410-
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
407+
let new_impl_items = self.arena.alloc_from_iter(
408+
impl_items
409+
.iter()
410+
.map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())),
411+
);
411412

412413
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
413414
// to not cause an assertion failure inside the `lower_defaultness` function.
@@ -484,7 +485,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
484485
ItemKind::Delegation(box delegation) => {
485486
debug_assert_ne!(ident.name, kw::Empty);
486487
let ident = self.lower_ident(ident);
487-
let delegation_results = self.lower_delegation(delegation, id);
488+
let delegation_results = self.lower_delegation(delegation, id, false);
488489
hir::ItemKind::Fn {
489490
ident,
490491
sig: delegation_results.sig,
@@ -634,10 +635,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
634635
match ctxt {
635636
AssocCtxt::Trait => hir::OwnerNode::TraitItem(self.lower_trait_item(item)),
636637
AssocCtxt::Impl { of_trait } => {
637-
if of_trait {
638-
self.is_in_trait_impl = of_trait;
639-
}
640-
hir::OwnerNode::ImplItem(self.lower_impl_item(item))
638+
hir::OwnerNode::ImplItem(self.lower_impl_item(item, of_trait))
641639
}
642640
}
643641
}
@@ -879,7 +877,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
879877
(generics, kind, ty.is_some())
880878
}
881879
AssocItemKind::Delegation(box delegation) => {
882-
let delegation_results = self.lower_delegation(delegation, i.id);
880+
let delegation_results = self.lower_delegation(delegation, i.id, false);
883881
let item_kind = hir::TraitItemKind::Fn(
884882
delegation_results.sig,
885883
hir::TraitFn::Provided(delegation_results.body_id),
@@ -910,7 +908,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
910908
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
911909
}
912910
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
913-
has_self: self.delegatee_is_method(i.id, delegation.id, i.span),
911+
has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false),
914912
},
915913
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
916914
panic!("macros should have been expanded by now")
@@ -930,7 +928,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
930928
self.expr(span, hir::ExprKind::Err(guar))
931929
}
932930

933-
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
931+
fn lower_impl_item(
932+
&mut self,
933+
i: &AssocItem,
934+
is_in_trait_impl: bool,
935+
) -> &'hir hir::ImplItem<'hir> {
934936
debug_assert_ne!(i.ident.name, kw::Empty);
935937
// Since `default impl` is not yet implemented, this is always true in impls.
936938
let has_value = true;
@@ -966,7 +968,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
966968
generics,
967969
sig,
968970
i.id,
969-
if self.is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
971+
if is_in_trait_impl { FnDeclKind::Impl } else { FnDeclKind::Inherent },
970972
sig.header.coroutine_kind,
971973
attrs,
972974
);
@@ -1006,7 +1008,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10061008
)
10071009
}
10081010
AssocItemKind::Delegation(box delegation) => {
1009-
let delegation_results = self.lower_delegation(delegation, i.id);
1011+
let delegation_results = self.lower_delegation(delegation, i.id, is_in_trait_impl);
10101012
(
10111013
delegation_results.generics,
10121014
hir::ImplItemKind::Fn(delegation_results.sig, delegation_results.body_id),
@@ -1029,7 +1031,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10291031
self.arena.alloc(item)
10301032
}
10311033

1032-
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
1034+
fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef {
10331035
hir::ImplItemRef {
10341036
id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } },
10351037
ident: self.lower_ident(i.ident),
@@ -1041,7 +1043,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
10411043
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
10421044
}
10431045
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1044-
has_self: self.delegatee_is_method(i.id, delegation.id, i.span),
1046+
has_self: self.delegatee_is_method(
1047+
i.id,
1048+
delegation.id,
1049+
i.span,
1050+
is_in_trait_impl,
1051+
),
10451052
},
10461053
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
10471054
panic!("macros should have been expanded by now")

Diff for: compiler/rustc_ast_lowering/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ struct LoweringContext<'a, 'hir> {
121121
catch_scope: Option<HirId>,
122122
loop_scope: Option<HirId>,
123123
is_in_loop_condition: bool,
124-
is_in_trait_impl: bool,
125124
is_in_dyn_type: bool,
126125

127126
current_hir_id_owner: hir::OwnerId,
@@ -173,7 +172,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
173172
catch_scope: None,
174173
loop_scope: None,
175174
is_in_loop_condition: false,
176-
is_in_trait_impl: false,
177175
is_in_dyn_type: false,
178176
coroutine_kind: None,
179177
task_context: None,

0 commit comments

Comments
 (0)