Skip to content

Commit

Permalink
Refactor definitions of ADTs in rustc::middle::def
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jan 20, 2016
1 parent c4c9628 commit ceaaa1b
Show file tree
Hide file tree
Showing 42 changed files with 250 additions and 223 deletions.
6 changes: 3 additions & 3 deletions src/librustc/middle/check_const.rs
Expand Up @@ -610,11 +610,11 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
hir::ExprPath(..) => {
let def = v.tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
match def {
Some(def::DefVariant(_, _, _)) => {
Some(def::DefVariant(..)) => {
// Count the discriminator or function pointer.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
}
Some(def::DefStruct(_)) => {
Some(def::DefStruct(..)) => {
if let ty::TyBareFn(..) = node_ty.sty {
// Count the function pointer.
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
Expand Down Expand Up @@ -678,7 +678,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
true
}
Some(def::DefFn(did, _)) => {
Some(def::DefFn(did)) => {
v.handle_const_fn_call(e, did, node_ty)
}
Some(def::DefMethod(did)) => {
Expand Down
12 changes: 6 additions & 6 deletions src/librustc/middle/check_match.rs
Expand Up @@ -760,16 +760,16 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefStruct(_)) => vec!(Single),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(DefStruct(..)) => vec!(Single),
Some(DefVariant(_, id)) => vec!(Variant(id)),
_ => vec!()
},
hir::PatEnum(..) =>
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(DefVariant(_, id)) => vec!(Variant(id)),
_ => vec!(Single)
},
hir::PatQPath(..) =>
Expand All @@ -780,7 +780,7 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
Some(DefVariant(_, id)) => vec!(Variant(id)),
_ => vec!(Single)
},
hir::PatLit(ref expr) =>
Expand Down Expand Up @@ -872,7 +872,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
been rewritten"),
Some(DefVariant(_, id, _)) => if *constructor == Variant(id) {
Some(DefVariant(_, id)) => if *constructor == Variant(id) {
Some(vec!())
} else {
None
Expand All @@ -887,7 +887,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
DefConst(..) | DefAssociatedConst(..) =>
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
been rewritten"),
DefVariant(_, id, _) if *constructor != Variant(id) => None,
DefVariant(_, id) if *constructor != Variant(id) => None,
DefVariant(..) | DefStruct(..) => {
Some(match args {
&Some(ref args) => args.iter().map(|p| &**p).collect(),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/check_static_recursion.rs
Expand Up @@ -263,7 +263,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
// affect the specific variant used, but we need to check
// the whole enum definition to see what expression that
// might be (if any).
Some(DefVariant(enum_id, variant_id, false)) => {
Some(DefVariant(enum_id, variant_id)) => {
if let Some(enum_node_id) = self.ast_map.as_local_node_id(enum_id) {
if let hir::ItemEnum(ref enum_def, ref generics) =
self.ast_map.expect_item(enum_node_id).node
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/const_eval.rs
Expand Up @@ -332,7 +332,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
}
let path = match def.full_def() {
def::DefStruct(def_id) => def_to_path(tcx, def_id),
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
def::DefVariant(_, variant_did) => def_to_path(tcx, variant_did),
def::DefFn(..) => return P(hir::Pat {
id: expr.id,
node: hir::PatLit(P(expr.clone())),
Expand Down Expand Up @@ -1052,10 +1052,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
}
}
Some(def::DefVariant(enum_def, variant_def, _)) => {
Some(def::DefVariant(enum_def, variant_def)) => {
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
}
Some(def::DefStruct(_)) => {
Some(def::DefStruct(..)) => {
return Ok(ConstVal::Struct(e.id))
}
Some(def::DefLocal(_, id)) => {
Expand All @@ -1066,7 +1066,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
(None, None)
}
},
Some(def::DefMethod(id)) | Some(def::DefFn(id, _)) => return Ok(Function(id)),
Some(def::DefMethod(id)) | Some(def::DefFn(id)) => return Ok(Function(id)),
_ => (None, None)
};
let const_expr = match const_expr {
Expand Down
7 changes: 6 additions & 1 deletion src/librustc/middle/cstore.rs
Expand Up @@ -26,7 +26,7 @@ use back::svh::Svh;
use front::map as hir_map;
use middle::def;
use middle::lang_items;
use middle::ty::{self, Ty};
use middle::ty::{self, Ty, VariantKind};
use middle::def_id::{DefId, DefIndex};
use mir::repr::Mir;
use session::Session;
Expand Down Expand Up @@ -211,6 +211,8 @@ pub trait CrateStore<'tcx> : Any {

// resolve
fn def_path(&self, def: DefId) -> hir_map::DefPath;
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind>;
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>;
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>;
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name>;
fn item_children(&self, did: DefId) -> Vec<ChildItem>;
Expand Down Expand Up @@ -380,6 +382,9 @@ impl<'tcx> CrateStore<'tcx> for DummyCrateStore {

// resolve
fn def_path(&self, def: DefId) -> hir_map::DefPath { unimplemented!() }
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { unimplemented!() }
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
{ unimplemented!() }
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
{ unimplemented!() }
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { unimplemented!() }
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/dead.rs
Expand Up @@ -100,7 +100,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
_ if self.ignore_non_const_paths => (),
def::DefPrimTy(_) => (),
def::DefSelfTy(..) => (),
def::DefVariant(enum_id, variant_id, _) => {
def::DefVariant(enum_id, variant_id) => {
self.check_def_id(enum_id);
if !self.ignore_variant_stack.contains(&variant_id) {
self.check_def_id(variant_id);
Expand Down
27 changes: 12 additions & 15 deletions src/librustc/middle/def.rs
Expand Up @@ -19,7 +19,7 @@ use rustc_front::hir;

#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum Def {
DefFn(DefId, bool /* is_ctor */),
DefFn(DefId),
DefSelfTy(Option<DefId>, // trait id
Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
DefMod(DefId),
Expand All @@ -29,8 +29,9 @@ pub enum Def {
DefAssociatedConst(DefId),
DefLocal(DefId, // def id of variable
ast::NodeId), // node id of variable
DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
DefTy(DefId, bool /* is_enum */),
DefVariant(DefId /* enum */, DefId /* variant */),
DefEnum(DefId),
DefTyAlias(DefId),
DefAssociatedTy(DefId /* trait */, DefId),
DefTrait(DefId),
DefPrimTy(hir::PrimTy),
Expand All @@ -40,14 +41,10 @@ pub enum Def {
usize, // index in the freevars list of the closure
ast::NodeId), // expr node that creates the closure

/// Note that if it's a tuple struct's definition, the node id of the DefId
/// may either refer to the item definition's id or the VariantData.ctor_id.
///
/// The cases that I have encountered so far are (this is not exhaustive):
/// - If it's a ty_path referring to some tuple struct, then DefMap maps
/// it to a def whose id is the item definition's id.
/// - If it's an ExprPath referring to some tuple struct, then DefMap maps
/// it to a def whose id is the VariantData.ctor_id.
// If DefStruct lives in type namespace it denotes a struct item and its DefId refers
// to NodeId of the struct itself.
// If DefStruct lives in value namespace (e.g. tuple struct, unit struct expressions)
// it denotes a constructor and its DefId refers to NodeId of the struct's constructor.
DefStruct(DefId),
DefLabel(ast::NodeId),
DefMethod(DefId),
Expand Down Expand Up @@ -121,7 +118,7 @@ impl Def {
}

DefFn(..) | DefMod(..) | DefForeignMod(..) | DefStatic(..) |
DefVariant(..) | DefTy(..) | DefAssociatedTy(..) |
DefVariant(..) | DefEnum(..) | DefTyAlias(..) | DefAssociatedTy(..) |
DefTyParam(..) | DefStruct(..) | DefTrait(..) |
DefMethod(..) | DefConst(..) | DefAssociatedConst(..) |
DefPrimTy(..) | DefLabel(..) | DefSelfTy(..) | DefErr => {
Expand All @@ -132,8 +129,8 @@ impl Def {

pub fn def_id(&self) -> DefId {
match *self {
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
DefFn(id) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
DefVariant(_, id) | DefEnum(id) | DefTyAlias(id) | DefAssociatedTy(_, id) |
DefTyParam(_, _, id, _) | DefStruct(id) | DefTrait(id) |
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
DefLocal(id, _) | DefUpvar(id, _, _, _) => {
Expand All @@ -151,7 +148,7 @@ impl Def {

pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
match *self {
DefVariant(enum_id, var_id, _) => {
DefVariant(enum_id, var_id) => {
Some((enum_id, var_id))
}
_ => None
Expand Down
17 changes: 3 additions & 14 deletions src/librustc/middle/expr_use_visitor.rs
Expand Up @@ -1077,7 +1077,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
// struct or enum pattern.
}

Some(def::DefVariant(enum_did, variant_did, _is_struct)) => {
Some(def::DefVariant(enum_did, variant_did)) => {
let downcast_cmt =
if tcx.lookup_adt_def(enum_did).is_univariant() {
cmt_pat
Expand All @@ -1093,7 +1093,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
delegate.matched_pat(pat, downcast_cmt, match_mode);
}

Some(def::DefStruct(..)) | Some(def::DefTy(_, false)) => {
Some(def::DefStruct(..)) | Some(def::DefTyAlias(..)) => {
// A struct (in either the value or type
// namespace; we encounter the former on
// e.g. patterns for unit structs).
Expand All @@ -1113,19 +1113,8 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
// `matched_pat` call.
}

Some(def @ def::DefTy(_, true)) => {
// An enum's type -- should never be in a
// pattern.

if !tcx.sess.has_errors() {
let msg = format!("Pattern has unexpected type: {:?} and type {:?}",
def,
cmt_pat.ty);
tcx.sess.span_bug(pat.span, &msg)
}
}

Some(def) => {
// An enum type should never be in a pattern.
// Remaining cases are e.g. DefFn, to
// which identifiers within patterns
// should not resolve. However, we do
Expand Down
3 changes: 1 addition & 2 deletions src/librustc/middle/infer/error_reporting.rs
Expand Up @@ -1404,7 +1404,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
Some(d) => d.full_def()
};
match a_def {
def::DefTy(did, _) | def::DefStruct(did) => {
def::DefEnum(did) | def::DefTyAlias(did) | def::DefStruct(did) => {
let generics = self.tcx.lookup_item_type(did).generics;

let expected =
Expand Down Expand Up @@ -1452,7 +1452,6 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
}
_ => ()
}

}

hir::TyPtr(ref mut_ty) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/intrinsicck.rs
Expand Up @@ -235,7 +235,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &hir::Expr) {
if let hir::ExprPath(..) = expr.node {
match self.tcx.resolve_expr(expr) {
DefFn(did, _) if self.def_id_is_transmute(did) => {
DefFn(did) if self.def_id_is_transmute(did) => {
let typ = self.tcx.node_id_to_type(expr.id);
match typ.sty {
TyBareFn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => {
Expand Down
15 changes: 5 additions & 10 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -552,19 +552,14 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
def::DefAssociatedConst(..) | def::DefFn(..) | def::DefMethod(..) => {
Ok(self.cat_rvalue_node(id, span, expr_ty))
}

def::DefMod(_) | def::DefForeignMod(_) |
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
def::DefTrait(_) | def::DefEnum(..) | def::DefTyAlias(..) | def::DefPrimTy(_) |
def::DefTyParam(..) |
def::DefLabel(_) | def::DefSelfTy(..) |
def::DefAssociatedTy(..) => {
Ok(Rc::new(cmt_ {
id:id,
span:span,
cat:Categorization::StaticItem,
mutbl: McImmutable,
ty:expr_ty,
note: NoteNone
}))
self.tcx().sess.span_bug(span, &format!("Unexpected definition in \
memory categorization: {:?}", def));
}

def::DefStatic(_, mutbl) => {
Expand Down Expand Up @@ -1218,7 +1213,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
// alone) because struct patterns can refer to struct types or
// to struct variants within enums.
let cmt = match opt_def {
Some(def::DefVariant(enum_did, variant_did, _))
Some(def::DefVariant(enum_did, variant_did))
// univariant enums do not need downcasts
if !self.tcx().lookup_adt_def(enum_did).is_univariant() => {
self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/middle/pat_util.rs
Expand Up @@ -228,7 +228,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
hir::PatIdent(_, _, None) |
hir::PatStruct(..) => {
match dm.get(&p.id) {
Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
Some(&PathResolution { base_def: DefVariant(_, id), .. }) => {
variants.push(id);
}
_ => ()
Expand Down
16 changes: 13 additions & 3 deletions src/librustc/middle/ty/mod.rs
Expand Up @@ -1429,9 +1429,19 @@ impl<'tcx> Decodable for AdtDef<'tcx> {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum AdtKind { Struct, Enum }

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[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,
}
}
}

impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
fn new(tcx: &ctxt<'tcx>,
did: DefId,
Expand Down Expand Up @@ -1577,8 +1587,8 @@ impl<'tcx, 'container> AdtDefData<'tcx, 'container> {

pub fn variant_of_def(&self, def: def::Def) -> &VariantDefData<'tcx, 'container> {
match def {
def::DefVariant(_, vid, _) => self.variant_with_id(vid),
def::DefStruct(..) | def::DefTy(..) => self.struct_variant(),
def::DefVariant(_, vid) => self.variant_with_id(vid),
def::DefStruct(..) | def::DefTyAlias(..) => self.struct_variant(),
_ => panic!("unexpected def {:?} in variant_of_def", def)
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_lint/builtin.rs
Expand Up @@ -1060,7 +1060,7 @@ impl LateLintPass for MutableTransmutes {
hir::ExprPath(..) => (),
_ => return None
}
if let def::DefFn(did, _) = cx.tcx.resolve_expr(expr) {
if let def::DefFn(did) = cx.tcx.resolve_expr(expr) {
if !def_id_is_transmute(cx, did) {
return None;
}
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_metadata/astencode.rs
Expand Up @@ -376,7 +376,7 @@ fn decode_def(dcx: &DecodeContext, dsr: &mut reader::Decoder) -> def::Def {
impl tr for def::Def {
fn tr(&self, dcx: &DecodeContext) -> def::Def {
match *self {
def::DefFn(did, is_ctor) => def::DefFn(did.tr(dcx), is_ctor),
def::DefFn(did) => def::DefFn(did.tr(dcx)),
def::DefMethod(did) => def::DefMethod(did.tr(dcx)),
def::DefSelfTy(opt_did, impl_ids) => { def::DefSelfTy(opt_did.map(|did| did.tr(dcx)),
impl_ids.map(|(nid1, nid2)| {
Expand All @@ -393,11 +393,10 @@ impl tr for def::Def {
let did = dcx.tcx.map.local_def_id(nid);
def::DefLocal(did, nid)
}
def::DefVariant(e_did, v_did, is_s) => {
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)
},
def::DefVariant(e_did, v_did) => def::DefVariant(e_did.tr(dcx), v_did.tr(dcx)),
def::DefTrait(did) => def::DefTrait(did.tr(dcx)),
def::DefTy(did, is_enum) => def::DefTy(did.tr(dcx), is_enum),
def::DefEnum(did) => def::DefEnum(did.tr(dcx)),
def::DefTyAlias(did) => def::DefTyAlias(did.tr(dcx)),
def::DefAssociatedTy(trait_did, did) =>
def::DefAssociatedTy(trait_did.tr(dcx), did.tr(dcx)),
def::DefPrimTy(p) => def::DefPrimTy(p),
Expand Down

0 comments on commit ceaaa1b

Please sign in to comment.