Skip to content

Commit 21de280

Browse files
committed
Auto merge of #95884 - cjgillot:assoc-item, r=lcnr
Thin `AssocItem` This PR removes a few fields from `AssocItem` that should be easily computed using other queries. This simplifies some of the metadata decoding.
2 parents fe33428 + 212a06e commit 21de280

File tree

61 files changed

+354
-403
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+354
-403
lines changed

compiler/rustc_ast_lowering/src/index.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -323,16 +323,15 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
323323
fn visit_trait_item_ref(&mut self, ii: &'hir TraitItemRef) {
324324
// Do not visit the duplicate information in TraitItemRef. We want to
325325
// map the actual nodes, not the duplicate ones in the *Ref.
326-
let TraitItemRef { id, ident: _, kind: _, span: _, defaultness: _ } = *ii;
326+
let TraitItemRef { id, ident: _, kind: _, span: _ } = *ii;
327327

328328
self.visit_nested_trait_item(id);
329329
}
330330

331331
fn visit_impl_item_ref(&mut self, ii: &'hir ImplItemRef) {
332332
// Do not visit the duplicate information in ImplItemRef. We want to
333333
// map the actual nodes, not the duplicate ones in the *Ref.
334-
let ImplItemRef { id, ident: _, kind: _, span: _, defaultness: _, trait_item_def_id: _ } =
335-
*ii;
334+
let ImplItemRef { id, ident: _, kind: _, span: _, trait_item_def_id: _ } = *ii;
336335

337336
self.visit_nested_impl_item(id);
338337
}

compiler/rustc_ast_lowering/src/item.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -755,17 +755,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
755755
let hir_id = self.lower_node_id(i.id);
756756
let trait_item_def_id = hir_id.expect_owner();
757757

758-
let (generics, kind) = match i.kind {
758+
let (generics, kind, has_default) = match i.kind {
759759
AssocItemKind::Const(_, ref ty, ref default) => {
760760
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
761761
let body = default.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
762-
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body))
762+
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
763763
}
764764
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: None, .. }) => {
765765
let names = self.lower_fn_params_to_names(&sig.decl);
766766
let (generics, sig) =
767767
self.lower_method_sig(generics, sig, i.id, FnDeclKind::Trait, None);
768-
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)))
768+
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Required(names)), false)
769769
}
770770
AssocItemKind::Fn(box Fn { ref sig, ref generics, body: Some(ref body), .. }) => {
771771
let asyncness = sig.header.asyncness;
@@ -778,7 +778,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
778778
FnDeclKind::Trait,
779779
asyncness.opt_return_id(),
780780
);
781-
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)))
781+
(generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true)
782782
}
783783
AssocItemKind::TyAlias(box TyAlias {
784784
ref generics,
@@ -789,7 +789,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
789789
}) => {
790790
let mut generics = generics.clone();
791791
add_ty_alias_where_clause(&mut generics, where_clauses, false);
792-
self.lower_generics(
792+
let (generics, kind) = self.lower_generics(
793793
&generics,
794794
i.id,
795795
ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
@@ -805,7 +805,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
805805
ty,
806806
)
807807
},
808-
)
808+
);
809+
(generics, kind, ty.is_some())
809810
}
810811
AssocItemKind::MacCall(..) => panic!("macro item shouldn't exist at this point"),
811812
};
@@ -817,28 +818,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
817818
generics,
818819
kind,
819820
span: self.lower_span(i.span),
821+
defaultness: hir::Defaultness::Default { has_value: has_default },
820822
};
821823
self.arena.alloc(item)
822824
}
823825

