Skip to content

Commit

Permalink
Eliminate ty::VariantKind in favor of def::CtorKind
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Oct 4, 2016
1 parent 64bdf1b commit 75d6522
Show file tree
Hide file tree
Showing 16 changed files with 73 additions and 104 deletions.
9 changes: 8 additions & 1 deletion src/librustc/hir/def.rs
Expand Up @@ -105,13 +105,20 @@ pub struct Export {
}

impl CtorKind {
pub fn from_vdata(vdata: &ast::VariantData) -> CtorKind {
pub fn from_ast(vdata: &ast::VariantData) -> CtorKind {
match *vdata {
ast::VariantData::Tuple(..) => CtorKind::Fn,
ast::VariantData::Unit(..) => CtorKind::Const,
ast::VariantData::Struct(..) => CtorKind::Fictive,
}
}
pub fn from_hir(vdata: &hir::VariantData) -> CtorKind {
match *vdata {
hir::VariantData::Tuple(..) => CtorKind::Fn,
hir::VariantData::Unit(..) => CtorKind::Const,
hir::VariantData::Struct(..) => CtorKind::Fictive,
}
}
}

impl Def {
Expand Down
2 changes: 0 additions & 2 deletions src/librustc/middle/cstore.rs
Expand Up @@ -201,7 +201,6 @@ pub trait CrateStore<'tcx> {
-> Option<DefIndex>;
fn def_key(&self, def: DefId) -> hir_map::DefKey;
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath>;
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>;
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<def::Export>;
Expand Down Expand Up @@ -378,7 +377,6 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {
fn relative_def_path(&self, def: DefId) -> Option<hir_map::DefPath> {
bug!("relative_def_path")
}
fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind> { bug!("variant_kind") }
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{ bug!("struct_ctor_def_id") }
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { bug!("struct_field_names") }
Expand Down
9 changes: 5 additions & 4 deletions src/librustc/mir/repr.rs
Expand Up @@ -15,6 +15,7 @@ use rustc_data_structures::indexed_vec::{IndexVec, Idx};
use rustc_data_structures::control_flow_graph::dominators::{Dominators, dominators};
use rustc_data_structures::control_flow_graph::{GraphPredecessors, GraphSuccessors};
use rustc_data_structures::control_flow_graph::ControlFlowGraph;
use hir::def::CtorKind;
use hir::def_id::DefId;
use ty::subst::Substs;
use ty::{self, AdtDef, ClosureSubsts, Region, Ty};
Expand Down Expand Up @@ -1140,10 +1141,10 @@ impl<'tcx> Debug for Rvalue<'tcx> {
ppaux::parameterized(fmt, substs, variant_def.did,
ppaux::Ns::Value, &[])?;

match variant_def.kind {
ty::VariantKind::Unit => Ok(()),
ty::VariantKind::Tuple => fmt_tuple(fmt, lvs),
ty::VariantKind::Struct => {
match variant_def.ctor_kind {
CtorKind::Const => Ok(()),
CtorKind::Fn => fmt_tuple(fmt, lvs),
CtorKind::Fictive => {
let mut struct_fmt = fmt.debug_struct("");
for (field, lv) in variant_def.fields.iter().zip(lvs) {
struct_fmt.field(&field.name.as_str(), lv);
Expand Down
22 changes: 1 addition & 21 deletions src/librustc/ty/mod.rs
Expand Up @@ -1420,7 +1420,7 @@ pub struct VariantDefData<'tcx, 'container: 'tcx> {
pub name: Name, // struct's name if this is a struct
pub disr_val: Disr,
pub fields: Vec<FieldDefData<'tcx, 'container>>,
pub kind: VariantKind,
pub ctor_kind: CtorKind,
}

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

#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug, RustcEncodable, RustcDecodable)]
pub enum VariantKind { Struct, Tuple, Unit }

impl VariantKind {
pub fn from_variant_data(vdata: &hir::VariantData) -> Self {
match *vdata {
hir::VariantData::Struct(..) => VariantKind::Struct,
hir::VariantData::Tuple(..) => VariantKind::Tuple,
hir::VariantData::Unit(..) => VariantKind::Unit,
}
}
pub fn ctor_kind(self) -> CtorKind {
match self {
VariantKind::Tuple => CtorKind::Fn,
VariantKind::Unit => CtorKind::Const,
VariantKind::Struct => CtorKind::Fictive,
}
}
}

impl<'a, 'gcx, 'tcx, 'container> AdtDefData<'gcx, 'container> {
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
did: DefId,
Expand Down
10 changes: 5 additions & 5 deletions src/librustc_const_eval/check_match.rs
Expand Up @@ -247,7 +247,7 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
if edef.is_enum() {
if let Def::Local(..) = cx.tcx.expect_def(p.id) {
if edef.variants.iter().any(|variant| {
variant.name == name.node && variant.kind == ty::VariantKind::Unit
variant.name == name.node && variant.ctor_kind == CtorKind::Const
}) {
let ty_path = cx.tcx.item_path_str(edef.did);
let mut err = struct_span_warn!(cx.tcx.sess, p.span, E0170,
Expand Down Expand Up @@ -577,8 +577,8 @@ fn construct_witness<'a,'tcx>(cx: &MatchCheckCtxt<'a,'tcx>, ctor: &Constructor,

ty::TyAdt(adt, _) => {
let v = ctor.variant_for_adt(adt);
match v.kind {
ty::VariantKind::Struct => {
match v.ctor_kind {
CtorKind::Fictive => {
let field_pats: hir::HirVec<_> = v.fields.iter()
.zip(pats)
.filter(|&(_, ref pat)| pat.node != PatKind::Wild)
Expand All @@ -593,10 +593,10 @@ fn construct_witness<'a,'tcx>(cx: &MatchCheckCtxt<'a,'tcx>, ctor: &Constructor,
let has_more_fields = field_pats.len() < pats_len;
PatKind::Struct(def_to_path(cx.tcx, v.did), field_pats, has_more_fields)
}
ty::VariantKind::Tuple => {
CtorKind::Fn => {
PatKind::TupleStruct(def_to_path(cx.tcx, v.did), pats.collect(), None)
}
ty::VariantKind::Unit => {
CtorKind::Const => {
PatKind::Path(None, def_to_path(cx.tcx, v.did))
}
}
Expand Down
6 changes: 0 additions & 6 deletions src/librustc_metadata/csearch.rs
Expand Up @@ -342,12 +342,6 @@ impl<'tcx> CrateStore<'tcx> for cstore::CStore {
self.get_crate_data(def.krate).def_path(def.index)
}

fn variant_kind(&self, def_id: DefId) -> Option<ty::VariantKind>
{
self.dep_graph.read(DepNode::MetaData(def_id));
self.get_crate_data(def_id.krate).get_variant_kind(def_id.index)
}

fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{
self.dep_graph.read(DepNode::MetaData(struct_def_id));
Expand Down
18 changes: 9 additions & 9 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -22,7 +22,7 @@ use rustc::hir;
use rustc::hir::intravisit::IdRange;

use rustc::middle::cstore::{InlinedItem, LinkagePreference};
use rustc::hir::def::{self, Def};
use rustc::hir::def::{self, Def, CtorKind};
use rustc::hir::def_id::{CrateNum, DefId, DefIndex, LOCAL_CRATE};
use rustc::middle::lang_items;
use rustc::ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -534,7 +534,7 @@ impl<'a, 'tcx> CrateMetadata {
name: self.item_name(item),
fields: fields,
disr_val: ConstInt::Infer(data.disr),
kind: data.kind,
ctor_kind: data.ctor_kind,
}, data.struct_ctor)
}

Expand Down Expand Up @@ -693,16 +693,16 @@ impl<'a, 'tcx> CrateMetadata {
match def {
Def::Struct(..) => {
if let Some(ctor_def_id) = self.get_struct_ctor_def_id(child_index) {
let vkind = self.get_variant_kind(child_index).unwrap();
let ctor_def = Def::StructCtor(ctor_def_id, vkind.ctor_kind());
let ctor_kind = self.get_ctor_kind(child_index);
let ctor_def = Def::StructCtor(ctor_def_id, ctor_kind);
callback(def::Export { def: ctor_def, name: name });
}
}
Def::Variant(def_id) => {
// Braced variants, unlike structs, generate unusable names in
// value namespace, they are reserved for possible future use.
let vkind = self.get_variant_kind(child_index).unwrap();
let ctor_def = Def::VariantCtor(def_id, vkind.ctor_kind());
let ctor_kind = self.get_ctor_kind(child_index);
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
callback(def::Export { def: ctor_def, name: name });
}
_ => {}
Expand Down Expand Up @@ -806,12 +806,12 @@ impl<'a, 'tcx> CrateMetadata {
self.entry(id).variances.decode(self).collect()
}

pub fn get_variant_kind(&self, node_id: DefIndex) -> Option<ty::VariantKind> {
pub fn get_ctor_kind(&self, node_id: DefIndex) -> CtorKind {
match self.entry(node_id).kind {
EntryKind::Struct(data) |
EntryKind::Union(data) |
EntryKind::Variant(data) => Some(data.decode(self).kind),
_ => None
EntryKind::Variant(data) => data.decode(self).ctor_kind,
_ => CtorKind::Fictive,
}
}

Expand Down
27 changes: 10 additions & 17 deletions src/librustc_metadata/encoder.rs
Expand Up @@ -258,7 +258,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let def_id = variant.did;

let data = VariantData {
kind: variant.kind,
ctor_kind: variant.ctor_kind,
disr: variant.disr_val.to_u64_unchecked(),
struct_ctor: None
};
Expand Down Expand Up @@ -410,7 +410,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let variant = tcx.lookup_adt_def(adt_def_id).struct_variant();

let data = VariantData {
kind: variant.kind,
ctor_kind: variant.ctor_kind,
disr: variant.disr_val.to_u64_unchecked(),
struct_ctor: Some(def_id.index)
};
Expand Down Expand Up @@ -675,7 +675,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
None
};
EntryKind::Struct(self.lazy(&VariantData {
kind: variant.kind,
ctor_kind: variant.ctor_kind,
disr: variant.disr_val.to_u64_unchecked(),
struct_ctor: struct_ctor
}))
Expand All @@ -684,7 +684,7 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let variant = tcx.lookup_adt_def(def_id).struct_variant();

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

// If this is a tuple-like struct, encode the type of the constructor.
match self.tcx.lookup_adt_def(def_id).struct_variant().kind {
ty::VariantKind::Struct => {
// no value for structs like struct Foo { ... }
}
ty::VariantKind::Tuple | ty::VariantKind::Unit => {
// there is a value for structs like `struct
// Foo()` and `struct Foo`
let ctor_def_id = self.tcx.map.local_def_id(struct_def.id());
self.record(ctor_def_id,
EncodeContext::encode_struct_ctor,
(def_id, ctor_def_id));
}
// If the struct has a constructor, encode it.
if !struct_def.is_struct() {
let ctor_def_id = self.tcx.map.local_def_id(struct_def.id());
self.record(ctor_def_id,
EncodeContext::encode_struct_ctor,
(def_id, ctor_def_id));
}
}
hir::ItemUnion(..) => {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/schema.rs
Expand Up @@ -12,7 +12,7 @@ use astencode;
use index;

use rustc::hir;
use rustc::hir::def;
use rustc::hir::def::{self, CtorKind};
use rustc::hir::def_id::{DefIndex, DefId};
use rustc::middle::cstore::{LinkagePreference, NativeLibraryKind};
use rustc::middle::lang_items;
Expand Down Expand Up @@ -261,7 +261,7 @@ pub struct FnData {

#[derive(RustcEncodable, RustcDecodable)]
pub struct VariantData {
pub kind: ty::VariantKind,
pub ctor_kind: CtorKind,
pub disr: u64,

/// If this is a struct's only variant, this
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_mir/transform/deaggregator.rs
Expand Up @@ -9,10 +9,10 @@
// except according to those terms.

use rustc::ty::TyCtxt;
use rustc::hir::def::CtorKind;
use rustc::mir::repr::*;
use rustc::mir::transform::{MirPass, MirSource, Pass};
use rustc_data_structures::indexed_vec::Idx;
use rustc::ty::VariantKind;

pub struct Deaggregator;

Expand Down Expand Up @@ -130,7 +130,7 @@ fn get_aggregate_statement_index<'a, 'tcx, 'b>(start: usize,
debug!("getting variant {:?}", variant);
debug!("for adt_def {:?}", adt_def);
let variant_def = &adt_def.variants[variant];
if variant_def.kind == VariantKind::Struct {
if variant_def.ctor_kind == CtorKind::Fictive {
return Some(i);
}
};
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_resolve/build_reduced_graph.rs
Expand Up @@ -302,7 +302,7 @@ impl<'b> Resolver<'b> {
// in the value namespace as well.
if !struct_def.is_struct() {
let ctor_def = Def::StructCtor(self.definitions.local_def_id(struct_def.id()),
CtorKind::from_vdata(struct_def));
CtorKind::from_ast(struct_def));
self.define(parent, name, ValueNS, (ctor_def, sp, vis));
}

Expand Down Expand Up @@ -359,7 +359,7 @@ impl<'b> Resolver<'b> {
// Define a constructor name in the value namespace.
// Braced variants, unlike structs, generate unusable names in
// value namespace, they are reserved for possible future use.
let ctor_kind = CtorKind::from_vdata(&variant.node.data);
let ctor_kind = CtorKind::from_ast(&variant.node.data);
let ctor_def = Def::VariantCtor(def_id, ctor_kind);
self.define(parent, name, ValueNS, (ctor_def, variant.span, vis));
}
Expand Down
23 changes: 10 additions & 13 deletions src/librustc_trans/debuginfo/metadata.rs
Expand Up @@ -24,6 +24,7 @@ use session::Session;
use llvm::{self, ValueRef};
use llvm::debuginfo::{DIType, DIFile, DIScope, DIDescriptor, DICompositeType, DILexicalBlock};

use rustc::hir::def::CtorKind;
use rustc::hir::def_id::DefId;
use rustc::ty::subst::Substs;
use rustc::hir;
Expand Down Expand Up @@ -1076,10 +1077,6 @@ struct StructMemberDescriptionFactory<'tcx> {
impl<'tcx> StructMemberDescriptionFactory<'tcx> {
fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>)
-> Vec<MemberDescription> {
if self.variant.kind == ty::VariantKind::Unit {
return Vec::new();
}

let field_size = if self.is_simd {
let fty = monomorphize::field_ty(cx.tcx(),
self.substs,
Expand All @@ -1093,7 +1090,7 @@ impl<'tcx> StructMemberDescriptionFactory<'tcx> {
};

self.variant.fields.iter().enumerate().map(|(i, f)| {
let name = if self.variant.kind == ty::VariantKind::Tuple {
let name = if self.variant.ctor_kind == CtorKind::Fn {
format!("__{}", i)
} else {
f.name.to_string()
Expand Down Expand Up @@ -1387,12 +1384,12 @@ impl<'tcx> EnumMemberDescriptionFactory<'tcx> {
// For the metadata of the wrapper struct, we need to create a
// MemberDescription of the struct's single field.
let sole_struct_member_description = MemberDescription {
name: match non_null_variant.kind {
ty::VariantKind::Tuple => "__0".to_string(),
ty::VariantKind::Struct => {
name: match non_null_variant.ctor_kind {
CtorKind::Fn => "__0".to_string(),
CtorKind::Fictive => {
non_null_variant.fields[0].name.to_string()
}
ty::VariantKind::Unit => bug!()
CtorKind::Const => bug!()
},
llvm_type: non_null_llvm_type,
type_metadata: non_null_type_metadata,
Expand Down Expand Up @@ -1579,16 +1576,16 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>,
containing_scope);

// Get the argument names from the enum variant info
let mut arg_names: Vec<_> = match variant.kind {
ty::VariantKind::Unit => vec![],
ty::VariantKind::Tuple => {
let mut arg_names: Vec<_> = match variant.ctor_kind {
CtorKind::Const => vec![],
CtorKind::Fn => {
variant.fields
.iter()
.enumerate()
.map(|(i, _)| format!("__{}", i))
.collect()
}
ty::VariantKind::Struct => {
CtorKind::Fictive => {
variant.fields
.iter()
.map(|f| f.name.to_string())
Expand Down

0 comments on commit 75d6522

Please sign in to comment.