Skip to content

Commit 277b0ec

Browse files
committed
Remove hir::AssocItemKind.
1 parent 3ecd03b commit 277b0ec

File tree

32 files changed

+294
-456
lines changed

32 files changed

+294
-456
lines changed

compiler/rustc_ast_lowering/src/delegation.rs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,21 +62,6 @@ pub(crate) struct DelegationResults<'hir> {
6262
}
6363

6464
impl<'hir> LoweringContext<'_, 'hir> {
65-
/// Defines whether the delegatee is an associated function whose first parameter is `self`.
66-
pub(crate) fn delegatee_is_method(
67-
&self,
68-
item_id: NodeId,
69-
path_id: NodeId,
70-
span: Span,
71-
is_in_trait_impl: bool,
72-
) -> bool {
73-
let sig_id = self.get_delegation_sig_id(item_id, path_id, span, is_in_trait_impl);
74-
let Ok(sig_id) = sig_id else {
75-
return false;
76-
};
77-
self.is_method(sig_id, span)
78-
}
79-
8065
fn is_method(&self, def_id: DefId, span: Span) -> bool {
8166
match self.tcx.def_kind(def_id) {
8267
DefKind::Fn => false,

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: _ } = *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: 5 additions & 44 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

@@ -1137,31 +1116,13 @@ impl<'hir> LoweringContext<'_, 'hir> {
11371116
self.arena.alloc(item)
11381117
}
11391118

