Skip to content

Commit b7463e8

Browse files
committed
Auto merge of #103578 - petrochenkov:nofict, r=nagisa
Unreserve braced enum variants in value namespace With this PR braced enum variants (`enum E { V { /*...*/ } }`) no longer take a slot in value namespace, so the special case mentioned in the note in https://github.com/rust-lang/rfcs/blob/master/text/1506-adt-kinds.md#braced-structs is removed. Report - #103578 (comment).
2 parents a78c9be + 7a5376d commit b7463e8

File tree

71 files changed

+364
-642
lines changed

Some content is hidden

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

71 files changed

+364
-642
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2675,7 +2675,7 @@ impl VariantData {
26752675
}
26762676

26772677
/// Return the `NodeId` of this variant's constructor, if it has one.
2678-
pub fn ctor_id(&self) -> Option<NodeId> {
2678+
pub fn ctor_node_id(&self) -> Option<NodeId> {
26792679
match *self {
26802680
VariantData::Struct(..) => None,
26812681
VariantData::Tuple(_, id) | VariantData::Unit(id) => Some(id),

compiler/rustc_borrowck/src/diagnostics/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,7 +350,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
350350
} else {
351351
def.non_enum_variant()
352352
};
353-
if !including_tuple_field.0 && variant.ctor_kind == CtorKind::Fn {
353+
if !including_tuple_field.0 && variant.ctor_kind() == Some(CtorKind::Fn) {
354354
return None;
355355
}
356356
Some(variant.fields[field.index()].name.to_string())

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ fn build_struct_type_di_node<'ll, 'tcx>(
998998
.iter()
999999
.enumerate()
10001000
.map(|(i, f)| {
1001-
let field_name = if variant_def.ctor_kind == CtorKind::Fn {
1001+
let field_name = if variant_def.ctor_kind() == Some(CtorKind::Fn) {
10021002
// This is a tuple struct
10031003
tuple_field_name(i)
10041004
} else {

compiler/rustc_codegen_llvm/src/debuginfo/metadata/enums/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ fn build_enum_variant_struct_type_di_node<'ll, 'tcx>(
269269
|cx, struct_type_di_node| {
270270
(0..variant_layout.fields.count())
271271
.map(|field_index| {
272-
let field_name = if variant_def.ctor_kind != CtorKind::Fn {
272+
let field_name = if variant_def.ctor_kind() != Some(CtorKind::Fn) {
273273
// Fields have names
274274
Cow::from(variant_def.fields[field_index].name.as_str())
275275
} else {

compiler/rustc_hir/src/def.rs

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@ pub enum CtorKind {
2828
Fn,
2929
/// Constructor constant automatically created by a unit struct/variant.
3030
Const,
31-
/// Unusable name in value namespace created by a struct variant.
32-
Fictive,
3331
}
3432

3533
/// An attribute that is not a macro; e.g., `#[inline]` or `#[rustfmt::skip]`.
@@ -132,13 +130,9 @@ impl DefKind {
132130
DefKind::Variant => "variant",
133131
DefKind::Ctor(CtorOf::Variant, CtorKind::Fn) => "tuple variant",
134132
DefKind::Ctor(CtorOf::Variant, CtorKind::Const) => "unit variant",
135-
DefKind::Ctor(CtorOf::Variant, CtorKind::Fictive) => "struct variant",
136133
DefKind::Struct => "struct",
137134
DefKind::Ctor(CtorOf::Struct, CtorKind::Fn) => "tuple struct",
138135
DefKind::Ctor(CtorOf::Struct, CtorKind::Const) => "unit struct",
139-
DefKind::Ctor(CtorOf::Struct, CtorKind::Fictive) => {
140-
panic!("impossible struct constructor")
141-
}
142136
DefKind::OpaqueTy => "opaque type",
143137
DefKind::ImplTraitPlaceholder => "opaque type in trait",
144138
DefKind::TyAlias => "type alias",
@@ -562,19 +556,11 @@ impl<T> PerNS<Option<T>> {
562556
}
563557

564558
impl CtorKind {
565-
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
566-
match *vdata {
567-
ast::VariantData::Tuple(..) => CtorKind::Fn,
568-
ast::VariantData::Unit(..) => CtorKind::Const,
569-
ast::VariantData::Struct(..) => CtorKind::Fictive,
570-
}
571-
}
572-
573-
pub fn from_hir(vdata: &hir::VariantData<'_>) -> CtorKind {
559+
pub fn from_ast(vdata: &ast::VariantData) -> Option<(CtorKind, NodeId)> {
574560
match *vdata {
575-
hir::VariantData::Tuple(..) => CtorKind::Fn,
576-
hir::VariantData::Unit(..) => CtorKind::Const,
577-
hir::VariantData::Struct(..) => CtorKind::Fictive,
561+
ast::VariantData::Tuple(_, node_id) => Some((CtorKind::Fn, node_id)),
562+
ast::VariantData::Unit(node_id) => Some((CtorKind::Const, node_id)),
563+
ast::VariantData::Struct(..) => None,
578564
}
579565
}
580566
}

compiler/rustc_hir/src/hir.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,20 +2913,29 @@ impl<'hir> VariantData<'hir> {
29132913
}
29142914
}
29152915

2916-
/// Return the `LocalDefId` of this variant's constructor, if it has one.
2917-
pub fn ctor_def_id(&self) -> Option<LocalDefId> {
2916+
pub fn ctor(&self) -> Option<(CtorKind, HirId, LocalDefId)> {
29182917
match *self {
2919-
VariantData::Struct(_, _) => None,
2920-
VariantData::Tuple(_, _, def_id) | VariantData::Unit(_, def_id) => Some(def_id),
2918+
VariantData::Tuple(_, hir_id, def_id) => Some((CtorKind::Fn, hir_id, def_id)),
2919+
VariantData::Unit(hir_id, def_id) => Some((CtorKind::Const, hir_id, def_id)),
2920+
VariantData::Struct(..) => None,
29212921
}
29222922
}
29232923

2924+
#[inline]
2925+
pub fn ctor_kind(&self) -> Option<CtorKind> {
2926+
self.ctor().map(|(kind, ..)| kind)
2927+
}
2928+
29242929
/// Return the `HirId` of this variant's constructor, if it has one.
2930+
#[inline]
29252931
pub fn ctor_hir_id(&self) -> Option<HirId> {
2926-
match *self {
2927-
VariantData::Struct(_, _) => None,
2928-
VariantData::Tuple(_, hir_id, _) | VariantData::Unit(hir_id, _) => Some(hir_id),
2929-
}
2932+
self.ctor().map(|(_, hir_id, _)| hir_id)
2933+
}
2934+
2935+
/// Return the `LocalDefId` of this variant's constructor, if it has one.
2936+
#[inline]
2937+
pub fn ctor_def_id(&self) -> Option<LocalDefId> {
2938+
self.ctor().map(|(.., def_id)| def_id)
29302939
}
29312940
}
29322941

compiler/rustc_hir_analysis/src/check/check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1165,7 +1165,7 @@ fn check_enum<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) {
11651165
}
11661166

11671167
if def.repr().int.is_none() {
1168-
let is_unit = |var: &ty::VariantDef| matches!(var.ctor_kind, CtorKind::Const);
1168+
let is_unit = |var: &ty::VariantDef| matches!(var.ctor_kind(), Some(CtorKind::Const));
11691169
let has_disr = |var: &ty::VariantDef| matches!(var.discr, ty::VariantDiscr::Explicit(_));
11701170

11711171
let has_non_units = def.variants().iter().any(|var| !is_unit(var));

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use rustc_data_structures::captures::Captures;
2424
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
2525
use rustc_errors::{struct_span_err, Applicability, DiagnosticBuilder, ErrorGuaranteed, StashKey};
2626
use rustc_hir as hir;
27-
use rustc_hir::def::CtorKind;
2827
use rustc_hir::def_id::{DefId, LocalDefId, LOCAL_CRATE};
2928
use rustc_hir::intravisit::{self, Visitor};
3029
use rustc_hir::weak_lang_items::WEAK_LANG_ITEMS;
@@ -794,7 +793,7 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
794793

795794
// Convert the ctor, if any. This also registers the variant as
796795
// an item.
797-
if let Some(ctor_def_id) = variant.ctor_def_id {
796+
if let Some(ctor_def_id) = variant.ctor_def_id() {
798797
convert_variant_ctor(tcx, ctor_def_id.expect_local());
799798
}
800799
}
@@ -803,7 +802,6 @@ fn convert_enum_variant_types(tcx: TyCtxt<'_>, def_id: DefId) {
803802
fn convert_variant(
804803
tcx: TyCtxt<'_>,
805804
variant_did: Option<LocalDefId>,
806-
ctor_did: Option<LocalDefId>,
807805
ident: Ident,
808806
discr: ty::VariantDiscr,
809807
def: &hir::VariantData<'_>,
@@ -840,10 +838,9 @@ fn convert_variant(
840838
ty::VariantDef::new(
841839
ident.name,
842840
variant_did.map(LocalDefId::to_def_id),
843-
ctor_did.map(LocalDefId::to_def_id),
841+
def.ctor().map(|(kind, _, def_id)| (kind, def_id.to_def_id())),
844842
discr,
845843
fields,
846-
CtorKind::from_hir(def),
847844
adt_kind,
848845
parent_did.to_def_id(),
849846
recovered,
@@ -882,7 +879,6 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
882879
convert_variant(
883880
tcx,
884881
Some(v.def_id),
885-
v.data.ctor_def_id(),
886882
v.ident,
887883
discr,
888884
&v.data,
@@ -894,35 +890,23 @@ fn adt_def<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> ty::AdtDef<'tcx> {
894890

895891
(AdtKind::Enum, variants)
896892
}
897-
ItemKind::Struct(ref def, _) => {
898-
let variants = std::iter::once(convert_variant(
899-
tcx,
900-
None,
901-
def.ctor_def_id(),
902-
item.ident,
903-
ty::VariantDiscr::Relative(0),
904-
def,
905-
AdtKind::Struct,
906-
def_id,
907-
))
908-
.collect();
909-
910-
(AdtKind::Struct, variants)
911-
}
912-
ItemKind::Union(ref def, _) => {
893+
ItemKind::Struct(ref def, _) | ItemKind::Union(ref def, _) => {
894+
let adt_kind = match item.kind {
895+
ItemKind::Struct(..) => AdtKind::Struct,
896+
_ => AdtKind::Union,
897+
};
913898
let variants = std::iter::once(convert_variant(
914899
tcx,
915900
None,
916-
def.ctor_def_id(),
917901
item.ident,
918902
ty::VariantDiscr::Relative(0),
919903
def,
920-
AdtKind::Union,
904+
adt_kind,
921905
def_id,
922906
))
923907
.collect();
924908

925-
(AdtKind::Union, variants)
909+
(adt_kind, variants)
926910
}
927911
_ => bug!(),
928912
};
@@ -1171,7 +1155,7 @@ fn fn_sig(tcx: TyCtxt<'_>, def_id: DefId) -> ty::PolyFnSig<'_> {
11711155
compute_sig_of_foreign_fn_decl(tcx, def_id.to_def_id(), fn_decl, abi)
11721156
}
11731157

1174-
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor_hir_id().is_some() => {
1158+
Ctor(data) | Variant(hir::Variant { data, .. }) if data.ctor().is_some() => {
11751159
let ty = tcx.type_of(tcx.hir().get_parent_item(hir_id));
11761160
let inputs = data.fields().iter().map(|f| tcx.type_of(f.def_id));
11771161
ty::Binder::dummy(tcx.mk_fn_sig(

compiler/rustc_hir_analysis/src/variance/constraints.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ pub fn add_constraints_from_crate<'a, 'tcx>(
7272

7373
let adt = tcx.adt_def(def_id);
7474
for variant in adt.variants() {
75-
if let Some(ctor) = variant.ctor_def_id {
76-
constraint_cx.build_constraints_for_item(ctor.expect_local());
75+
if let Some(ctor_def_id) = variant.ctor_def_id() {
76+
constraint_cx.build_constraints_for_item(ctor_def_id.expect_local());
7777
}
7878
}
7979
}

compiler/rustc_hir_analysis/src/variance/terms.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ pub fn determine_parameters_to_be_inferred<'a, 'tcx>(
9191

9292
let adt = tcx.adt_def(def_id);
9393
for variant in adt.variants() {
94-
if let Some(ctor) = variant.ctor_def_id {
95-
terms_cx.add_inferreds_for_item(ctor.expect_local());
94+
if let Some(ctor_def_id) = variant.ctor_def_id() {
95+
terms_cx.add_inferreds_for_item(ctor_def_id.expect_local());
9696
}
9797
}
9898
}

0 commit comments

Comments
 (0)