Skip to content

Commit bdb9661

Browse files
committed
Remove hir::AssocItemKind.
1 parent c96a690 commit bdb9661

File tree

32 files changed

+376
-517
lines changed

32 files changed

+376
-517
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ use rustc_errors::ErrorGuaranteed;
4747
use rustc_hir::def_id::DefId;
4848
use rustc_middle::span_bug;
4949
use rustc_middle::ty::{Asyncness, ResolverAstLowering};
50+
use rustc_span::symbol::kw;
5051
use rustc_span::{Ident, Span, Symbol};
5152
use {rustc_ast as ast, rustc_hir as hir};
5253

@@ -61,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
6162
}
6263

6364
impl<'hir> LoweringContext<'_, 'hir> {
64-
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
65-
pub(crate) fn delegatee_is_method(
66-
&self,
67-
item_id: NodeId,
68-
path_id: NodeId,
69-
span: Span,
70-
is_in_trait_impl: bool,
71-
) -> bool {
72-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
73-
let Ok(sig_id) = sig_id else {
74-
return false;
75-
};
76-
self.is_method(sig_id, span)
77-
}
78-
7965
fn is_method(&self, def_id: DefId, span: Span) -> bool {
8066
match self.tcx.def_kind(def_id) {
8167
DefKind::Fn => false,
@@ -101,10 +87,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
10187
let sig_id = self.get_delegation_sig_id(item_id, delegation.id, span, is_in_trait_impl);
10288
match sig_id {
10389
Ok(sig_id) => {
90+
let is_method = self.is_method(sig_id, span);
10491
let (param_count, c_variadic) = self.param_count(sig_id);
10592
let decl = self.lower_delegation_decl(sig_id, param_count, c_variadic, span);
10693
let sig = self.lower_delegation_sig(sig_id, decl, span);
107-
let body_id = self.lower_delegation_body(delegation, param_count, span);
94+
let body_id = self.lower_delegation_body(delegation, is_method, param_count, span);
10895
let ident = self.lower_ident(delegation.ident);
10996
let generics = self.lower_delegation_generics(span);
11097
DelegationResults { body_id, sig, ident, generics }
@@ -234,10 +221,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
234221
hir::FnSig { decl, header, span }
235222
}
236223

237-
fn generate_param(&mut self, idx: usize, span: Span) -> (hir::Param<'hir>, NodeId) {
224+
fn generate_param(
225+
&mut self,
226+
is_method: bool,
227+
idx: usize,
228+
span: Span,
229+
) -> (hir::Param<'hir>, NodeId) {
238230
let pat_node_id = self.next_node_id();
239231
let pat_id = self.lower_node_id(pat_node_id);
240-
let ident = Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}")));
232+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
233+
let name = if is_method && idx == 0 {
234+
kw::SelfLower
235+
} else {
236+
Symbol::intern(&format!("arg{idx}"))
237+
};
238+
let ident = Ident::with_dummy_span(name);
241239
let pat = self.arena.alloc(hir::Pat {
242240
hir_id: pat_id,
243241
kind: hir::PatKind::Binding(hir::BindingMode::NONE, pat_id, ident, None),
@@ -248,9 +246,21 @@ impl<'hir> LoweringContext<'_, 'hir> {
248246
(hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }, pat_node_id)
249247
}
250248

251-
fn generate_arg(&mut self, idx: usize, param_id: HirId, span: Span) -> hir::Expr<'hir> {
249+
fn generate_arg(
250+
&mut self,
251+
is_method: bool,
252+
idx: usize,
253+
param_id: HirId,
254+
span: Span,
255+
) -> hir::Expr<'hir> {
256+
// FIXME(cjgillot) AssocItem currently relies on self parameter being exactly named `self`.
257+
let name = if is_method && idx == 0 {
258+
kw::SelfLower
259+
} else {
260+
Symbol::intern(&format!("arg{idx}"))
261+
};
252262
let segments = self.arena.alloc_from_iter(iter::once(hir::PathSegment {
253-
ident: Ident::with_dummy_span(Symbol::intern(&format!("arg{idx}"))),
263+
ident: Ident::with_dummy_span(name),
254264
hir_id: self.next_id(),
255265
res: Res::Local(param_id),
256266
args: None,
@@ -264,6 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
264274
fn lower_delegation_body(
265275
&mut self,
266276
delegation: &Delegation,
277+
is_method: bool,
267278
param_count: usize,
268279
span: Span,
269280
) -> BodyId {
@@ -274,7 +285,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
274285
let mut args: Vec<hir::Expr<'_>> = Vec::with_capacity(param_count);
275286

276287
for idx in 0..param_count {
277-
let (param, pat_node_id) = this.generate_param(idx, span);
288+
let (param, pat_node_id) = this.generate_param(is_method, idx, span);
278289
parameters.push(param);
279290

280291
let arg = if let Some(block) = block
@@ -290,7 +301,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
290301
this.ident_and_label_to_local_id.insert(pat_node_id, param.pat.hir_id.local_id);
291302
this.lower_target_expr(&block)
292303
} else {
293-
this.generate_arg(idx, param.pat.hir_id, span)
304+
this.generate_arg(is_method, idx, param.pat.hir_id, span)
294305
};
295306
args.push(arg);
296307
}

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
384384
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
385385
// Do not visit the duplicate information in TraitItemRef. We want to
386386
// map the actual nodes, not the duplicate ones in the *Ref.
387-
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
387+
let TraitItemRef { id, ident: _, span: _ } = *ii;
388388

389389
self.visit_nested_trait_item(id);
390390
}
391391

392392
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
393393
// Do not visit the duplicate information in ImplItemRef. We want to
394394
// map the actual nodes, not the duplicate ones in the *Ref.
395-
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
395+
let ImplItemRef { id, ident: _, span: _ } = *ii;
396396

397397
self.visit_nested_impl_item(id);
398398
}

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
393393
(trait_ref, lowered_ty)
394394
});
395395

396-
let new_impl_items = self.arena.alloc_from_iter(
397-
impl_items
398-
.iter()
399-
.map(|item| self.lower_impl_item_ref(item, trait_ref.is_some())),
400-
);
396+
let new_impl_items = self
397+
.arena
398+
.alloc_from_iter(impl_items.iter().map(|item| self.lower_impl_item_ref(item)));
401399

402400
// `defaultness.has_value()` is never called for an `impl`, always `true` in order
403401
// to not cause an assertion failure inside the `lower_defaultness` function.
@@ -973,30 +971,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
973971
}
974972

975973
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
976-
let (ident, kind) = match &i.kind {
977-
AssocItemKind::Const(box ConstItem { ident, .. }) => {
978-
(*ident, hir::AssocItemKind::Const)
979-
}
980-
AssocItemKind::Type(box TyAlias { ident, .. }) => (*ident, hir::AssocItemKind::Type),
981-
AssocItemKind::Fn(box Fn { ident, sig, .. }) => {
982-
(*ident, hir::AssocItemKind::Fn { has_self: sig.decl.has_self() })
983-
}
984-
AssocItemKind::Delegation(box delegation) => (
985-
delegation.ident,
986-
hir::AssocItemKind::Fn {
987-
has_self: self.delegatee_is_method(i.id, delegation.id, i.span, false),
988-
},
989-
),
990-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
991-
panic!("macros should have been expanded by now")
992-
}
993-
};
994974
let id = hir::TraitItemId { owner_id: self.owner_id(i.id) };
995975
hir::TraitItemRef {
996976
id,
997-
ident: self.lower_ident(ident),
977+
ident: self.lower_ident(i.kind.ident().unwrap()),
998978
span: self.lower_span(i.span),
999-
kind,
1000979
}
1001980
}
1002981

