Skip to content

Commit 764310e

Browse files
Ariel Ben-Yehudaarielb1
authored andcommitted
introduce an ADTDef struct for struct/enum definitions
1 parent 03ee3f5 commit 764310e

Some content is hidden

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

46 files changed

+654
-550
lines changed

src/librustc/metadata/csearch.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,12 @@ pub fn get_trait_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId) -> ty::TraitDe
222222
decoder::get_trait_def(&*cdata, def.node, tcx)
223223
}
224224

225+
pub fn get_adt_def<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId) -> &'tcx ty::ADTDef<'tcx> {
226+
let cstore = &tcx.sess.cstore;
227+
let cdata = cstore.get_crate_data(def.krate);
228+
decoder::get_adt_def(&*cdata, def.node, tcx)
229+
}
230+
225231
pub fn get_predicates<'tcx>(tcx: &ty::ctxt<'tcx>, def: ast::DefId)
226232
-> ty::GenericPredicates<'tcx>
227233
{

src/librustc/metadata/decoder.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,13 @@ pub fn get_trait_def<'tcx>(cdata: Cmd,
390390
}
391391
}
392392

393+
pub fn get_adt_def<'tcx>(cdata: Cmd,
394+
item_id: ast::NodeId,
395+
tcx: &ty::ctxt<'tcx>) -> &'tcx ty::ADTDef<'tcx>
396+
{
397+
tcx.intern_adt_def(ast::DefId { krate: cdata.cnum, node: item_id })
398+
}
399+
393400
pub fn get_predicates<'tcx>(cdata: Cmd,
394401
item_id: ast::NodeId,
395402
tcx: &ty::ctxt<'tcx>)

src/librustc/metadata/tydecode.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,10 @@ fn parse_ty_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F) -> Ty<'tcx> w
468468
'c' => return tcx.types.char,
469469
't' => {
470470
assert_eq!(next(st), '[');
471-
let def = parse_def_(st, NominalType, conv);
471+
let did = parse_def_(st, NominalType, conv);
472472
let substs = parse_substs_(st, conv);
473473
assert_eq!(next(st), ']');
474+
let def = st.tcx.lookup_adt_def(did);
474475
return tcx.mk_enum(def, st.tcx.mk_substs(substs));
475476
}
476477
'x' => {
@@ -558,7 +559,8 @@ fn parse_ty_<'a, 'tcx, F>(st: &mut PState<'a, 'tcx>, conv: &mut F) -> Ty<'tcx> w
558559
let did = parse_def_(st, NominalType, conv);
559560
let substs = parse_substs_(st, conv);
560561
assert_eq!(next(st), ']');
561-
return st.tcx.mk_struct(did, st.tcx.mk_substs(substs));
562+
let def = st.tcx.lookup_adt_def(did);
563+
return st.tcx.mk_struct(def, st.tcx.mk_substs(substs));
562564
}
563565
'k' => {
564566
assert_eq!(next(st), '[');

src/librustc/metadata/tyencode.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
8686
}
8787
}
8888
ty::TyEnum(def, substs) => {
89-
mywrite!(w, "t[{}|", (cx.ds)(def));
89+
mywrite!(w, "t[{}|", (cx.ds)(def.did));
9090
enc_substs(w, cx, substs);
9191
mywrite!(w, "]");
9292
}
@@ -138,7 +138,7 @@ pub fn enc_ty<'a, 'tcx>(w: &mut Encoder, cx: &ctxt<'a, 'tcx>, t: Ty<'tcx>) {
138138
mywrite!(w, "p[{}|{}|{}]", idx, space.to_uint(), name)
139139
}
140140
ty::TyStruct(def, substs) => {
141-
mywrite!(w, "a[{}|", (cx.ds)(def));
141+
mywrite!(w, "a[{}|", (cx.ds)(def.did));
142142
enc_substs(w, cx, substs);
143143
mywrite!(w, "]");
144144
}

