Skip to content

Commit f1f40f8

Browse files
authored
Auto merge of #36331 - petrochenkov:tyadt, r=eddyb
Refactor `TyStruct`/`TyEnum`/`TyUnion` into `TyAdt` r? @eddyb
2 parents 3344f89 + 553d5f0 commit f1f40f8

Some content is hidden

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

77 files changed

+912
-1059
lines changed

src/librustc/infer/error_reporting.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
488488
// if they are both "path types", there's a chance of ambiguity
489489
// due to different versions of the same crate
490490
match (&exp_found.expected.sty, &exp_found.found.sty) {
491-
(&ty::TyEnum(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) |
492-
(&ty::TyStruct(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
493-
(&ty::TyEnum(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
494-
(&ty::TyStruct(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) => {
491+
(&ty::TyAdt(exp_adt, _), &ty::TyAdt(found_adt, _)) => {
495492
report_path_match(err, exp_adt.did, found_adt.did);
496493
},
497494
_ => ()

src/librustc/infer/freshen.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
156156
ty::TyInt(..) |
157157
ty::TyUint(..) |
158158
ty::TyFloat(..) |
159-
ty::TyEnum(..) |
159+
ty::TyAdt(..) |
160160
ty::TyBox(..) |
161161
ty::TyStr |
162162
ty::TyError |
@@ -167,8 +167,6 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
167167
ty::TyFnDef(..) |
168168
ty::TyFnPtr(_) |
169169
ty::TyTrait(..) |
170-
ty::TyStruct(..) |
171-
ty::TyUnion(..) |
172170
ty::TyClosure(..) |
173171
ty::TyNever |
174172
ty::TyTuple(..) |

src/librustc/middle/dead.rs

+11-12
Original file line numberDiff line numberDiff line change
@@ -86,20 +86,15 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
8686
}
8787

8888
fn lookup_and_handle_definition(&mut self, id: ast::NodeId) {
89-
use ty::TypeVariants::{TyEnum, TyStruct, TyUnion};
90-
9189
let def = self.tcx.expect_def(id);
9290

9391
// If `bar` is a trait item, make sure to mark Foo as alive in `Foo::bar`
9492
match def {
9593
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_)
9694
if self.tcx.trait_of_item(def.def_id()).is_some() => {
9795
if let Some(substs) = self.tcx.tables.borrow().item_substs.get(&id) {
98-
match substs.substs.type_at(0).sty {
99-
TyEnum(tyid, _) | TyStruct(tyid, _) | TyUnion(tyid, _) => {
100-
self.check_def_id(tyid.did)
101-
}
102-
_ => {}
96+
if let ty::TyAdt(tyid, _) = substs.substs.type_at(0).sty {
97+
self.check_def_id(tyid.did);
10398
}
10499
}
105100
}
@@ -133,23 +128,27 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
133128

134129
fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
135130
match self.tcx.expr_ty_adjusted(lhs).sty {
136-
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
131+
ty::TyAdt(def, _) => {
137132
self.insert_def_id(def.struct_variant().field_named(name).did);
138133
}
139-
_ => span_bug!(lhs.span, "named field access on non-struct/union"),
134+
_ => span_bug!(lhs.span, "named field access on non-ADT"),
140135
}
141136
}
142137

143138
fn handle_tup_field_access(&mut self, lhs: &hir::Expr, idx: usize) {
144-
if let ty::TyStruct(def, _) = self.tcx.expr_ty_adjusted(lhs).sty {
145-
self.insert_def_id(def.struct_variant().fields[idx].did);
139+
match self.tcx.expr_ty_adjusted(lhs).sty {
140+
ty::TyAdt(def, _) => {
141+
self.insert_def_id(def.struct_variant().fields[idx].did);
142+
}
143+
ty::TyTuple(..) => {}
144+
_ => span_bug!(lhs.span, "numeric field access on non-ADT"),
146145
}
147146
}
148147

149148
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat,
150149
pats: &[codemap::Spanned<hir::FieldPat>]) {
151150
let variant = match self.tcx.node_id_to_type(lhs.id).sty {
152-
ty::TyStruct(adt, _) | ty::TyUnion(adt, _) | ty::TyEnum(adt, _) => {
151+
ty::TyAdt(adt, _) => {
153152
adt.variant_of_def(self.tcx.expect_def(lhs.id))
154153
}
155154
_ => span_bug!(lhs.span, "non-ADT in struct pattern")

src/librustc/middle/effect.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,10 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
200200
}
201201
}
202202
hir::ExprField(ref base_expr, field) => {
203-
if let ty::TyUnion(..) = self.tcx.expr_ty_adjusted(base_expr).sty {
204-
self.require_unsafe(field.span, "access to union field");
203+
if let ty::TyAdt(adt, ..) = self.tcx.expr_ty_adjusted(base_expr).sty {
204+
if adt.is_union() {
205+
self.require_unsafe(field.span, "access to union field");
206+
}
205207
}
206208
}
207209
_ => {}
@@ -212,9 +214,11 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
212214

213215
fn visit_pat(&mut self, pat: &hir::Pat) {
214216
if let PatKind::Struct(_, ref fields, _) = pat.node {
215-
if let ty::TyUnion(..) = self.tcx.pat_ty(pat).sty {
216-
for field in fields {
217-
self.require_unsafe(field.span, "matching on union field");
217+
if let ty::TyAdt(adt, ..) = self.tcx.pat_ty(pat).sty {
218+
if adt.is_union() {
219+
for field in fields {
220+
self.require_unsafe(field.span, "matching on union field");
221+
}
218222
}
219223
}
220224
}

src/librustc/middle/expr_use_visitor.rs

+23-20
Original file line numberDiff line numberDiff line change
@@ -671,28 +671,31 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
671671

672672
// Select just those fields of the `with`
673673
// expression that will actually be used
674-
if let ty::TyStruct(def, substs) = with_cmt.ty.sty {
675-
// Consume those fields of the with expression that are needed.
676-
for with_field in &def.struct_variant().fields {
677-
if !contains_field_named(with_field, fields) {
678-
let cmt_field = self.mc.cat_field(
679-
&*with_expr,
680-
with_cmt.clone(),
681-
with_field.name,
682-
with_field.ty(self.tcx(), substs)
683-
);
684-
self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
674+
match with_cmt.ty.sty {
675+
ty::TyAdt(adt, substs) if adt.is_struct() => {
676+
// Consume those fields of the with expression that are needed.
677+
for with_field in &adt.struct_variant().fields {
678+
if !contains_field_named(with_field, fields) {
679+
let cmt_field = self.mc.cat_field(
680+
&*with_expr,
681+
with_cmt.clone(),
682+
with_field.name,
683+
with_field.ty(self.tcx(), substs)
684+
);
685+
self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
686+
}
685687
}
686688
}
687-
} else {
688-
// the base expression should always evaluate to a
689-
// struct; however, when EUV is run during typeck, it
690-
// may not. This will generate an error earlier in typeck,
691-
// so we can just ignore it.
692-
if !self.tcx().sess.has_errors() {
693-
span_bug!(
694-
with_expr.span,
695-
"with expression doesn't evaluate to a struct");
689+
_ => {
690+
// the base expression should always evaluate to a
691+
// struct; however, when EUV is run during typeck, it
692+
// may not. This will generate an error earlier in typeck,
693+
// so we can just ignore it.
694+
if !self.tcx().sess.has_errors() {
695+
span_bug!(
696+
with_expr.span,
697+
"with expression doesn't evaluate to a struct");
698+
}
696699
}
697700
}
698701

src/librustc/middle/mem_categorization.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -223,8 +223,7 @@ fn deref_kind(t: Ty, context: DerefKindContext) -> McResult<deref_kind> {
223223
Ok(deref_ptr(UnsafePtr(mt.mutbl)))
224224
}
225225

226-
ty::TyEnum(..) |
227-
ty::TyStruct(..) => { // newtype
226+
ty::TyAdt(..) => { // newtype
228227
Ok(deref_interior(InteriorField(PositionalField(0))))
229228
}
230229

@@ -1154,7 +1153,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
11541153
}
11551154
Def::Struct(..) => {
11561155
match self.pat_ty(&pat)?.sty {
1157-
ty::TyStruct(adt_def, _) => {
1156+
ty::TyAdt(adt_def, _) => {
11581157
adt_def.struct_variant().fields.len()
11591158
}
11601159
ref ty => {

src/librustc/middle/stability.rs

+30-32
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use lint;
2020
use middle::cstore::LOCAL_CRATE;
2121
use hir::def::Def;
2222
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
23-
use ty::{self, TyCtxt};
23+
use ty::{self, TyCtxt, AdtKind};
2424
use middle::privacy::AccessLevels;
2525
use syntax::parse::token::InternedString;
2626
use syntax_pos::{Span, DUMMY_SP};
@@ -561,49 +561,48 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
561561
hir::ExprField(ref base_e, ref field) => {
562562
span = field.span;
563563
match tcx.expr_ty_adjusted(base_e).sty {
564-
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
564+
ty::TyAdt(def, _) => {
565565
def.struct_variant().field_named(field.node).did
566566
}
567567
_ => span_bug!(e.span,
568-
"stability::check_expr: named field access on non-struct/union")
568+
"stability::check_expr: named field access on non-ADT")
569569
}
570570
}
571571
hir::ExprTupField(ref base_e, ref field) => {
572572
span = field.span;
573573
match tcx.expr_ty_adjusted(base_e).sty {
574-
ty::TyStruct(def, _) => def.struct_variant().fields[field.node].did,
574+
ty::TyAdt(def, _) => {
575+
def.struct_variant().fields[field.node].did
576+
}
575577
ty::TyTuple(..) => return,
576578
_ => span_bug!(e.span,
577579
"stability::check_expr: unnamed field access on \
578580
something other than a tuple or struct")
579581
}
580582
}
581583
hir::ExprStruct(_, ref expr_fields, _) => {
582-
let type_ = tcx.expr_ty(e);
583-
match type_.sty {
584-
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
585-
// check the stability of each field that appears
586-
// in the construction expression.
587-
for field in expr_fields {
588-
let did = def.struct_variant()
589-
.field_named(field.name.node)
590-
.did;
591-
maybe_do_stability_check(tcx, did, field.span, cb);
592-
}
584+
match tcx.expr_ty(e).sty {
585+
ty::TyAdt(adt, ..) => match adt.adt_kind() {
586+
AdtKind::Struct | AdtKind::Union => {
587+
// check the stability of each field that appears
588+
// in the construction expression.
589+
for field in expr_fields {
590+
let did = adt.struct_variant().field_named(field.name.node).did;
591+
maybe_do_stability_check(tcx, did, field.span, cb);
592+
}
593593

594-
// we're done.
595-
return
596-
}
597-
// we don't look at stability attributes on
598-
// struct-like enums (yet...), but it's definitely not
599-
// a bug to have construct one.
600-
ty::TyEnum(..) => return,
601-
_ => {
602-
span_bug!(e.span,
603-
"stability::check_expr: struct construction \
604-
of non-struct/union, type {:?}",
605-
type_);
606-
}
594+
// we're done.
595+
return
596+
}
597+
AdtKind::Enum => {
598+
// we don't look at stability attributes on
599+
// struct-like enums (yet...), but it's definitely not
600+
// a bug to have construct one.
601+
return
602+
}
603+
},
604+
ref ty => span_bug!(e.span, "stability::check_expr: struct \
605+
construction of non-ADT type: {:?}", ty)
607606
}
608607
}
609608
_ => return
@@ -648,10 +647,9 @@ pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat,
648647
debug!("check_pat(pat = {:?})", pat);
649648
if is_internal(tcx, pat.span) { return; }
650649

651-
let v = match tcx.pat_ty_opt(pat) {
652-
Some(&ty::TyS { sty: ty::TyStruct(def, _), .. }) |
653-
Some(&ty::TyS { sty: ty::TyUnion(def, _), .. }) => def.struct_variant(),
654-
Some(_) | None => return,
650+
let v = match tcx.pat_ty_opt(pat).map(|ty| &ty.sty) {
651+
Some(&ty::TyAdt(adt, _)) if !adt.is_enum() => adt.struct_variant(),
652+
_ => return,
655653
};
656654
match pat.node {
657655
// Foo(a, b, c)

src/librustc/mir/tcx.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
4040
LvalueTy::Ty { ty } =>
4141
ty,
4242
LvalueTy::Downcast { adt_def, substs, variant_index: _ } =>
43-
tcx.mk_enum(adt_def, substs),
43+
tcx.mk_adt(adt_def, substs),
4444
}
4545
}
4646

@@ -75,15 +75,16 @@ impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
7575
}
7676
ProjectionElem::Downcast(adt_def1, index) =>
7777
match self.to_ty(tcx).sty {
78-
ty::TyEnum(adt_def, substs) => {
78+
ty::TyAdt(adt_def, substs) => {
79+
assert!(adt_def.is_enum());
7980
assert!(index < adt_def.variants.len());
8081
assert_eq!(adt_def, adt_def1);
8182
LvalueTy::Downcast { adt_def: adt_def,
8283
substs: substs,
8384
variant_index: index }
8485
}
8586
_ => {
86-
bug!("cannot downcast non-enum type: `{:?}`", self)
87+
bug!("cannot downcast non-ADT type: `{:?}`", self)
8788
}
8889
},
8990
ProjectionElem::Field(_, fty) => LvalueTy::Ty { ty: fty }

src/librustc/traits/coherence.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
224224
match ty.sty {
225225
ty::TyBox(..) | ty::TyRef(..) =>
226226
true,
227-
ty::TyEnum(def, _) | ty::TyStruct(def, _) | ty::TyUnion(def, _) =>
227+
ty::TyAdt(def, _) =>
228228
def.is_fundamental(),
229229
ty::TyTrait(ref data) =>
230230
tcx.has_attr(data.principal.def_id(), "fundamental"),
@@ -260,9 +260,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
260260
infer_is_local.0
261261
}
262262

263-
ty::TyEnum(def, _) |
264-
ty::TyStruct(def, _) |
265-
ty::TyUnion(def, _) => {
263+
ty::TyAdt(def, _) => {
266264
def.did.is_local()
267265
}
268266

src/librustc/traits/error_reporting.rs

+18-20
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use super::{
2727
use fmt_macros::{Parser, Piece, Position};
2828
use hir::def_id::DefId;
2929
use infer::{self, InferCtxt, TypeOrigin};
30-
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
30+
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
3131
use ty::error::ExpectedFound;
3232
use ty::fast_reject;
3333
use ty::fold::TypeFolder;
@@ -151,32 +151,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
151151
ty::TyBool => Some(0),
152152
ty::TyChar => Some(1),
153153
ty::TyStr => Some(2),
154-
ty::TyInt(..) | ty::TyUint(..) |
155-
ty::TyInfer(ty::IntVar(..)) => Some(3),
154+
ty::TyInt(..) | ty::TyUint(..) | ty::TyInfer(ty::IntVar(..)) => Some(3),
156155
ty::TyFloat(..) | ty::TyInfer(ty::FloatVar(..)) => Some(4),
157-
ty::TyEnum(..) => Some(5),
158-
ty::TyStruct(..) => Some(6),
159-
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(7),
160-
ty::TyArray(..) | ty::TySlice(..) => Some(8),
161-
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(9),
162-
ty::TyTrait(..) => Some(10),
163-
ty::TyClosure(..) => Some(11),
164-
ty::TyTuple(..) => Some(12),
165-
ty::TyProjection(..) => Some(13),
166-
ty::TyParam(..) => Some(14),
167-
ty::TyAnon(..) => Some(15),
168-
ty::TyNever => Some(16),
169-
ty::TyUnion(..) => Some(17),
156+
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
157+
ty::TyArray(..) | ty::TySlice(..) => Some(6),
158+
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(7),
159+
ty::TyTrait(..) => Some(8),
160+
ty::TyClosure(..) => Some(9),
161+
ty::TyTuple(..) => Some(10),
162+
ty::TyProjection(..) => Some(11),
163+
ty::TyParam(..) => Some(12),
164+
ty::TyAnon(..) => Some(13),
165+
ty::TyNever => Some(14),
166+
ty::TyAdt(adt, ..) => match adt.adt_kind() {
167+
AdtKind::Struct => Some(15),
168+
AdtKind::Union => Some(16),
169+
AdtKind::Enum => Some(17),
170+
},
170171
ty::TyInfer(..) | ty::TyError => None
171172
}
172173
}
173174

174175
match (type_category(a), type_category(b)) {
175176
(Some(cat_a), Some(cat_b)) => match (&a.sty, &b.sty) {
176-
(&ty::TyStruct(def_a, _), &ty::TyStruct(def_b, _)) |
177-
(&ty::TyUnion(def_a, _), &ty::TyUnion(def_b, _)) |
178-
(&ty::TyEnum(def_a, _), &ty::TyEnum(def_b, _)) =>
179-
def_a == def_b,
177+
(&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) => def_a == def_b,
180178
_ => cat_a == cat_b
181179
},
182180
// infer and error can be equated to all types

0 commit comments

Comments
 (0)