824826
fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef {
825-
let (kind, has_default) = match &i.kind {
826-
AssocItemKind::Const(_, _, default) => (hir::AssocItemKind::Const, default.is_some()),
827-
AssocItemKind::TyAlias(box TyAlias { ty, .. }) => {
828-
(hir::AssocItemKind::Type, ty.is_some())
829-
}
830-
AssocItemKind::Fn(box Fn { sig, body, .. }) => {
831-
(hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }, body.is_some())
827+
let kind = match &i.kind {
828+
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
829+
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,
830+
AssocItemKind::Fn(box Fn { sig, .. }) => {
831+
hir::AssocItemKind::Fn { has_self: sig.decl.has_self() }
832832
}
833833
AssocItemKind::MacCall(..) => unimplemented!(),
834834
};
835835
let id = hir::TraitItemId { def_id: self.local_def_id(i.id) };
836-
let defaultness = hir::Defaultness::Default { has_value: has_default };
837836
hir::TraitItemRef {
838837
id,
839838
ident: self.lower_ident(i.ident),
840839
span: self.lower_span(i.span),
841-
defaultness,
842840
kind,
843841
}
844842
}
@@ -849,6 +847,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
849847
}
850848

851849
fn lower_impl_item(&mut self, i: &AssocItem) -> &'hir hir::ImplItem<'hir> {
850+
// Since `default impl` is not yet implemented, this is always true in impls.
851+
let has_value = true;
852+
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
853+
852854
let (generics, kind) = match &i.kind {
853855
AssocItemKind::Const(_, ty, expr) => {
854856
let ty = self.lower_ty(ty, ImplTraitContext::Disallowed(ImplTraitPosition::Type));
@@ -903,19 +905,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
903905
kind,
904906
vis_span: self.lower_span(i.vis.span),
905907
span: self.lower_span(i.span),
908+
defaultness,
906909
};
907910
self.arena.alloc(item)
908911
}
909912

910913
fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef {
911-
// Since `default impl` is not yet implemented, this is always true in impls.
912-
let has_value = true;
913-
let (defaultness, _) = self.lower_defaultness(i.kind.defaultness(), has_value);
914914
hir::ImplItemRef {
915915
id: hir::ImplItemId { def_id: self.local_def_id(i.id) },
916916
ident: self.lower_ident(i.ident),
917917
span: self.lower_span(i.span),
918-
defaultness,
919918
kind: match &i.kind {
920919
AssocItemKind::Const(..) => hir::AssocItemKind::Const,
921920
AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type,

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -772,7 +772,7 @@ impl<'tcx> Visitor<'tcx> for Checker<'_, 'tcx> {
772772
let mut nonconst_call_permission = false;
773773
if let Some(callee_trait) = tcx.trait_of_item(callee)
774774
&& tcx.has_attr(callee_trait, sym::const_trait)
775-
&& Some(callee_trait) == tcx.trait_of_item(caller)
775+
&& Some(callee_trait) == tcx.trait_of_item(caller.to_def_id())
776776
// Can only call methods when it's `<Self as TheTrait>::f`.
777777
&& tcx.types.self_param == substs.type_at(0)
778778
{

compiler/rustc_const_eval/src/util/call_kind.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ pub fn call_kind<'tcx>(
6666
from_hir_call: bool,
6767
self_arg: Option<Ident>,
6868
) -> CallKind<'tcx> {
69-
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| match assoc.container {
70-
AssocItemContainer::ImplContainer(impl_did) => tcx.trait_id_of_impl(impl_did),
71-
AssocItemContainer::TraitContainer(trait_did) => Some(trait_did),
69+
let parent = tcx.opt_associated_item(method_did).and_then(|assoc| {
70+
let container_id = assoc.container_id(tcx);
71+
match assoc.container {
72+
AssocItemContainer::ImplContainer => tcx.trait_id_of_impl(container_id),
73+
AssocItemContainer::TraitContainer => Some(container_id),
74+
}
7275
});
7376

7477
let fn_call = parent

compiler/rustc_hir/src/hir.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2222,6 +2222,7 @@ pub struct TraitItem<'hir> {
22222222
pub generics: &'hir Generics<'hir>,
22232223
pub kind: TraitItemKind<'hir>,
22242224
pub span: Span,
2225+
pub defaultness: Defaultness,
22252226
}
22262227

22272228
impl TraitItem<'_> {
@@ -2281,6 +2282,7 @@ pub struct ImplItem<'hir> {
22812282
pub def_id: LocalDefId,
22822283
pub generics: &'hir Generics<'hir>,
22832284
pub kind: ImplItemKind<'hir>,
2285+
pub defaultness: Defaultness,
22842286
pub span: Span,
22852287
pub vis_span: Span,
22862288
}
@@ -3083,7 +3085,6 @@ pub struct TraitItemRef {
30833085
pub ident: Ident,
30843086
pub kind: AssocItemKind,
30853087
pub span: Span,
3086-
pub defaultness: Defaultness,
30873088
}
30883089

30893090
/// A reference from an impl to one of its associated items. This
@@ -3098,7 +3099,6 @@ pub struct ImplItemRef {
30983099
pub ident: Ident,
30993100
pub kind: AssocItemKind,
31003101
pub span: Span,
3101-
pub defaultness: Defaultness,
31023102
/// When we are in a trait impl, link to the trait-item's id.
31033103
pub trait_item_def_id: Option<DefId>,
31043104
}
@@ -3496,11 +3496,11 @@ mod size_asserts {
34963496
rustc_data_structures::static_assert_size!(ForeignItem<'static>, 72);
34973497
rustc_data_structures::static_assert_size!(GenericBound<'_>, 48);
34983498
rustc_data_structures::static_assert_size!(Generics<'static>, 56);
3499-
rustc_data_structures::static_assert_size!(ImplItem<'static>, 80);
3499+
rustc_data_structures::static_assert_size!(ImplItem<'static>, 88);
35003500
rustc_data_structures::static_assert_size!(Impl<'static>, 80);
35013501
rustc_data_structures::static_assert_size!(Item<'static>, 80);
35023502
rustc_data_structures::static_assert_size!(Pat<'static>, 88);
35033503
rustc_data_structures::static_assert_size!(QPath<'static>, 24);
3504-
rustc_data_structures::static_assert_size!(TraitItem<'static>, 88);
3504+
rustc_data_structures::static_assert_size!(TraitItem<'static>, 96);
35053505
rustc_data_structures::static_assert_size!(Ty<'static>, 72);
35063506
}

compiler/rustc_hir/src/intravisit.rs

+23-19
Original file line numberDiff line numberDiff line change
@@ -946,32 +946,30 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
946946
}
947947

948948
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
949-
visitor.visit_ident(trait_item.ident);
950-
visitor.visit_generics(&trait_item.generics);
951-
match trait_item.kind {
949+
// N.B., deliberately force a compilation error if/when new fields are added.
950+
let TraitItem { ident, generics, ref defaultness, ref kind, span, def_id: _ } = *trait_item;
951+
let hir_id = trait_item.hir_id();
952+
visitor.visit_ident(ident);
953+
visitor.visit_generics(&generics);
954+
visitor.visit_defaultness(&defaultness);
955+
match *kind {
952956
TraitItemKind::Const(ref ty, default) => {
953-
visitor.visit_id(trait_item.hir_id());
957+
visitor.visit_id(hir_id);
954958
visitor.visit_ty(ty);
955959
walk_list!(visitor, visit_nested_body, default);
956960
}
957961
TraitItemKind::Fn(ref sig, TraitFn::Required(param_names)) => {
958-
visitor.visit_id(trait_item.hir_id());
962+
visitor.visit_id(hir_id);
959963
visitor.visit_fn_decl(&sig.decl);
960964
for &param_name in param_names {
961965
visitor.visit_ident(param_name);
962966
}
963967
}
964968
TraitItemKind::Fn(ref sig, TraitFn::Provided(body_id)) => {
965-
visitor.visit_fn(
966-
FnKind::Method(trait_item.ident, sig),
967-
&sig.decl,
968-
body_id,
969-
trait_item.span,
970-
trait_item.hir_id(),
971-
);
969+
visitor.visit_fn(FnKind::Method(ident, sig), &sig.decl, body_id, span, hir_id);
972970
}
973971
TraitItemKind::Type(bounds, ref default) => {
974-
visitor.visit_id(trait_item.hir_id());
972+
visitor.visit_id(hir_id);
975973
walk_list!(visitor, visit_param_bound, bounds);
976974
walk_list!(visitor, visit_ty, default);
977975
}
@@ -980,19 +978,27 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
980978

981979
pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
982980
// N.B., deliberately force a compilation error if/when new fields are added.
983-
let TraitItemRef { id, ident, ref kind, span: _, ref defaultness } = *trait_item_ref;
981+
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
984982
visitor.visit_nested_trait_item(id);
985983
visitor.visit_ident(ident);
986984
visitor.visit_associated_item_kind(kind);
987-
visitor.visit_defaultness(defaultness);
988985
}
989986

990987
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
991988
// N.B., deliberately force a compilation error if/when new fields are added.
992-
let ImplItem { def_id: _, ident, ref generics, ref kind, span: _, vis_span: _ } = *impl_item;
989+
let ImplItem {
990+
def_id: _,
991+
ident,
992+
ref generics,
993+
ref kind,
994+
ref defaultness,
995+
span: _,
996+
vis_span: _,
997+
} = *impl_item;
993998

994999
visitor.visit_ident(ident);
9951000
visitor.visit_generics(generics);
1001+
visitor.visit_defaultness(defaultness);
9961002
match *kind {
9971003
ImplItemKind::Const(ref ty, body) => {
9981004
visitor.visit_id(impl_item.hir_id());
@@ -1027,12 +1033,10 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
10271033

10281034
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
10291035
// N.B., deliberately force a compilation error if/when new fields are added.
1030-
let ImplItemRef { id, ident, ref kind, span: _, ref defaultness, trait_item_def_id: _ } =
1031-
*impl_item_ref;
1036+
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
10321037
visitor.visit_nested_impl_item(id);
10331038
visitor.visit_ident(ident);
10341039
visitor.visit_associated_item_kind(kind);
1035-
visitor.visit_defaultness(defaultness);
10361040
}
10371041

10381042
pub fn walk_struct_def<'v, V: Visitor<'v>>(

compiler/rustc_incremental/src/persist/dirty_clean.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ const BASE_STRUCT: &[&str] =
8080
/// Extra `DepNode`s for functions and methods.
8181
const EXTRA_ASSOCIATED: &[&str] = &[label_strs::associated_item];
8282

83-
const EXTRA_TRAIT: &[&str] = &[label_strs::trait_of_item];
83+
const EXTRA_TRAIT: &[&str] = &[];
8484

8585
// Fully Built Labels
8686

compiler/rustc_infer/src/infer/error_reporting/nice_region_error/static_impl_trait.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ impl<'a, 'tcx> NiceRegionError<'a, 'tcx> {
7676
"...is used and required to live as long as `'static` here \
7777
because of an implicit lifetime bound on the {}",
7878
match ctxt.assoc_item.container {
79-
AssocItemContainer::TraitContainer(id) =>
80-
format!("`impl` of `{}`", tcx.def_path_str(id)),
81-
AssocItemContainer::ImplContainer(_) =>
82-
"inherent `impl`".to_string(),
79+
AssocItemContainer::TraitContainer => {
80+
let id = ctxt.assoc_item.container_id(tcx);
81+
format!("`impl` of `{}`", tcx.def_path_str(id))
82+
}
83+
AssocItemContainer::ImplContainer => "inherent `impl`".to_string(),
8384
},
8485
),
8586
);

compiler/rustc_lint/src/nonstandard_style.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ pub fn method_context(cx: &LateContext<'_>, id: hir::HirId) -> MethodLateContext
2222
let def_id = cx.tcx.hir().local_def_id(id);
2323
let item = cx.tcx.associated_item(def_id);
2424
match item.container {
25-
ty::TraitContainer(..) => MethodLateContext::TraitAutoImpl,
26-
ty::ImplContainer(cid) => match cx.tcx.impl_trait_ref(cid) {
25+
ty::TraitContainer => MethodLateContext::TraitAutoImpl,
26+
ty::ImplContainer => match cx.tcx.impl_trait_ref(item.container_id(cx.tcx)) {
2727
Some(_) => MethodLateContext::TraitImpl,
2828
None => MethodLateContext::PlainImpl,
2929
},

compiler/rustc_metadata/src/rmeta/decoder.rs

+4-24
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11141114

11151115
fn get_fn_has_self_parameter(self, id: DefIndex) -> bool {
11161116
match self.kind(id) {
1117-
EntryKind::AssocFn(data) => data.decode(self).has_self,
1117+
EntryKind::AssocFn { has_self, .. } => has_self,
11181118
_ => false,
11191119
}
11201120
}
@@ -1134,28 +1134,21 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11341134
}
11351135

11361136
fn get_associated_item(self, id: DefIndex) -> ty::AssocItem {
1137-
let def_key = self.def_key(id);
1138-
let parent = self.local_def_id(def_key.parent.unwrap());
11391137
let name = self.item_name(id);
11401138

11411139
let (kind, container, has_self) = match self.kind(id) {
11421140
EntryKind::AssocConst(container) => (ty::AssocKind::Const, container, false),
1143-
EntryKind::AssocFn(data) => {
1144-
let data = data.decode(self);
1145-
(ty::AssocKind::Fn, data.container, data.has_self)
1146-
}
1141+
EntryKind::AssocFn { container, has_self } => (ty::AssocKind::Fn, container, has_self),
11471142
EntryKind::AssocType(container) => (ty::AssocKind::Type, container, false),
1148-
_ => bug!("cannot get associated-item of `{:?}`", def_key),
1143+
_ => bug!("cannot get associated-item of `{:?}`", id),
11491144
};
11501145

11511146
ty::AssocItem {
11521147
name,
11531148
kind,
1154-
vis: self.get_visibility(id),
1155-
defaultness: container.defaultness(),
11561149
def_id: self.local_def_id(id),
11571150
trait_item_def_id: self.get_trait_item_def_id(id),
1158-
container: container.with_def_id(parent),
1151+
container,
11591152
fn_has_self_parameter: has_self,
11601153
}
11611154
}
@@ -1310,19 +1303,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13101303
}
13111304
}
13121305

1313-
fn get_trait_of_item(self, id: DefIndex) -> Option<DefId> {
1314-
let def_key = self.def_key(id);
1315-
match def_key.disambiguated_data.data {
1316-
DefPathData::TypeNs(..) | DefPathData::ValueNs(..) => (),
1317-
// Not an associated item
1318-
_ => return None,
1319-
}
1320-
def_key.parent.and_then(|parent_index| match self.kind(parent_index) {
1321-
EntryKind::Trait | EntryKind::TraitAlias => Some(self.local_def_id(parent_index)),
1322-
_ => None,
1323-
})
1324-
}
1325-
13261306
fn get_native_libraries(self, sess: &'a Session) -> impl Iterator<Item = NativeLib> + 'a {
13271307
self.root.native_libraries.decode((self, sess))
13281308
}

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ provide! { <'tcx> tcx, def_id, other, cdata,
235235
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
236236
is_foreign_item => { cdata.is_foreign_item(def_id.index) }
237237
item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
238-
trait_of_item => { cdata.get_trait_of_item(def_id.index) }
239238
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
240239
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
241240

0 commit comments

Comments
 (0)