src/librustc/middle/check_const.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
546546
fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>,
547547
e: &ast::Expr, node_ty: Ty<'tcx>) {
548548
match node_ty.sty {
549-
ty::TyStruct(did, _) |
550-
ty::TyEnum(did, _) if v.tcx.has_dtor(did) => {
549+
ty::TyStruct(def, _) |
550+
ty::TyEnum(def, _) if def.has_dtor(v.tcx) => {
551551
v.add_qualif(ConstQualif::NEEDS_DROP);
552552
if v.mode != Mode::Var {
553553
v.tcx.sess.span_err(e.span,

src/librustc/middle/check_match.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,10 @@ fn check_for_bindings_named_the_same_as_variants(cx: &MatchCheckCtxt, pat: &Pat)
234234
match p.node {
235235
ast::PatIdent(ast::BindByValue(ast::MutImmutable), ident, None) => {
236236
let pat_ty = cx.tcx.pat_ty(p);
237-
if let ty::TyEnum(def_id, _) = pat_ty.sty {
237+
if let ty::TyEnum(edef, _) = pat_ty.sty {
238238
let def = cx.tcx.def_map.borrow().get(&p.id).map(|d| d.full_def());
239239
if let Some(DefLocal(_)) = def {
240-
if cx.tcx.enum_variants(def_id).iter().any(|variant|
240+
if cx.tcx.enum_variants(edef.did).iter().any(|variant|
241241
variant.name == ident.node.name
242242
&& variant.args.is_empty()
243243
) {
@@ -501,8 +501,8 @@ impl<'a, 'tcx> Folder for StaticInliner<'a, 'tcx> {
501501
///
502502
/// left_ty: struct X { a: (bool, &'static str), b: usize}
503503
/// pats: [(false, "foo"), 42] => X { a: (false, "foo"), b: 42 }
504-
fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
505-
pats: Vec<&Pat>, left_ty: Ty) -> P<Pat> {
504+
fn construct_witness<'a,'tcx>(cx: &MatchCheckCtxt<'a,'tcx>, ctor: &Constructor,
505+
pats: Vec<&Pat>, left_ty: Ty<'tcx>) -> P<Pat> {
506506
let pats_len = pats.len();
507507
let mut pats = pats.into_iter().map(|p| P((*p).clone()));
508508
let pat = match left_ty.sty {
@@ -511,9 +511,9 @@ fn construct_witness(cx: &MatchCheckCtxt, ctor: &Constructor,
511511
ty::TyEnum(cid, _) | ty::TyStruct(cid, _) => {
512512
let (vid, is_structure) = match ctor {
513513
&Variant(vid) =>
514-
(vid, cx.tcx.enum_variant_with_id(cid, vid).arg_names.is_some()),
514+
(vid, cx.tcx.enum_variant_with_id(cid.did, vid).arg_names.is_some()),
515515
_ =>
516-
(cid, !cx.tcx.is_tuple_struct(cid))
516+
(cid.did, !cid.is_tuple_struct(cx.tcx))
517517
};
518518
if is_structure {
519519
let fields = cx.tcx.lookup_struct_fields(vid);
@@ -606,8 +606,8 @@ fn all_constructors(cx: &MatchCheckCtxt, left_ty: Ty,
606606
_ => vec!(Single)
607607
},
608608

609-
ty::TyEnum(eid, _) =>
610-
cx.tcx.enum_variants(eid)
609+
ty::TyEnum(edef, _) =>
610+
cx.tcx.enum_variants(edef.did)
611611
.iter()
612612
.map(|va| Variant(va.id))
613613
.collect(),
@@ -817,13 +817,13 @@ pub fn constructor_arity(cx: &MatchCheckCtxt, ctor: &Constructor, ty: Ty) -> usi
817817
ty::TyStr => 0,
818818
_ => 1
819819
},
820-
ty::TyEnum(eid, _) => {
820+
ty::TyEnum(edef, _) => {
821821
match *ctor {
822-
Variant(id) => cx.tcx.enum_variant_with_id(eid, id).args.len(),
822+
Variant(id) => cx.tcx.enum_variant_with_id(edef.did, id).args.len(),
823823
_ => unreachable!()
824824
}
825825
}
826-
ty::TyStruct(cid, _) => cx.tcx.lookup_struct_fields(cid).len(),
826+
ty::TyStruct(cdef, _) => cx.tcx.lookup_struct_fields(cdef.did).len(),
827827
ty::TyArray(_, n) => n,
828828
_ => 0
829829
}

src/librustc/middle/dead.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
101101

102102
fn handle_field_access(&mut self, lhs: &ast::Expr, name: ast::Name) {
103103
match self.tcx.expr_ty_adjusted(lhs).sty {
104-
ty::TyStruct(id, _) => {
105-
let fields = self.tcx.lookup_struct_fields(id);
104+
ty::TyStruct(def, _) => {
105+
let fields = self.tcx.lookup_struct_fields(def.did);
106106
let field_id = fields.iter()
107107
.find(|field| field.name == name).unwrap().id;
108108
self.live_symbols.insert(field_id.node);
@@ -113,8 +113,8 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
113113

114114
fn handle_tup_field_access(&mut self, lhs: &ast::Expr, idx: usize) {
115115
match self.tcx.expr_ty_adjusted(lhs).sty {
116-
ty::TyStruct(id, _) => {
117-
let fields = self.tcx.lookup_struct_fields(id);
116+
ty::TyStruct(def, _) => {
117+
let fields = self.tcx.lookup_struct_fields(def.did);
118118
let field_id = fields[idx].id;
119119
self.live_symbols.insert(field_id.node);
120120
},

src/librustc/middle/expr_use_visitor.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,8 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
695695
// Select just those fields of the `with`
696696
// expression that will actually be used
697697
let with_fields = match with_cmt.ty.sty {
698-
ty::TyStruct(did, substs) => {
699-
self.tcx().struct_fields(did, substs)
698+
ty::TyStruct(def, substs) => {
699+
self.tcx().struct_fields(def.did, substs)
700700
}
701701
_ => {
702702
// the base expression should always evaluate to a
@@ -708,7 +708,6 @@ impl<'d,'t,'a,'tcx> ExprUseVisitor<'d,'t,'a,'tcx> {
708708
with_expr.span,
709709
"with expression doesn't evaluate to a struct");
710710
}
711-
assert!(self.tcx().sess.has_errors());
712711
vec!()
713712
}
714713
};

src/librustc/middle/fast_reject.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ pub fn simplify_type(tcx: &ty::ctxt,
5353
ty::TyInt(int_type) => Some(IntSimplifiedType(int_type)),
5454
ty::TyUint(uint_type) => Some(UintSimplifiedType(uint_type)),
5555
ty::TyFloat(float_type) => Some(FloatSimplifiedType(float_type)),
56-
ty::TyEnum(def_id, _) => Some(EnumSimplifiedType(def_id)),
56+
ty::TyEnum(def, _) => Some(EnumSimplifiedType(def.did)),
5757
ty::TyStr => Some(StrSimplifiedType),
5858
ty::TyArray(..) | ty::TySlice(_) => Some(VecSimplifiedType),
5959
ty::TyRawPtr(_) => Some(PtrSimplifiedType),
6060
ty::TyTrait(ref trait_info) => {
6161
Some(TraitSimplifiedType(trait_info.principal_def_id()))
6262
}
63-
ty::TyStruct(def_id, _) => {
64-
Some(StructSimplifiedType(def_id))
63+
ty::TyStruct(def, _) => {
64+
Some(StructSimplifiedType(def.did))
6565
}
6666
ty::TyRef(_, mt) => {
6767
// since we introduce auto-refs during method lookup, we

src/librustc/middle/implicator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ impl<'a, 'tcx> Implicator<'a, 'tcx> {
144144
self.accumulate_from_object_ty(ty, t.bounds.region_bound, required_region_bounds)
145145
}
146146

147-
ty::TyEnum(def_id, substs) |
148-
ty::TyStruct(def_id, substs) => {
149-
let item_scheme = self.tcx().lookup_item_type(def_id);
150-
self.accumulate_from_adt(ty, def_id, &item_scheme.generics, substs)
147+
ty::TyEnum(def, substs) |
148+
ty::TyStruct(def, substs) => {
149+
let item_scheme = def.type_scheme(self.tcx());
150+
self.accumulate_from_adt(ty, def.did, &item_scheme.generics, substs)
151151
}
152152

153153
ty::TyArray(t, _) |

0 commit comments

Comments
 (0)