1140-
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 {
11411120
hir::ImplItemRef {
11421121
id: hir::ImplItemId { owner_id: self.owner_id(i.id) },
11431122
// `unwrap` is safe because `AssocItemKind::{MacCall,DelegationMac}` are the only
11441123
// assoc item kinds without an identifier and they cannot reach here.
11451124
ident: self.lower_ident(i.kind.ident().unwrap()),
11461125
span: self.lower_span(i.span),
1147-
kind: match &i.kind {
1148-
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
1149-
AssocItemKind::Type(..) => hir::AssocItemKind::Type,
1150-
AssocItemKind::Fn(box Fn { sig, .. }) => {
1151-
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
1152-
}
1153-
AssocItemKind::Delegation(box delegation) => hir::AssocItemKind::Fn {
1154-
has_self: self.delegatee_is_method(
1155-
i.id,
1156-
delegation.id,
1157-
i.span,
1158-
is_in_trait_impl,
1159-
),
1160-
},
1161-
AssocItemKind::MacCall(..) | AssocItemKind::DelegationMac(..) => {
1162-
panic!("macros should have been expanded by now")
1163-
}
1164-
},
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: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4415,7 +4415,6 @@ impl ItemKind<'_> {
44154415
pub struct TraitItemRef {
44164416
pub id: TraitItemId,
44174417
pub ident: Ident,
4418-
pub kind: AssocItemKind,
44194418
pub span: Span,
44204419
}
44214420

@@ -4429,17 +4428,9 @@ pub struct TraitItemRef {
44294428
pub struct ImplItemRef {
44304429
pub id: ImplItemId,
44314430
pub ident: Ident,
4432-
pub kind: AssocItemKind,
44334431
pub span: Span,
44344432
}
44354433

4436-
#[derive(Copy, Clone, PartialEq, Debug, HashStable_Generic)]
4437-
pub enum AssocItemKind {
4438-
Const,
4439-
Fn { has_self: bool },
4440-
Type,
4441-
}
4442-
44434434
// The bodies for items are stored "out of line", in a separate
44444435
// hashmap in the `Crate`. Here we just record the hir-id of the item
44454436
// so it can fetched later.

compiler/rustc_hir/src/intravisit.rs

Lines changed: 4 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>>(
@@ -1307,10 +1303,9 @@ pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(
13071303
visitor: &mut V,
13081304
impl_item_ref: &'v ImplItemRef,
13091305
) -> V::Result {
1310-
let ImplItemRef { id, ident, ref kind, span: _ } = *impl_item_ref;
1306+
let ImplItemRef { id, ident, span: _ } = *impl_item_ref;
13111307
try_visit!(visitor.visit_nested_impl_item(id));
1312-
try_visit!(visitor.visit_ident(ident));
1313-
visitor.visit_associated_item_kind(kind)
1308+
visitor.visit_ident(ident)
13141309
}
13151310

13161311
pub fn walk_trait_ref<'v, V: Visitor<'v>>(
@@ -1484,13 +1479,6 @@ pub fn walk_assoc_item_constraint<'v, V: Visitor<'v>>(
14841479
V::Result::output()
14851480
}
14861481

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

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ fn trait_def(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::TraitDef {
914914
let item = items.iter().find(|item| item.ident == *ident);
915915

916916
match item {
917-
Some(item) if matches!(item.kind, hir::AssocItemKind::Fn { .. }) => {
917+
Some(item) if matches!(tcx.def_kind(item.id.owner_id), DefKind::AssocFn) => {
918918
if !tcx.defaultness(item.id.owner_id).has_value() {
919919
tcx.dcx().emit_err(errors::FunctionNotHaveDefaultImplementation {
920920
span: item.span,

compiler/rustc_lint/src/builtin.rs

Lines changed: 34 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc_hir as hir;
2828
use rustc_hir::def::{DefKind, Res};
2929
use rustc_hir::def_id::{CRATE_DEF_ID, DefId, LocalDefId};
3030
use rustc_hir::intravisit::FnKind as HirFnKind;
31-
use rustc_hir::{Body, FnDecl, GenericParamKind, PatKind, PredicateOrigin};
31+
use rustc_hir::{Body, FnDecl, PatKind, PredicateOrigin};
3232
use rustc_middle::bug;
3333
use rustc_middle::lint::LevelAndSource;
3434
use rustc_middle::ty::layout::LayoutOf;
@@ -952,36 +952,34 @@ declare_lint! {
952952

953953
declare_lint_pass!(InvalidNoMangleItems => [NO_MANGLE_CONST_ITEMS, NO_MANGLE_GENERIC_ITEMS]);
954954

955+
impl InvalidNoMangleItems {
956+
fn check_no_mangle_on_generic_fn(
957+
&self,
958+
cx: &LateContext<'_>,
959+
attr_span: Span,
960+
def_id: LocalDefId,
961+
) {
962+
let generics = cx.tcx.generics_of(def_id);
963+
if generics.requires_monomorphization(cx.tcx) {
964+
cx.emit_span_lint(
965+
NO_MANGLE_GENERIC_ITEMS,
966+
cx.tcx.def_span(def_id),
967+
BuiltinNoMangleGeneric { suggestion: attr_span },
968+
);
969+
}
970+
}
971+
}
972+
955973
impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
956974
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
957975
let attrs = cx.tcx.hir_attrs(it.hir_id());
958-
let check_no_mangle_on_generic_fn = |attr_span: Span,
959-
impl_generics: Option<&hir::Generics<'_>>,
960-
generics: &hir::Generics<'_>,
961-
span| {
962-
for param in
963-
generics.params.iter().chain(impl_generics.map(|g| g.params).into_iter().flatten())
964-
{
965-
match param.kind {
966-
GenericParamKind::Lifetime { .. } => {}
967-
GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
968-
cx.emit_span_lint(
969-
NO_MANGLE_GENERIC_ITEMS,
970-
span,
971-
BuiltinNoMangleGeneric { suggestion: attr_span },
972-
);
973-
break;
974-
}
975-
}
976-
}
977-
};
978976
match it.kind {
979-
hir::ItemKind::Fn { generics, .. } => {
977+
hir::ItemKind::Fn { .. } => {
980978
if let Some(attr_span) =
981979
find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span)
982980
.or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span))
983981
{
984-
check_no_mangle_on_generic_fn(attr_span, None, generics, it.span);
982+
self.check_no_mangle_on_generic_fn(cx, attr_span, it.owner_id.def_id);
985983
}
986984
}
987985
hir::ItemKind::Const(..) => {
@@ -1006,24 +1004,19 @@ impl<'tcx> LateLintPass<'tcx> for InvalidNoMangleItems {
10061004
);
10071005
}
10081006
}
1009-
hir::ItemKind::Impl(hir::Impl { generics, items, .. }) => {
1010-
for it in *items {
1011-
if let hir::AssocItemKind::Fn { .. } = it.kind {
1012-
let attrs = cx.tcx.hir_attrs(it.id.hir_id());
1013-
if let Some(attr_span) =
1014-
find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span)
1015-
.or_else(
1016-
|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span),
1017-
)
1018-
{
1019-
check_no_mangle_on_generic_fn(
1020-
attr_span,
1021-
Some(generics),
1022-
cx.tcx.hir_get_generics(it.id.owner_id.def_id).unwrap(),
1023-
it.span,
1024-
);
1025-
}
1026-
}
1007+
_ => {}
1008+
}
1009+
}
1010+
1011+
fn check_impl_item(&mut self, cx: &LateContext<'_>, it: &hir::ImplItem<'_>) {
1012+
let attrs = cx.tcx.hir_attrs(it.hir_id());
1013+
match it.kind {
1014+
hir::ImplItemKind::Fn { .. } => {
1015+
if let Some(attr_span) =
1016+
find_attr!(attrs, AttributeKind::ExportName {span, ..} => *span)
1017+
.or_else(|| find_attr!(attrs, AttributeKind::NoMangle(span) => *span))
1018+
{
1019+
self.check_no_mangle_on_generic_fn(cx, attr_span, it.owner_id.def_id);
10271020
}
10281021
}
10291022
_ => {}

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,16 @@ impl AssocItems {
257257
self.items.get_by_key(Some(name))
258258
}
259259

260+
/// Returns the associated item with the given identifier and `AssocKind`, if one exists.
261+
/// The identifier is ignoring hygiene. This is meant to be used for lints and diagnostics.
262+
pub fn filter_by_name_unhygienic_and_kind(
263+
&self,
264+
name: Symbol,
265+
assoc_tag: AssocTag,
266+
) -> impl '_ + Iterator<Item = &ty::AssocItem> {
267+
self.filter_by_name_unhygienic(name).filter(move |item| item.as_tag() == assoc_tag)
268+
}
269+
260270
/// Returns the associated item with the given identifier and `AssocKind`, if one exists.
261271
/// The identifier is matched hygienically.
262272
pub fn find_by_ident_and_kind(

0 commit comments

Comments
 (0)