Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor TyStruct/TyEnum/TyUnion into TyAdt #36331

Merged
merged 2 commits into from
Sep 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/librustc/infer/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
// if they are both "path types", there's a chance of ambiguity
// due to different versions of the same crate
match (&exp_found.expected.sty, &exp_found.found.sty) {
(&ty::TyEnum(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) |
(&ty::TyStruct(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
(&ty::TyEnum(ref exp_adt, _), &ty::TyStruct(ref found_adt, _)) |
(&ty::TyStruct(ref exp_adt, _), &ty::TyEnum(ref found_adt, _)) => {
(&ty::TyAdt(exp_adt, _), &ty::TyAdt(found_adt, _)) => {
report_path_match(err, exp_adt.did, found_adt.did);
},
_ => ()
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/infer/freshen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::TyInt(..) |
ty::TyUint(..) |
ty::TyFloat(..) |
ty::TyEnum(..) |
ty::TyAdt(..) |
ty::TyBox(..) |
ty::TyStr |
ty::TyError |
Expand All @@ -167,8 +167,6 @@ impl<'a, 'gcx, 'tcx> TypeFolder<'gcx, 'tcx> for TypeFreshener<'a, 'gcx, 'tcx> {
ty::TyFnDef(..) |
ty::TyFnPtr(_) |
ty::TyTrait(..) |
ty::TyStruct(..) |
ty::TyUnion(..) |
ty::TyClosure(..) |
ty::TyNever |
ty::TyTuple(..) |
Expand Down
21 changes: 10 additions & 11 deletions src/librustc/middle/dead.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,15 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}

fn lookup_and_handle_definition(&mut self, id: ast::NodeId) {
use ty::TypeVariants::{TyEnum, TyStruct, TyUnion};

let def = self.tcx.expect_def(id);

// If `bar` is a trait item, make sure to mark Foo as alive in `Foo::bar`
match def {
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_)
if self.tcx.trait_of_item(def.def_id()).is_some() => {
if let Some(substs) = self.tcx.tables.borrow().item_substs.get(&id) {
match substs.substs.type_at(0).sty {
TyEnum(tyid, _) | TyStruct(tyid, _) | TyUnion(tyid, _) => {
self.check_def_id(tyid.did)
}
_ => {}
if let ty::TyAdt(tyid, _) = substs.substs.type_at(0).sty {
self.check_def_id(tyid.did);
}
}
}
Expand Down Expand Up @@ -133,23 +128,27 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {

fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
match self.tcx.expr_ty_adjusted(lhs).sty {
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
ty::TyAdt(def, _) => {
self.insert_def_id(def.struct_variant().field_named(name).did);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens here if sty here was a TyEnum previously?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct_variant() reports another span_bug!

}
_ => span_bug!(lhs.span, "named field access on non-struct/union"),
_ => span_bug!(lhs.span, "named field access on non-ADT"),
}
}

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

fn handle_field_pattern_match(&mut self, lhs: &hir::Pat,
pats: &[codemap::Spanned<hir::FieldPat>]) {
let variant = match self.tcx.node_id_to_type(lhs.id).sty {
ty::TyStruct(adt, _) | ty::TyUnion(adt, _) | ty::TyEnum(adt, _) => {
ty::TyAdt(adt, _) => {
adt.variant_of_def(self.tcx.expect_def(lhs.id))
}
_ => span_bug!(lhs.span, "non-ADT in struct pattern")
Expand Down
8 changes: 6 additions & 2 deletions src/librustc/middle/effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,12 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
}
}
hir::ExprField(ref base_expr, field) => {
if let ty::TyUnion(..) = self.tcx.expr_ty_adjusted(base_expr).sty {
if let ty::TyAdt(adt, ..) = self.tcx.expr_ty_adjusted(base_expr).sty {
if adt.is_union() {
self.require_unsafe(field.span, "access to union field");
}
}
}
_ => {}
}

Expand All @@ -190,12 +192,14 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {

fn visit_pat(&mut self, pat: &hir::Pat) {
if let PatKind::Struct(_, ref fields, _) = pat.node {
if let ty::TyUnion(..) = self.tcx.pat_ty(pat).sty {
if let ty::TyAdt(adt, ..) = self.tcx.pat_ty(pat).sty {
if adt.is_union() {
for field in fields {
self.require_unsafe(field.span, "matching on union field");
}
}
}
}

intravisit::walk_pat(self, pat);
}
Expand Down
9 changes: 6 additions & 3 deletions src/librustc/middle/expr_use_visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,9 +671,10 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {

// Select just those fields of the `with`
// expression that will actually be used
if let ty::TyStruct(def, substs) = with_cmt.ty.sty {
match with_cmt.ty.sty {
ty::TyAdt(adt, substs) if adt.is_struct() => {
// Consume those fields of the with expression that are needed.
for with_field in &def.struct_variant().fields {
for with_field in &adt.struct_variant().fields {
if !contains_field_named(with_field, fields) {
let cmt_field = self.mc.cat_field(
&*with_expr,
Expand All @@ -684,7 +685,8 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
self.delegate_consume(with_expr.id, with_expr.span, cmt_field);
}
}
} else {
}
_ => {
// the base expression should always evaluate to a
// struct; however, when EUV is run during typeck, it
// may not. This will generate an error earlier in typeck,
Expand All @@ -695,6 +697,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
"with expression doesn't evaluate to a struct");
}
}
}

// walk the with expression so that complex expressions
// are properly handled.
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/mem_categorization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,7 @@ fn deref_kind(t: Ty, context: DerefKindContext) -> McResult<deref_kind> {
Ok(deref_ptr(UnsafePtr(mt.mutbl)))
}

ty::TyEnum(..) |
ty::TyStruct(..) => { // newtype
ty::TyAdt(..) => { // newtype
Ok(deref_interior(InteriorField(PositionalField(0))))
}

Expand Down Expand Up @@ -1154,7 +1153,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
}
Def::Struct(..) => {
match self.pat_ty(&pat)?.sty {
ty::TyStruct(adt_def, _) => {
ty::TyAdt(adt_def, _) => {
adt_def.struct_variant().fields.len()
}
ref ty => {
Expand Down
38 changes: 18 additions & 20 deletions src/librustc/middle/stability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use lint;
use middle::cstore::LOCAL_CRATE;
use hir::def::Def;
use hir::def_id::{CRATE_DEF_INDEX, DefId, DefIndex};
use ty::{self, TyCtxt};
use ty::{self, TyCtxt, AdtKind};
use middle::privacy::AccessLevels;
use syntax::parse::token::InternedString;
use syntax_pos::{Span, DUMMY_SP};
Expand Down Expand Up @@ -561,49 +561,48 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
hir::ExprField(ref base_e, ref field) => {
span = field.span;
match tcx.expr_ty_adjusted(base_e).sty {
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
ty::TyAdt(def, _) => {
def.struct_variant().field_named(field.node).did
}
_ => span_bug!(e.span,
"stability::check_expr: named field access on non-struct/union")
"stability::check_expr: named field access on non-ADT")
}
}
hir::ExprTupField(ref base_e, ref field) => {
span = field.span;
match tcx.expr_ty_adjusted(base_e).sty {
ty::TyStruct(def, _) => def.struct_variant().fields[field.node].did,
ty::TyAdt(def, _) => {
def.struct_variant().fields[field.node].did
}
ty::TyTuple(..) => return,
_ => span_bug!(e.span,
"stability::check_expr: unnamed field access on \
something other than a tuple or struct")
}
}
hir::ExprStruct(_, ref expr_fields, _) => {
let type_ = tcx.expr_ty(e);
match type_.sty {
ty::TyStruct(def, _) | ty::TyUnion(def, _) => {
match tcx.expr_ty(e).sty {
ty::TyAdt(adt, ..) => match adt.adt_kind() {
AdtKind::Struct | AdtKind::Union => {
// check the stability of each field that appears
// in the construction expression.
for field in expr_fields {
let did = def.struct_variant()
.field_named(field.name.node)
.did;
let did = adt.struct_variant().field_named(field.name.node).did;
maybe_do_stability_check(tcx, did, field.span, cb);
}

// we're done.
return
}
AdtKind::Enum => {
// we don't look at stability attributes on
// struct-like enums (yet...), but it's definitely not
// a bug to have construct one.
ty::TyEnum(..) => return,
_ => {
span_bug!(e.span,
"stability::check_expr: struct construction \
of non-struct/union, type {:?}",
type_);
return
}
},
ref ty => span_bug!(e.span, "stability::check_expr: struct \
construction of non-ADT type: {:?}", ty)
}
}
_ => return
Expand Down Expand Up @@ -648,10 +647,9 @@ pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat,
debug!("check_pat(pat = {:?})", pat);
if is_internal(tcx, pat.span) { return; }

let v = match tcx.pat_ty_opt(pat) {
Some(&ty::TyS { sty: ty::TyStruct(def, _), .. }) |
Some(&ty::TyS { sty: ty::TyUnion(def, _), .. }) => def.struct_variant(),
Some(_) | None => return,
let v = match tcx.pat_ty_opt(pat).map(|ty| &ty.sty) {
Some(&ty::TyAdt(adt, _)) if !adt.is_enum() => adt.struct_variant(),
_ => return,
};
match pat.node {
// Foo(a, b, c)
Expand Down
7 changes: 4 additions & 3 deletions src/librustc/mir/tcx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
LvalueTy::Ty { ty } =>
ty,
LvalueTy::Downcast { adt_def, substs, variant_index: _ } =>
tcx.mk_enum(adt_def, substs),
tcx.mk_adt(adt_def, substs),
}
}

Expand Down Expand Up @@ -75,15 +75,16 @@ impl<'a, 'gcx, 'tcx> LvalueTy<'tcx> {
}
ProjectionElem::Downcast(adt_def1, index) =>
match self.to_ty(tcx).sty {
ty::TyEnum(adt_def, substs) => {
ty::TyAdt(adt_def, substs) => {
assert!(adt_def.is_enum());
assert!(index < adt_def.variants.len());
assert_eq!(adt_def, adt_def1);
LvalueTy::Downcast { adt_def: adt_def,
substs: substs,
variant_index: index }
}
_ => {
bug!("cannot downcast non-enum type: `{:?}`", self)
bug!("cannot downcast non-ADT type: `{:?}`", self)
}
},
ProjectionElem::Field(_, fty) => LvalueTy::Ty { ty: fty }
Expand Down
6 changes: 2 additions & 4 deletions src/librustc/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ fn fundamental_ty(tcx: TyCtxt, ty: Ty) -> bool {
match ty.sty {
ty::TyBox(..) | ty::TyRef(..) =>
true,
ty::TyEnum(def, _) | ty::TyStruct(def, _) | ty::TyUnion(def, _) =>
ty::TyAdt(def, _) =>
def.is_fundamental(),
ty::TyTrait(ref data) =>
tcx.has_attr(data.principal.def_id(), "fundamental"),
Expand Down Expand Up @@ -260,9 +260,7 @@ fn ty_is_local_constructor(tcx: TyCtxt, ty: Ty, infer_is_local: InferIsLocal)->
infer_is_local.0
}

ty::TyEnum(def, _) |
ty::TyStruct(def, _) |
ty::TyUnion(def, _) => {
ty::TyAdt(def, _) => {
def.did.is_local()
}

Expand Down
38 changes: 18 additions & 20 deletions src/librustc/traits/error_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use super::{
use fmt_macros::{Parser, Piece, Position};
use hir::def_id::DefId;
use infer::{self, InferCtxt, TypeOrigin};
use ty::{self, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable};
use ty::error::ExpectedFound;
use ty::fast_reject;
use ty::fold::TypeFolder;
Expand Down Expand Up @@ -151,32 +151,30 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
ty::TyBool => Some(0),
ty::TyChar => Some(1),
ty::TyStr => Some(2),
ty::TyInt(..) | ty::TyUint(..) |
ty::TyInfer(ty::IntVar(..)) => Some(3),
ty::TyInt(..) | ty::TyUint(..) | ty::TyInfer(ty::IntVar(..)) => Some(3),
ty::TyFloat(..) | ty::TyInfer(ty::FloatVar(..)) => Some(4),
ty::TyEnum(..) => Some(5),
ty::TyStruct(..) => Some(6),
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(7),
ty::TyArray(..) | ty::TySlice(..) => Some(8),
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(9),
ty::TyTrait(..) => Some(10),
ty::TyClosure(..) => Some(11),
ty::TyTuple(..) => Some(12),
ty::TyProjection(..) => Some(13),
ty::TyParam(..) => Some(14),
ty::TyAnon(..) => Some(15),
ty::TyNever => Some(16),
ty::TyUnion(..) => Some(17),
ty::TyBox(..) | ty::TyRef(..) | ty::TyRawPtr(..) => Some(5),
ty::TyArray(..) | ty::TySlice(..) => Some(6),
ty::TyFnDef(..) | ty::TyFnPtr(..) => Some(7),
ty::TyTrait(..) => Some(8),
ty::TyClosure(..) => Some(9),
ty::TyTuple(..) => Some(10),
ty::TyProjection(..) => Some(11),
ty::TyParam(..) => Some(12),
ty::TyAnon(..) => Some(13),
ty::TyNever => Some(14),
ty::TyAdt(adt, ..) => match adt.adt_kind() {
AdtKind::Struct => Some(15),
AdtKind::Union => Some(16),
AdtKind::Enum => Some(17),
},
ty::TyInfer(..) | ty::TyError => None
}
}

match (type_category(a), type_category(b)) {
(Some(cat_a), Some(cat_b)) => match (&a.sty, &b.sty) {
(&ty::TyStruct(def_a, _), &ty::TyStruct(def_b, _)) |
(&ty::TyUnion(def_a, _), &ty::TyUnion(def_b, _)) |
(&ty::TyEnum(def_a, _), &ty::TyEnum(def_b, _)) =>
def_a == def_b,
(&ty::TyAdt(def_a, _), &ty::TyAdt(def_b, _)) => def_a == def_b,
_ => cat_a == cat_b
},
// infer and error can be equated to all types
Expand Down
Loading