@@ -1128,40 +1107,22 @@ impl<'hir> LoweringContext<'_, 'hir> {
11281107
span: self.lower_span(i.span),
11291108
defaultness,
11301109
has_delayed_lints: !self.delayed_lints.is_empty(),
1110+
trait_item_def_id: self
1111+
.resolver
1112+
.get_partial_res(i.id)
1113+
.map(|r| r.expect_full_res().opt_def_id())
1114+
.unwrap_or(None),
11311115
};
11321116
self.arena.alloc(item)
11331117
}
11341118

1135-
fn lower_impl_item_ref(&mut self, i: &AssocItem, is_in_trait_impl: bool) -> hir::ImplItemRef {
1119+
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
11361120
hir::ImplItemRef {
11371121
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
11381122
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
11391123
// assoc item kinds without an identifier and they cannot reach here.
11401124
ident: self.lower_ident(i.kind.ident().unwrap()),
11411125
span: self.lower_span(i.span),
1142-
kind: match &i.kind {
1143-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
1144-
AssocItemKind::Type(..) => hir::AssocItemKind::Type,
1145-
AssocItemKind::Fn(box Fn { sig, .. }) => {
1146-
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
1147-
}
1148-
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1149-
has_self: self.delegatee_is_method(
1150-
i.id,
1151-
delegation.id,
1152-
i.span,
1153-
is_in_trait_impl,
1154-
),
1155-
},
1156-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
1157-
panic!("macros should have been expanded by now")
1158-
}
1159-
},
1160-
trait_item_def_id: self
1161-
.resolver
1162-
.get_partial_res(i.id)
1163-
.map(|r| r.expect_full_res().opt_def_id())
1164-
.unwrap_or(None),
11651126
}
11661127
}
11671128

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -681,46 +681,30 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
681681
return (false, false, None);
682682
}
683683
let my_def = self.body.source.def_id();
684-
let my_hir = self.infcx.tcx.local_def_id_to_hir_id(my_def.as_local().unwrap());
685684
let Some(td) =
686685
self.infcx.tcx.impl_of_method(my_def).and_then(|x| self.infcx.tcx.trait_id_of_impl(x))
687686
else {
688687
return (false, false, None);
689688
};
689+
690+
let implemented_trait_item = self.infcx.tcx.associated_item(my_def).trait_item_def_id;
691+
690692
(
691693
true,
692694
td.is_local(),
693-
td.as_local().and_then(|tld| match self.infcx.tcx.hir_node_by_def_id(tld) {
694-
Node::Item(hir::Item {
695-
kind: hir::ItemKind::Trait(_, _, _, _, _, items), ..
696-
}) => {
697-
let mut f_in_trait_opt = None;
698-
for hir::TraitItemRef { id: fi, kind: k, .. } in *items {
699-
let hi = fi.hir_id();
700-
if !matches!(k, hir::AssocItemKind::Fn { .. }) {
701-
continue;
702-
}
703-
if self.infcx.tcx.hir_name(hi) != self.infcx.tcx.hir_name(my_hir) {
704-
continue;
705-
}
706-
f_in_trait_opt = Some(hi);
707-
break;
708-
}
709-
f_in_trait_opt.and_then(|f_in_trait| {
710-
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node(f_in_trait)
711-
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
712-
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
713-
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
714-
&& let hir::Mutability::Not = mut_ty.mutbl
715-
&& sig.decl.implicit_self.has_implicit_self()
716-
{
717-
Some(ty.span)
718-
} else {
719-
None
720-
}
721-
})
695+
implemented_trait_item.and_then(|f_in_trait| {
696+
let f_in_trait = f_in_trait.as_local()?;
697+
if let Node::TraitItem(ti) = self.infcx.tcx.hir_node_by_def_id(f_in_trait)
698+
&& let hir::TraitItemKind::Fn(sig, _) = ti.kind
699+
&& let Some(ty) = sig.decl.inputs.get(local.index() - 1)
700+
&& let hir::TyKind::Ref(_, mut_ty) = ty.kind
701+
&& let hir::Mutability::Not = mut_ty.mutbl
702+
&& sig.decl.implicit_self.has_implicit_self()
703+
{
704+
Some(ty.span)
705+
} else {
706+
None
722707
}
723-
_ => None,
724708
}),
725709
)
726710
}

compiler/rustc_hir/src/hir.rs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3171,6 +3171,8 @@ pub struct ImplItem<'hir> {
31713171
pub span: Span,
31723172
pub vis_span: Span,
31733173
pub has_delayed_lints: bool,
3174+
/// When we are in a trait impl, link to the trait-item's id.
3175+
pub trait_item_def_id: Option<DefId>,
31743176
}
31753177

31763178
impl<'hir> ImplItem<'hir> {
@@ -4412,7 +4414,6 @@ impl ItemKind<'_> {
44124414
pub struct TraitItemRef {
44134415
pub id: TraitItemId,
44144416
pub ident: Ident,
4415-
pub kind: AssocItemKind,
44164417
pub span: Span,
44174418
}
44184419

@@ -4426,17 +4427,7 @@ pub struct TraitItemRef {
44264427
pub struct ImplItemRef {
44274428
pub id: ImplItemId,
44284429
pub ident: Ident,
4429-
pub kind: AssocItemKind,
44304430
pub span: Span,
4431-
/// When we are in a trait impl, link to the trait-item's id.
4432-
pub trait_item_def_id: Option<DefId>,
4433-
}
4434-
4435-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
4436-
pub enum AssocItemKind {
4437-
Const,
4438-
Fn { has_self: bool },
4439-
Type,
44404431
}
44414432

44424433
// The bodies for items are stored "out of line", in a separate
@@ -4968,7 +4959,7 @@ mod size_asserts {
49684959
static_assert_size!(GenericBound<'_>, 64);
49694960
static_assert_size!(Generics<'_>, 56);
49704961
static_assert_size!(Impl<'_>, 80);
4971-
static_assert_size!(ImplItem<'_>, 88);
4962+
static_assert_size!(ImplItem<'_>, 96);
49724963
static_assert_size!(ImplItemKind<'_>, 40);
49734964
static_assert_size!(Item<'_>, 88);
49744965
static_assert_size!(ItemKind<'_>, 64);

compiler/rustc_hir/src/intravisit.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -499,9 +499,6 @@ pub trait Visitor<'v>: Sized {
499499
fn visit_attribute(&mut self, _attr: &'v Attribute) -> Self::Result {
500500
Self::Result::output()
501501
}
502-
fn visit_associated_item_kind(&mut self, kind: &'v AssocItemKind) -> Self::Result {
503-
walk_associated_item_kind(self, kind)
504-
}
505502
fn visit_defaultness(&mut self, defaultness: &'v Defaultness) -> Self::Result {
506503
walk_defaultness(self, defaultness)
507504
}
@@ -1252,10 +1249,9 @@ pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(
12521249
visitor: &mut V,
12531250
trait_item_ref: &'v TraitItemRef,
12541251
) -> V::Result {
1255-
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
1252+
let TraitItemRef { id, ident, span: _ } = *trait_item_ref;
12561253
try_visit!(visitor.visit_nested_trait_item(id));
1257-
try_visit!(visitor.visit_ident(ident));
1258-
visitor.visit_associated_item_kind(kind)
1254+
visitor.visit_ident(ident)
12591255
}
12601256

12611257
pub fn walk_impl_item<'v, V: Visitor<'v>>(
@@ -1271,6 +1267,7 @@ pub fn walk_impl_item<'v, V: Visitor<'v>>(
12711267
span: _,
12721268
vis_span: _,
12731269
has_delayed_lints: _,
1270+
trait_item_def_id: _,
12741271
} = *impl_item;
12751272

12761273
try_visit!(visitor.visit_ident(ident));
@@ -1306,10 +1303,9 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
13061303
visitor: &mut V,
13071304
impl_item_ref: &'v ImplItemRef,
13081305
) -> V::Result {
1309-
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
1306+
let ImplItemRef { id, ident, span: _ } = *impl_item_ref;
13101307
try_visit!(visitor.visit_nested_impl_item(id));
1311-
try_visit!(visitor.visit_ident(ident));
1312-
visitor.visit_associated_item_kind(kind)
1308+
visitor.visit_ident(ident)
13131309
}
13141310

13151311
pub fn walk_trait_ref<'v, V: Visitor<'v>>(
@@ -1483,13 +1479,6 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
14831479
V::Result::output()
14841480
}
14851481

1486-
pub fn walk_associated_item_kind<'v, V: Visitor<'v>>(_: &mut V, _: &'v AssocItemKind) -> V::Result {
1487-
// No visitable content here: this fn exists so you can call it if
1488-
// the right thing to do, should content be added in the future,
1489-
// would be to walk it.
1490-
V::Result::output()
1491-
}
1492-
14931482
pub fn walk_defaultness<'v, V: Visitor<'v>>(_: &mut V, _: &'v Defaultness) -> V::Result {
14941483
// No visitable content here: this fn exists so you can call it if
14951484
// the right thing to do, should content be added in the future,

0 commit comments

Comments
 (0)