Skip to content

Commit 75d6522

Browse files
committed
Eliminate ty::VariantKind in favor of def::CtorKind
1 parent 64bdf1b commit 75d6522

File tree

16 files changed

+73
-104
lines changed

16 files changed

+73
-104
lines changed

src/librustc/hir/def.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,20 @@ pub struct Export {
105105
}
106106

107107
impl CtorKind {
108-
pub fn from_vdata(vdata: &ast::VariantData) -> CtorKind {
108+
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
109109
match *vdata {
110110
ast::VariantData::Tuple(..) => CtorKind::Fn,
111111
ast::VariantData::Unit(..) => CtorKind::Const,
112112
ast::VariantData::Struct(..) => CtorKind::Fictive,
113113
}
114114
}
115+
pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
116+
match *vdata {
117+
hir::VariantData::Tuple(..) => CtorKind::Fn,
118+
hir::VariantData::Unit(..) => CtorKind::Const,
119+
hir::VariantData::Struct(..) => CtorKind::Fictive,
120+
}
121+
}
115122
}
116123

117124
impl Def {

src/librustc/middle/cstore.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,6 @@ pub trait CrateStore<'tcx> {
201201
-> Option<DefIndex>;
202202
fn def_key(&self, def: DefId) -> hir_map::DefKey;
203203
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
204-
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>;
205204
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
206205
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
207206
fn item_children(&self, did: DefId) -> Vec<def::Export>;
@@ -378,7 +377,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
378377
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
379378
bug!("relative_def_path")
380379
}
381-
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind> { bug!("variant_kind") }
382380
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
383381
{ bug!("struct_ctor_def_id") }
384382
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }

src/librustc/mir/repr.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
1515
use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
1616
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
1717
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
18+
use hir::def::CtorKind;
1819
use hir::def_id::DefId;
1920
use ty::subst::Substs;
2021
use ty::{self, AdtDef, ClosureSubsts, Region, Ty};
@@ -1140,10 +1141,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
11401141
ppaux::parameterized(fmt, substs, variant_def.did,
11411142
ppaux::Ns::Value, &[])?;
11421143

