Skip to content

Commit ceaaa1b

Browse files
committed
Refactor definitions of ADTs in rustc::middle::def
1 parent c4c9628 commit ceaaa1b

Some content is hidden

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

42 files changed

+250
-223
lines changed

src/librustc/middle/check_const.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -610,11 +610,11 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
610610
hir::ExprPath(..) => {
611611
let def = v.tcx.def_map.borrow().get(&e.id).map(|d| d.full_def());
612612
match def {
613-
Some(def::DefVariant(_, _, _)) => {
613+
Some(def::DefVariant(..)) => {
614614
// Count the discriminator or function pointer.
615615
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
616616
}
617-
Some(def::DefStruct(_)) => {
617+
Some(def::DefStruct(..)) => {
618618
if let ty::TyBareFn(..) = node_ty.sty {
619619
// Count the function pointer.
620620
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
@@ -678,7 +678,7 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
678678
v.add_qualif(ConstQualif::NON_ZERO_SIZED);
679679
true
680680
}
681-
Some(def::DefFn(did, _)) => {
681+
Some(def::DefFn(did)) => {
682682
v.handle_const_fn_call(e, did, node_ty)
683683
}
684684
Some(def::DefMethod(did)) => {

src/librustc/middle/check_match.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -760,16 +760,16 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
760760
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
761761
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
762762
been rewritten"),
763-
Some(DefStruct(_)) => vec!(Single),
764-
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
763+
Some(DefStruct(..)) => vec!(Single),
764+
Some(DefVariant(_, id)) => vec!(Variant(id)),
765765
_ => vec!()
766766
},
767767
hir::PatEnum(..) =>
768768
match cx.tcx.def_map.borrow().get(&pat.id).map(|d| d.full_def()) {
769769
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
770770
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
771771
been rewritten"),
772-
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
772+
Some(DefVariant(_, id)) => vec!(Variant(id)),
773773
_ => vec!(Single)
774774
},
775775
hir::PatQPath(..) =>
@@ -780,7 +780,7 @@ fn pat_constructors(cx: &MatchCheckCtxt, p: &Pat,
780780
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
781781
cx.tcx.sess.span_bug(pat.span, "const pattern should've \
782782
been rewritten"),
783-
Some(DefVariant(_, id, _)) => vec!(Variant(id)),
783+
Some(DefVariant(_, id)) => vec!(Variant(id)),
784784
_ => vec!(Single)
785785
},
786786
hir::PatLit(ref expr) =>
@@ -872,7 +872,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
872872
Some(DefConst(..)) | Some(DefAssociatedConst(..)) =>
873873
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
874874
been rewritten"),
875-
Some(DefVariant(_, id, _)) => if *constructor == Variant(id) {
875+
Some(DefVariant(_, id)) => if *constructor == Variant(id) {
876876
Some(vec!())
877877
} else {
878878
None
@@ -887,7 +887,7 @@ pub fn specialize<'a>(cx: &MatchCheckCtxt, r: &[&'a Pat],
887887
DefConst(..) | DefAssociatedConst(..) =>
888888
cx.tcx.sess.span_bug(pat_span, "const pattern should've \
889889
been rewritten"),
890-
DefVariant(_, id, _) if *constructor != Variant(id) => None,
890+
DefVariant(_, id) if *constructor != Variant(id) => None,
891891
DefVariant(..) | DefStruct(..) => {
892892
Some(match args {
893893
&Some(ref args) => args.iter().map(|p| &**p).collect(),

src/librustc/middle/check_static_recursion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'a, 'ast: 'a> Visitor<'ast> for CheckItemRecursionVisitor<'a, 'ast> {
263263
// affect the specific variant used, but we need to check
264264
// the whole enum definition to see what expression that
265265
// might be (if any).
266-
Some(DefVariant(enum_id, variant_id, false)) => {
266+
Some(DefVariant(enum_id, variant_id)) => {
267267
if let Some(enum_node_id) = self.ast_map.as_local_node_id(enum_id) {
268268
if let hir::ItemEnum(ref enum_def, ref generics) =
269269
self.ast_map.expect_item(enum_node_id).node

src/librustc/middle/const_eval.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr, span: Span) -> P<hir::Pat>
332332
}
333333
let path = match def.full_def() {
334334
def::DefStruct(def_id) => def_to_path(tcx, def_id),
335-
def::DefVariant(_, variant_did, _) => def_to_path(tcx, variant_did),
335+
def::DefVariant(_, variant_did) => def_to_path(tcx, variant_did),
336336
def::DefFn(..) => return P(hir::Pat {
337337
id: expr.id,
338338
node: hir::PatLit(P(expr.clone())),
@@ -1052,10 +1052,10 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10521052
(lookup_const_by_id(tcx, def_id, Some(e.id), None), None)
10531053
}
10541054
}
1055-
Some(def::DefVariant(enum_def, variant_def, _)) => {
1055+
Some(def::DefVariant(enum_def, variant_def)) => {
10561056
(lookup_variant_by_id(tcx, enum_def, variant_def), None)
10571057
}
1058-
Some(def::DefStruct(_)) => {
1058+
Some(def::DefStruct(..)) => {
10591059
return Ok(ConstVal::Struct(e.id))
10601060
}
10611061
Some(def::DefLocal(_, id)) => {
@@ -1066,7 +1066,7 @@ pub fn eval_const_expr_partial<'tcx>(tcx: &ty::ctxt<'tcx>,
10661066
(None, None)
10671067
}
10681068
},
1069-
Some(def::DefMethod(id)) | Some(def::DefFn(id, _)) => return Ok(Function(id)),
1069+
Some(def::DefMethod(id)) | Some(def::DefFn(id)) => return Ok(Function(id)),
10701070
_ => (None, None)
10711071
};
10721072
let const_expr = match const_expr {

src/librustc/middle/cstore.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use back::svh::Svh;
2626
use front::map as hir_map;
2727
use middle::def;
2828
use middle::lang_items;
29-
use middle::ty::{self, Ty};
29+
use middle::ty::{self, Ty, VariantKind};
3030
use middle::def_id::{DefId, DefIndex};
3131
use mir::repr::Mir;
3232
use session::Session;
@@ -211,6 +211,8 @@ pub trait CrateStore<'tcx> : Any {
211211

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

381383
// resolve
382384
fn def_path(&self, def: DefId) -> hir_map::DefPath { unimplemented!() }
385+
fn variant_kind(&self, def_id: DefId) -> Option<VariantKind> { unimplemented!() }
386+
fn struct_ctor_def_id(&self, struct_def_id: DefId) -> Option<DefId>
387+
{ unimplemented!() }
383388
fn tuple_struct_definition_if_ctor(&self, did: DefId) -> Option<DefId>
384389
{ unimplemented!() }
385390
fn struct_field_names(&self, def: DefId) -> Vec<ast::Name> { unimplemented!() }

src/librustc/middle/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
100100
_ if self.ignore_non_const_paths => (),
101101
def::DefPrimTy(_) => (),
102102
def::DefSelfTy(..) => (),
103-
def::DefVariant(enum_id, variant_id, _) => {
103+
def::DefVariant(enum_id, variant_id) => {
104104
self.check_def_id(enum_id);
105105
if !self.ignore_variant_stack.contains(&variant_id) {
106106
self.check_def_id(variant_id);

src/librustc/middle/def.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_front::hir;
1919

2020
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
2121
pub enum Def {
22-
DefFn(DefId, bool /* is_ctor */),
22+
DefFn(DefId),
2323
DefSelfTy(Option<DefId>, // trait id
2424
Option<(ast::NodeId, ast::NodeId)>), // (impl id, self type id)
2525
DefMod(DefId),
@@ -29,8 +29,9 @@ pub enum Def {
2929
DefAssociatedConst(DefId),
3030
DefLocal(DefId, // def id of variable
3131
ast::NodeId), // node id of variable
32-
DefVariant(DefId /* enum */, DefId /* variant */, bool /* is_structure */),
33-
DefTy(DefId, bool /* is_enum */),
32+
DefVariant(DefId /* enum */, DefId /* variant */),
33+
DefEnum(DefId),
34+
DefTyAlias(DefId),
3435
DefAssociatedTy(DefId /* trait */, DefId),
3536
DefTrait(DefId),
3637
DefPrimTy(hir::PrimTy),
@@ -40,14 +41,10 @@ pub enum Def {
4041
usize, // index in the freevars list of the closure
4142
ast::NodeId), // expr node that creates the closure
4243

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

123120
DefFn(..) | DefMod(..) | DefForeignMod(..) | DefStatic(..) |
124-
DefVariant(..) | DefTy(..) | DefAssociatedTy(..) |
121+
DefVariant(..) | DefEnum(..) | DefTyAlias(..) | DefAssociatedTy(..) |
125122
DefTyParam(..) | DefStruct(..) | DefTrait(..) |
126123
DefMethod(..) | DefConst(..) | DefAssociatedConst(..) |
127124
DefPrimTy(..) | DefLabel(..) | DefSelfTy(..) | DefErr => {
@@ -132,8 +129,8 @@ impl Def {
132129

133130
pub fn def_id(&self) -> DefId {
134131
match *self {
135-
DefFn(id, _) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
136-
DefVariant(_, id, _) | DefTy(id, _) | DefAssociatedTy(_, id) |
132+
DefFn(id) | DefMod(id) | DefForeignMod(id) | DefStatic(id, _) |
133+
DefVariant(_, id) | DefEnum(id) | DefTyAlias(id) | DefAssociatedTy(_, id) |
137134
DefTyParam(_, _, id, _) | DefStruct(id) | DefTrait(id) |
138135
DefMethod(id) | DefConst(id) | DefAssociatedConst(id) |
139136
DefLocal(id, _) | DefUpvar(id, _, _, _) => {
@@ -151,7 +148,7 @@ impl Def {
151148

152149
pub fn variant_def_ids(&self) -> Option<(DefId, DefId)> {
153150
match *self {
154-
DefVariant(enum_id, var_id, _) => {
151+
DefVariant(enum_id, var_id) => {
155152
Some((enum_id, var_id))
156153
}
157154
_ => None

src/librustc/middle/expr_use_visitor.rs

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,7 +1077,7 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
10771077
// struct or enum pattern.
10781078
}
10791079

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

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

1116-
Some(def @ def::DefTy(_, true)) => {
1117-
// An enum's type -- should never be in a
1118-
// pattern.
1119-
1120-
if !tcx.sess.has_errors() {
1121-
let msg = format!("Pattern has unexpected type: {:?} and type {:?}",
1122-
def,
1123-
cmt_pat.ty);
1124-
tcx.sess.span_bug(pat.span, &msg)
1125-
}
1126-
}
1127-
11281116
Some(def) => {
1117+
// An enum type should never be in a pattern.
11291118
// Remaining cases are e.g. DefFn, to
11301119
// which identifiers within patterns
11311120
// should not resolve. However, we do

src/librustc/middle/infer/error_reporting.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,7 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
14041404
Some(d) => d.full_def()
14051405
};
14061406
match a_def {
1407-
def::DefTy(did, _) | def::DefStruct(did) => {
1407+
def::DefEnum(did) | def::DefTyAlias(did) | def::DefStruct(did) => {
14081408
let generics = self.tcx.lookup_item_type(did).generics;
14091409

14101410
let expected =
@@ -1452,7 +1452,6 @@ impl<'a, 'tcx> Rebuilder<'a, 'tcx> {
14521452
}
14531453
_ => ()
14541454
}
1455-
14561455
}
14571456

14581457
hir::TyPtr(ref mut_ty) => {

src/librustc/middle/intrinsicck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for IntrinsicCheckingVisitor<'a, 'tcx> {
235235
fn visit_expr(&mut self, expr: &hir::Expr) {
236236
if let hir::ExprPath(..) = expr.node {
237237
match self.tcx.resolve_expr(expr) {
238-
DefFn(did, _) if self.def_id_is_transmute(did) => {
238+
DefFn(did) if self.def_id_is_transmute(did) => {
239239
let typ = self.tcx.node_id_to_type(expr.id);
240240
match typ.sty {
241241
TyBareFn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => {

src/librustc/middle/mem_categorization.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -552,19 +552,14 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
552552
def::DefAssociatedConst(..) | def::DefFn(..) | def::DefMethod(..) => {
553553
Ok(self.cat_rvalue_node(id, span, expr_ty))
554554
}
555+
555556
def::DefMod(_) | def::DefForeignMod(_) |
556-
def::DefTrait(_) | def::DefTy(..) | def::DefPrimTy(_) |
557+
def::DefTrait(_) | def::DefEnum(..) | def::DefTyAlias(..) | def::DefPrimTy(_) |
557558
def::DefTyParam(..) |
558559
def::DefLabel(_) | def::DefSelfTy(..) |
559560
def::DefAssociatedTy(..) => {
560-
Ok(Rc::new(cmt_ {
561-
id:id,
562-
span:span,
563-
cat:Categorization::StaticItem,
564-
mutbl: McImmutable,
565-
ty:expr_ty,
566-
note: NoteNone
567-
}))
561+
self.tcx().sess.span_bug(span, &format!("Unexpected definition in \
562+
memory categorization: {:?}", def));
568563
}
569564

570565
def::DefStatic(_, mutbl) => {
@@ -1218,7 +1213,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> {
12181213
// alone) because struct patterns can refer to struct types or
12191214
// to struct variants within enums.
12201215
let cmt = match opt_def {
1221-
Some(def::DefVariant(enum_did, variant_did, _))
1216+
Some(def::DefVariant(enum_did, variant_did))
12221217
// univariant enums do not need downcasts
12231218
if !self.tcx().lookup_adt_def(enum_did).is_univariant() => {
12241219
self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did)

src/librustc/middle/pat_util.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ pub fn necessary_variants(dm: &DefMap, pat: &hir::Pat) -> Vec<DefId> {
228228
hir::PatIdent(_, _, None) |
229229
hir::PatStruct(..) => {
230230
match dm.get(&p.id) {
231-
Some(&PathResolution { base_def: DefVariant(_, id, _), .. }) => {
231+
Some(&PathResolution { base_def: DefVariant(_, id), .. }) => {
232232
variants.push(id);
233233
}
234234
_ => ()

src/librustc/middle/ty/mod.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,9 +1429,19 @@ impl<'tcx> Decodable for AdtDef<'tcx> {
14291429
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
14301430
pub enum AdtKind { Struct, Enum }
14311431

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

1435+
impl VariantKind {
1436+
pub fn from_variant_data(vdata: &hir::VariantData) -> Self {
1437+
match *vdata {
1438+
hir::VariantData::Struct(..) => VariantKind::Struct,
1439+
hir::VariantData::Tuple(..) => VariantKind::Tuple,
1440+
hir::VariantData::Unit(..) => VariantKind::Unit,
1441+
}
1442+
}
1443+
}
1444+
14351445
impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
14361446
fn new(tcx: &ctxt<'tcx>,
14371447
did: DefId,
@@ -1577,8 +1587,8 @@ impl<'tcx, 'container> AdtDefData<'tcx, 'container> {
15771587

15781588
pub fn variant_of_def(&self, def: def::Def) -> &VariantDefData<'tcx, 'container> {
15791589
match def {
1580-
def::DefVariant(_, vid, _) => self.variant_with_id(vid),
1581-
def::DefStruct(..) | def::DefTy(..) => self.struct_variant(),
1590+
def::DefVariant(_, vid) => self.variant_with_id(vid),
1591+
def::DefStruct(..) | def::DefTyAlias(..) => self.struct_variant(),
15821592
_ => panic!("unexpected def {:?} in variant_of_def", def)
15831593
}
15841594
}

src/librustc_lint/builtin.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1060,7 +1060,7 @@ impl LateLintPass for MutableTransmutes {
10601060
hir::ExprPath(..) => (),
10611061
_ => return None
10621062
}
1063-
if let def::DefFn(did, _) = cx.tcx.resolve_expr(expr) {
1063+
if let def::DefFn(did) = cx.tcx.resolve_expr(expr) {
10641064
if !def_id_is_transmute(cx, did) {
10651065
return None;
10661066
}

src/librustc_metadata/astencode.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -376,7 +376,7 @@ fn decode_def(dcx: &DecodeContext, dsr: &mut reader::Decoder) -> def::Def {
376376
impl tr for def::Def {
377377
fn tr(&self, dcx: &DecodeContext) -> def::Def {
378378
match *self {
379-
def::DefFn(did, is_ctor) => def::DefFn(did.tr(dcx), is_ctor),
379+
def::DefFn(did) => def::DefFn(did.tr(dcx)),
380380
def::DefMethod(did) => def::DefMethod(did.tr(dcx)),
381381
def::DefSelfTy(opt_did, impl_ids) => { def::DefSelfTy(opt_did.map(|did| did.tr(dcx)),
382382
impl_ids.map(|(nid1, nid2)| {
@@ -393,11 +393,10 @@ impl tr for def::Def {
393393
let did = dcx.tcx.map.local_def_id(nid);
394394
def::DefLocal(did, nid)
395395
}
396-
def::DefVariant(e_did, v_did, is_s) => {
397-
def::DefVariant(e_did.tr(dcx), v_did.tr(dcx), is_s)
398-
},
396+
def::DefVariant(e_did, v_did) => def::DefVariant(e_did.tr(dcx), v_did.tr(dcx)),
399397
def::DefTrait(did) => def::DefTrait(did.tr(dcx)),
400-
def::DefTy(did, is_enum) => def::DefTy(did.tr(dcx), is_enum),
398+
def::DefEnum(did) => def::DefEnum(did.tr(dcx)),
399+
def::DefTyAlias(did) => def::DefTyAlias(did.tr(dcx)),
401400
def::DefAssociatedTy(trait_did, did) =>
402401
def::DefAssociatedTy(trait_did.tr(dcx), did.tr(dcx)),
403402
def::DefPrimTy(p) => def::DefPrimTy(p),

0 commit comments

Comments
 (0)