Skip to content

Commit 110f065

Browse files
committed
Store associated item defaultness in impl_defaultness.
1 parent c9e134e commit 110f065

File tree

25 files changed

+89
-107
lines changed

25 files changed

+89
-107
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 2 additions & 3 deletions
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

Lines changed: 18 additions & 19 deletions
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_hir/src/hir.rs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -948,6 +948,7 @@ pub fn walk_fn<'v, V: Visitor<'v>>(
948948
pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v TraitItem<'v>) {
949949
visitor.visit_ident(trait_item.ident);
950950
visitor.visit_generics(&trait_item.generics);
951+
visitor.visit_defaultness(&trait_item.defaultness);
951952
match trait_item.kind {
952953
TraitItemKind::Const(ref ty, default) => {
953954
visitor.visit_id(trait_item.hir_id());
@@ -980,19 +981,27 @@ pub fn walk_trait_item<'v, V: Visitor<'v>>(visitor: &mut V, trait_item: &'v Trai
980981

981982
pub fn walk_trait_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, trait_item_ref: &'v TraitItemRef) {
982983
// 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;
984+
let TraitItemRef { id, ident, ref kind, span: _ } = *trait_item_ref;
984985
visitor.visit_nested_trait_item(id);
985986
visitor.visit_ident(ident);
986987
visitor.visit_associated_item_kind(kind);
987-
visitor.visit_defaultness(defaultness);
988988
}
989989

990990
pub fn walk_impl_item<'v, V: Visitor<'v>>(visitor: &mut V, impl_item: &'v ImplItem<'v>) {
991991
// 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;
992+
let ImplItem {
993+
def_id: _,
994+
ident,
995+
ref generics,
996+
ref kind,
997+
ref defaultness,
998+
span: _,
999+
vis_span: _,
1000+
} = *impl_item;
9931001

9941002
visitor.visit_ident(ident);
9951003
visitor.visit_generics(generics);
1004+
visitor.visit_defaultness(defaultness);
9961005
match *kind {
9971006
ImplItemKind::Const(ref ty, body) => {
9981007
visitor.visit_id(impl_item.hir_id());
@@ -1027,12 +1036,10 @@ pub fn walk_foreign_item_ref<'v, V: Visitor<'v>>(
10271036

10281037
pub fn walk_impl_item_ref<'v, V: Visitor<'v>>(visitor: &mut V, impl_item_ref: &'v ImplItemRef) {
10291038
// 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;
1039+
let ImplItemRef { id, ident, ref kind, span: _, trait_item_def_id: _ } = *impl_item_ref;
10321040
visitor.visit_nested_impl_item(id);
10331041
visitor.visit_ident(ident);
10341042
visitor.visit_associated_item_kind(kind);
1035-
visitor.visit_defaultness(defaultness);
10361043
}
10371044

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

compiler/rustc_metadata/src/rmeta/decoder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1152,7 +1152,6 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
11521152
name,
11531153
kind,
11541154
vis: self.get_visibility(id),
1155-
defaultness: container.defaultness(),
11561155
def_id: self.local_def_id(id),
11571156
trait_item_def_id: self.get_trait_item_def_id(id),
11581157
container: container.with_def_id(parent),

compiler/rustc_metadata/src/rmeta/encoder.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,22 +1212,17 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12121212
let tcx = self.tcx;
12131213

12141214
let ast_item = tcx.hir().expect_trait_item(def_id.expect_local());
1215+
self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness);
12151216
let trait_item = tcx.associated_item(def_id);
12161217

1217-
let container = match trait_item.defaultness {
1218-
hir::Defaultness::Default { has_value: true } => AssocContainer::TraitWithDefault,
1219-
hir::Defaultness::Default { has_value: false } => AssocContainer::TraitRequired,
1220-
hir::Defaultness::Final => span_bug!(ast_item.span, "traits cannot have final items"),
1221-
};
1222-
12231218
match trait_item.kind {
12241219
ty::AssocKind::Const => {
12251220
let rendered = rustc_hir_pretty::to_string(
12261221
&(&self.tcx.hir() as &dyn intravisit::Map<'_>),
12271222
|s| s.print_trait_item(ast_item),
12281223
);
12291224

1230-
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container));
1225+
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Trait));
12311226
record!(self.tables.mir_const_qualif[def_id] <- mir::ConstQualifs::default());
12321227
record!(self.tables.rendered_const[def_id] <- rendered);
12331228
}
@@ -1244,21 +1239,21 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12441239
self.tables.asyncness.set(def_id.index, m_sig.header.asyncness);
12451240
self.tables.constness.set(def_id.index, hir::Constness::NotConst);
12461241
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1247-
container,
1242+
container:AssocContainer::Trait,
12481243
has_self: trait_item.fn_has_self_parameter,
12491244
})));
12501245
}
12511246
ty::AssocKind::Type => {
12521247
self.encode_explicit_item_bounds(def_id);
1253-
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
1248+
record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Trait));
12541249
}
12551250
}
12561251
match trait_item.kind {
12571252
ty::AssocKind::Const | ty::AssocKind::Fn => {
12581253
self.encode_item_type(def_id);
12591254
}
12601255
ty::AssocKind::Type => {
1261-
if trait_item.defaultness.has_value() {
1256+
if ast_item.defaultness.has_value() {
12621257
self.encode_item_type(def_id);
12631258
}
12641259
}
@@ -1273,23 +1268,16 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
12731268
let tcx = self.tcx;
12741269

12751270
let ast_item = self.tcx.hir().expect_impl_item(def_id.expect_local());
1271+
self.tables.impl_defaultness.set(def_id.index, ast_item.defaultness);
12761272
let impl_item = self.tcx.associated_item(def_id);
12771273

1278-
let container = match impl_item.defaultness {
1279-
hir::Defaultness::Default { has_value: true } => AssocContainer::ImplDefault,
1280-
hir::Defaultness::Final => AssocContainer::ImplFinal,
1281-
hir::Defaultness::Default { has_value: false } => {
1282-
span_bug!(ast_item.span, "impl items always have values (currently)")
1283-
}
1284-
};
1285-
12861274
match impl_item.kind {
12871275
ty::AssocKind::Const => {
12881276
if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind {
12891277
let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
12901278
let const_data = self.encode_rendered_const_for_body(body_id);
12911279

1292-
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(container));
1280+
record!(self.tables.kind[def_id] <- EntryKind::AssocConst(AssocContainer::Impl));
12931281
record!(self.tables.mir_const_qualif[def_id] <- qualifs);
12941282
record!(self.tables.rendered_const[def_id] <- const_data);
12951283
} else {
@@ -1308,12 +1296,12 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
13081296
};
13091297
self.tables.constness.set(def_id.index, constness);
13101298
record!(self.tables.kind[def_id] <- EntryKind::AssocFn(self.lazy(AssocFnData {
1311-
container,
1299+
container:AssocContainer::Impl,
13121300
has_self: impl_item.fn_has_self_parameter,
13131301
})));
13141302
}
13151303
ty::AssocKind::Type => {
1316-
record!(self.tables.kind[def_id] <- EntryKind::AssocType(container));
1304+
record!(self.tables.kind[def_id] <- EntryKind::AssocType(AssocContainer::Impl));
13171305
}
13181306
}
13191307
self.encode_item_type(def_id);

compiler/rustc_metadata/src/rmeta/mod.rs

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -439,32 +439,15 @@ struct VariantData {
439439
/// a default, or an in impl, whether it's marked "default".
440440
#[derive(Copy, Clone, TyEncodable, TyDecodable)]
441441
enum AssocContainer {
442-
TraitRequired,
443-
TraitWithDefault,
444-
ImplDefault,
445-
ImplFinal,
442+
Trait,
443+
Impl,
446444
}
447445

448446
impl AssocContainer {
449447
fn with_def_id(&self, def_id: DefId) -> ty::AssocItemContainer {
450448
match *self {
451-
AssocContainer::TraitRequired | AssocContainer::TraitWithDefault => {
452-
ty::TraitContainer(def_id)
453-
}
454-
455-
AssocContainer::ImplDefault | AssocContainer::ImplFinal => ty::ImplContainer(def_id),
456-
}
457-
}
458-
459-
fn defaultness(&self) -> hir::Defaultness {
460-
match *self {
461-
AssocContainer::TraitRequired => hir::Defaultness::Default { has_value: false },
462-
463-
AssocContainer::TraitWithDefault | AssocContainer::ImplDefault => {
464-
hir::Defaultness::Default { has_value: true }
465-
}
466-
467-
AssocContainer::ImplFinal => hir::Defaultness::Final,
449+
AssocContainer::Trait => ty::TraitContainer(def_id),
450+
AssocContainer::Impl => ty::ImplContainer(def_id),
468451
}
469452
}
470453
}

compiler/rustc_middle/src/traits/specialization_graph.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ impl<'tcx> Ancestors<'tcx> {
217217
self.find_map(|node| {
218218
if let Some(item) = node.item(tcx, trait_item_def_id) {
219219
if finalizing_node.is_none() {
220-
let is_specializable = item.defaultness.is_default()
220+
let is_specializable = item.defaultness(tcx).is_default()
221221
|| tcx.impl_defaultness(node.def_id()).is_default();
222222

223223
if !is_specializable {

compiler/rustc_middle/src/ty/assoc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ pub struct AssocItem {
4747
pub name: Symbol,
4848
pub kind: AssocKind,
4949
pub vis: Visibility,
50-
pub defaultness: hir::Defaultness,
5150
pub container: AssocItemContainer,
5251

5352
/// If this is an item in an impl of a trait then this is the `DefId` of
@@ -64,6 +63,10 @@ impl AssocItem {
6463
Ident::new(self.name, tcx.def_ident_span(self.def_id).unwrap())
6564
}
6665

66+
pub fn defaultness(&self, tcx: TyCtxt<'_>) -> hir::Defaultness {
67+
tcx.impl_defaultness(self.def_id)
68+
}
69+
6770
pub fn signature(&self, tcx: TyCtxt<'_>) -> String {
6871
match self.kind {
6972
ty::AssocKind::Fn => {

0 commit comments

Comments
 (0)