1143-
match variant_def.kind {
1144-
ty::VariantKind::Unit => Ok(()),
1145-
ty::VariantKind::Tuple => fmt_tuple(fmt, lvs),
1146-
ty::VariantKind::Struct => {
1144+
match variant_def.ctor_kind {
1145+
CtorKind::Const => Ok(()),
1146+
CtorKind::Fn => fmt_tuple(fmt, lvs),
1147+
CtorKind::Fictive => {
11471148
let mut struct_fmt = fmt.debug_struct("");
11481149
for (field, lv) in variant_def.fields.iter().zip(lvs) {
11491150
struct_fmt.field(&field.name.as_str(), lv);

src/librustc/ty/mod.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,7 @@ pub struct VariantDefData<'tcx, 'container: 'tcx> {
14201420
pub name: Name, // struct's name if this is a struct
14211421
pub disr_val: Disr,
14221422
pub fields: Vec<FieldDefData<'tcx, 'container>>,
1423-
pub kind: VariantKind,
1423+
pub ctor_kind: CtorKind,
14241424
}
14251425

14261426
pub struct FieldDefData<'tcx, 'container: 'tcx> {
@@ -1485,26 +1485,6 @@ impl<'tcx> serialize::UseSpecializedDecodable for AdtDef<'tcx> {}
14851485
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14861486
pub enum AdtKind { Struct, Union, Enum }
14871487

1488-
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
1489-
pub enum VariantKind { Struct, Tuple, Unit }
1490-
1491-
impl VariantKind {
1492-
pub fn from_variant_data(vdata: &hir::VariantData) -> Self {
1493-
match *vdata {
1494-
hir::VariantData::Struct(..) => VariantKind::Struct,
1495-
hir::VariantData::Tuple(..) => VariantKind::Tuple,
1496-
hir::VariantData::Unit(..) => VariantKind::Unit,
1497-
}
1498-
}
1499-
pub fn ctor_kind(self) -> CtorKind {
1500-
match self {
1501-
VariantKind::Tuple => CtorKind::Fn,
1502-
VariantKind::Unit => CtorKind::Const,
1503-
VariantKind::Struct => CtorKind::Fictive,
1504-
}
1505-
}
1506-
}
1507-
15081488
impl<'a, 'gcx, 'tcx, 'container> AdtDefData<'gcx, 'container> {
15091489
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
15101490
did: DefId,

src/librustc_const_eval/check_match.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
247247
if edef.is_enum() {
248248
if let Def::Local(..) = cx.tcx.expect_def(p.id) {
249249
if edef.variants.iter().any(|variant| {
250-
variant.name == name.node && variant.kind == ty::VariantKind::Unit
250+
variant.name == name.node && variant.ctor_kind == CtorKind::Const
251251
}) {
252252
let ty_path = cx.tcx.item_path_str(edef.did);
253253
let mut err = struct_span_warn!(cx.tcx.sess, p.span, E0170,
@@ -577,8 +577,8 @@ fn construct_witness<'a,'tcx>(cx: &MatchCheckCtxt<'a,'tcx>, ctor: &Constructor,
577577

578578
ty::TyAdt(adt, _) => {
579579
let v = ctor.variant_for_adt(adt);
580-
match v.kind {
581-
ty::VariantKind::Struct => {
580+
match v.ctor_kind {
581+
CtorKind::Fictive => {
582582
let field_pats: hir::HirVec<_> = v.fields.iter()
583583
.zip(pats)
584584
.filter(|&(_, ref pat)| pat.node != PatKind::Wild)
@@ -593,10 +593,10 @@ fn construct_witness<'a,'tcx>(cx: &MatchCheckCtxt<'a,'tcx>, ctor: &Constructor,
593593
let has_more_fields = field_pats.len() < pats_len;
594594
PatKind::Struct(def_to_path(cx.tcx, v.did), field_pats, has_more_fields)
595595
}
596-
ty::VariantKind::Tuple => {
596+
CtorKind::Fn => {
597597
PatKind::TupleStruct(def_to_path(cx.tcx, v.did), pats.collect(), None)
598598
}
599-
ty::VariantKind::Unit => {
599+
CtorKind::Const => {
600600
PatKind::Path(None, def_to_path(cx.tcx, v.did))
601601
}
602602
}

src/librustc_metadata/csearch.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -342,12 +342,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
342342
self.get_crate_data(def.krate).def_path(def.index)
343343
}
344344

345-
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>
346-
{
347-
self.dep_graph.read(DepNode::MetaData(def_id));
348-
self.get_crate_data(def_id.krate).get_variant_kind(def_id.index)
349-
}
350-
351345
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
352346
{
353347
self.dep_graph.read(DepNode::MetaData(struct_def_id));

src/librustc_metadata/decoder.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use rustc::hir;
2222
use rustc::hir::intravisit::IdRange;
2323

2424
use rustc::middle::cstore::{InlinedItem, LinkagePreference};
25-
use rustc::hir::def::{self, Def};
25+
use rustc::hir::def::{self, Def, CtorKind};
2626
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
2727
use rustc::middle::lang_items;
2828
use rustc::ty::{self, Ty, TyCtxt};
@@ -534,7 +534,7 @@ impl<'a, 'tcx> CrateMetadata {
534534
name: self.item_name(item),
535535
fields: fields,
536536
disr_val: ConstInt::Infer(data.disr),
537-
kind: data.kind,
537+
ctor_kind: data.ctor_kind,
538538
}, data.struct_ctor)
539539
}
540540

@@ -693,16 +693,16 @@ impl<'a, 'tcx> CrateMetadata {
693693
match def {
694694
Def::Struct(..) => {
695695
if let Some(ctor_def_id) = self.get_struct_ctor_def_id(child_index) {
696-
let vkind = self.get_variant_kind(child_index).unwrap();
697-
let ctor_def = Def::StructCtor(ctor_def_id, vkind.ctor_kind());
696+
let ctor_kind = self.get_ctor_kind(child_index);
697+
let ctor_def = Def::StructCtor(ctor_def_id, ctor_kind);
698698
callback(def::Export { def: ctor_def, name: name });
699699
}
700700
}
701701
Def::Variant(def_id) => {
702702
// Braced variants, unlike structs, generate unusable names in
703703
// value namespace, they are reserved for possible future use.
704-
let vkind = self.get_variant_kind(child_index).unwrap();
705-
let ctor_def = Def::VariantCtor(def_id, vkind.ctor_kind());
704+
let ctor_kind = self.get_ctor_kind(child_index);
705+
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
706706
callback(def::Export { def: ctor_def, name: name });
707707
}
708708
_ => {}
@@ -806,12 +806,12 @@ impl<'a, 'tcx> CrateMetadata {
806806
self.entry(id).variances.decode(self).collect()
807807
}
808808

809-
pub fn get_variant_kind(&self, node_id: DefIndex) -> Option<ty::VariantKind> {
809+
pub fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
810810
match self.entry(node_id).kind {
811811
EntryKind::Struct(data) |
812812
EntryKind::Union(data) |
813-
EntryKind::Variant(data) => Some(data.decode(self).kind),
814-
_ => None
813+
EntryKind::Variant(data) => data.decode(self).ctor_kind,
814+
_ => CtorKind::Fictive,
815815
}
816816
}
817817

src/librustc_metadata/encoder.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
258258
let def_id = variant.did;
259259

260260
let data = VariantData {
261-
kind: variant.kind,
261+
ctor_kind: variant.ctor_kind,
262262
disr: variant.disr_val.to_u64_unchecked(),
263263
struct_ctor: None
264264
};
@@ -410,7 +410,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
410410
let variant = tcx.lookup_adt_def(adt_def_id).struct_variant();
411411

412412
let data = VariantData {
413-
kind: variant.kind,
413+
ctor_kind: variant.ctor_kind,
414414
disr: variant.disr_val.to_u64_unchecked(),
415415
struct_ctor: Some(def_id.index)
416416
};
@@ -675,7 +675,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
675675
None
676676
};
677677
EntryKind::Struct(self.lazy(&VariantData {
678-
kind: variant.kind,
678+
ctor_kind: variant.ctor_kind,
679679
disr: variant.disr_val.to_u64_unchecked(),
680680
struct_ctor: struct_ctor
681681
}))
@@ -684,7 +684,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
684684
let variant = tcx.lookup_adt_def(def_id).struct_variant();
685685

686686
EntryKind::Union(self.lazy(&VariantData {
687-
kind: variant.kind,
687+
ctor_kind: variant.ctor_kind,
688688
disr: variant.disr_val.to_u64_unchecked(),
689689
struct_ctor: None
690690
}))
@@ -889,19 +889,12 @@ impl<'a, 'b, 'tcx> IndexBuilder<'a, 'b, 'tcx> {
889889
hir::ItemStruct(ref struct_def, _) => {
890890
self.encode_fields(def_id);
891891

892-
// If this is a tuple-like struct, encode the type of the constructor.
893-
match self.tcx.lookup_adt_def(def_id).struct_variant().kind {
894-
ty::VariantKind::Struct => {
895-
// no value for structs like struct Foo { ... }
896-
}
897-
ty::VariantKind::Tuple | ty::VariantKind::Unit => {
898-
// there is a value for structs like `struct
899-
// Foo()` and `struct Foo`
900-
let ctor_def_id = self.tcx.map.local_def_id(struct_def.id());
901-
self.record(ctor_def_id,
902-
EncodeContext::encode_struct_ctor,
903-
(def_id, ctor_def_id));
904-
}
892+
// If the struct has a constructor, encode it.
893+
if !struct_def.is_struct() {
894+
let ctor_def_id = self.tcx.map.local_def_id(struct_def.id());
895+
self.record(ctor_def_id,
896+
EncodeContext::encode_struct_ctor,
897+
(def_id, ctor_def_id));
905898
}
906899
}
907900
hir::ItemUnion(..) => {

src/librustc_metadata/schema.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use astencode;
1212
use index;
1313

1414
use rustc::hir;
15-
use rustc::hir::def;
15+
use rustc::hir::def::{self, CtorKind};
1616
use rustc::hir::def_id::{DefIndex, DefId};
1717
use rustc::middle::cstore::{LinkagePreference, NativeLibraryKind};
1818
use rustc::middle::lang_items;
@@ -261,7 +261,7 @@ pub struct FnData {
261261

262262
#[derive(RustcEncodable, RustcDecodable)]
263263
pub struct VariantData {
264-
pub kind: ty::VariantKind,
264+
pub ctor_kind: CtorKind,
265265
pub disr: u64,
266266

267267
/// If this is a struct's only variant, this

src/librustc_mir/transform/deaggregator.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
// except according to those terms.
1010

1111
use rustc::ty::TyCtxt;
12+
use rustc::hir::def::CtorKind;
1213
use rustc::mir::repr::*;
1314
use rustc::mir::transform::{MirPass, MirSource, Pass};
1415
use rustc_data_structures::indexed_vec::Idx;
15-
use rustc::ty::VariantKind;
1616

1717
pub struct Deaggregator;
1818

@@ -130,7 +130,7 @@ fn get_aggregate_statement_index<'a, 'tcx, 'b>(start: usize,
130130
debug!("getting variant {:?}", variant);
131131
debug!("for adt_def {:?}", adt_def);
132132
let variant_def = &adt_def.variants[variant];
133-
if variant_def.kind == VariantKind::Struct {
133+
if variant_def.ctor_kind == CtorKind::Fictive {
134134
return Some(i);
135135
}
136136
};

src/librustc_resolve/build_reduced_graph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ impl<'b> Resolver<'b> {
302302
// in the value namespace as well.
303303
if !struct_def.is_struct() {
304304
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
305-
CtorKind::from_vdata(struct_def));
305+
CtorKind::from_ast(struct_def));
306306
self.define(parent, name, ValueNS, (ctor_def, sp, vis));
307307
}
308308

@@ -359,7 +359,7 @@ impl<'b> Resolver<'b> {
359359
// Define a constructor name in the value namespace.
360360
// Braced variants, unlike structs, generate unusable names in
361361
// value namespace, they are reserved for possible future use.
362-
let ctor_kind = CtorKind::from_vdata(&variant.node.data);
362+
let ctor_kind = CtorKind::from_ast(&variant.node.data);
363363
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
364364
self.define(parent, name, ValueNS, (ctor_def, variant.span, vis));
365365
}

src/librustc_trans/debuginfo/metadata.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use session::Session;
2424
use llvm::{self, ValueRef};
2525
use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor, DICompositeType, DILexicalBlock};
2626

27+
use rustc::hir::def::CtorKind;
2728
use rustc::hir::def_id::DefId;
2829
use rustc::ty::subst::Substs;
2930
use rustc::hir;
@@ -1076,10 +1077,6 @@ struct StructMemberDescriptionFactory<'tcx> {
10761077
impl<'tcx> StructMemberDescriptionFactory<'tcx> {
10771078
fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>)
10781079
-> Vec<MemberDescription> {
1079-
if self.variant.kind == ty::VariantKind::Unit {
1080-
return Vec::new();
1081-
}
1082-
10831080
let field_size = if self.is_simd {
10841081
let fty = monomorphize::field_ty(cx.tcx(),
10851082
self.substs,
@@ -1093,7 +1090,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {
10931090
};
10941091

10951092
self.variant.fields.iter().enumerate().map(|(i, f)| {
1096-
let name = if self.variant.kind == ty::VariantKind::Tuple {
1093+
let name = if self.variant.ctor_kind == CtorKind::Fn {
10971094
format!("__{}", i)
10981095
} else {
10991096
f.name.to_string()
@@ -1387,12 +1384,12 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
13871384
// For the metadata of the wrapper struct, we need to create a
13881385
// MemberDescription of the struct's single field.
13891386
let sole_struct_member_description = MemberDescription {
1390-
name: match non_null_variant.kind {
1391-
ty::VariantKind::Tuple => "__0".to_string(),
1392-
ty::VariantKind::Struct => {
1387+
name: match non_null_variant.ctor_kind {
1388+
CtorKind::Fn => "__0".to_string(),
1389+
CtorKind::Fictive => {
13931390
non_null_variant.fields[0].name.to_string()
13941391
}
1395-
ty::VariantKind::Unit => bug!()
1392+
CtorKind::Const => bug!()
13961393
},
13971394
llvm_type: non_null_llvm_type,
13981395
type_metadata: non_null_type_metadata,
@@ -1579,16 +1576,16 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
15791576
containing_scope);
15801577

15811578
// Get the argument names from the enum variant info
1582-
let mut arg_names: Vec<_> = match variant.kind {
1583-
ty::VariantKind::Unit => vec![],
1584-
ty::VariantKind::Tuple => {
1579+
let mut arg_names: Vec<_> = match variant.ctor_kind {
1580+
CtorKind::Const => vec![],
1581+
CtorKind::Fn => {
15851582
variant.fields
15861583
.iter()
15871584
.enumerate()
15881585
.map(|(i, _)| format!("__{}", i))
15891586
.collect()
15901587
}
1591-
ty::VariantKind::Struct => {
1588+
CtorKind::Fictive => {
15921589
variant.fields
15931590
.iter()
15941591
.map(|f| f.name.to_string())

0 commit comments

Comments
 (0)