Skip to content

Commit 1fee722

Browse files
committed
Revert use of layout code in typeck::collect
1 parent 5e465d5 commit 1fee722

File tree

5 files changed

+27
-47
lines changed

5 files changed

+27
-47
lines changed

src/librustc/ty/layout.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ impl Integer {
453453
/// signed discriminant range and #[repr] attribute.
454454
/// N.B.: u64 values above i64::MAX will be treated as signed, but
455455
/// that shouldn't affect anything, other than maybe debuginfo.
456-
pub fn repr_discr(tcx: TyCtxt, hints: &[attr::ReprAttr], min: i128, max: i128)
456+
pub fn repr_discr(tcx: TyCtxt, ty: Ty, hints: &[attr::ReprAttr], min: i128, max: i128)
457457
-> (Integer, bool) {
458458
// Theoretically, negative values could be larger in unsigned representation
459459
// than the unsigned representation of the signed minimum. However, if there
@@ -488,10 +488,10 @@ impl Integer {
488488
}
489489
attr::ReprAny => {},
490490
attr::ReprPacked => {
491-
bug!("Integer::repr_discr: found #[repr(packed)] on enum");
491+
bug!("Integer::repr_discr: found #[repr(packed)] on enum {}", ty);
492492
}
493493
attr::ReprSimd => {
494-
bug!("Integer::repr_discr: found #[repr(simd)] on enum");
494+
bug!("Integer::repr_discr: found #[repr(simd)] on enum {}", ty);
495495
}
496496
}
497497
}
@@ -1236,8 +1236,7 @@ impl<'a, 'gcx, 'tcx> Layout {
12361236

12371237
// FIXME: should handle i128? signed-value based impl is weird and hard to
12381238
// grok.
1239-
let discr = Integer::from_attr(&tcx.data_layout, def.discr_ty);
1240-
let signed = def.discr_ty.is_signed();
1239+
let (discr, signed) = Integer::repr_discr(tcx, ty, hints, min, max);
12411240
return success(CEnum {
12421241
discr: discr,
12431242
signed: signed,
@@ -1352,7 +1351,9 @@ impl<'a, 'gcx, 'tcx> Layout {
13521351
}
13531352

13541353
// The general case.
1355-
let min_ity = Integer::from_attr(&tcx.data_layout, def.discr_ty);
1354+
let discr_max = (variants.len() - 1) as i64;
1355+
assert!(discr_max >= 0);
1356+
let (min_ity, _) = Integer::repr_discr(tcx, ty, &hints[..], 0, discr_max);
13561357
let mut align = dl.aggregate_align;
13571358
let mut size = Size::from_bytes(0);
13581359

src/librustc/ty/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1326,7 +1326,12 @@ pub struct FieldDef {
13261326
/// table.
13271327
pub struct AdtDef {
13281328
pub did: DefId,
1329-
pub discr_ty: attr::IntType, // Type of the discriminant
1329+
/// Type of the discriminant
1330+
///
1331+
/// Note, that this is the type specified in `repr()` or a default type of some sort, and might
1332+
/// not match the actual type that layout algorithm decides to use when translating this type
1333+
/// into LLVM. That being said, layout algorithm may not use a type larger than specified here.
1334+
pub discr_ty: attr::IntType,
13301335
pub variants: Vec<VariantDef>,
13311336
destructor: Cell<Option<DefId>>,
13321337
flags: Cell<AdtFlags>

src/librustc/ty/util.rs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use ty::TypeVariants::*;
2323
use util::nodemap::FxHashMap;
2424
use middle::lang_items;
2525

26-
use rustc_const_math::ConstInt;
2726
use rustc_data_structures::stable_hasher::{StableHasher, StableHasherResult};
2827

2928
use std::cell::RefCell;
@@ -34,14 +33,10 @@ use syntax::ast::{self, Name};
3433
use syntax::attr::{self, SignedInt, UnsignedInt};
3534
use syntax_pos::Span;
3635

37-
use rustc_i128::i128;
38-
3936
use hir;
4037

4138
pub trait IntTypeExt {
4239
fn to_ty<'a, 'gcx: 'a+'tcx, 'tcx: 'a>(self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx>;
43-
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
44-
-> Option<Disr>;
4540
fn initial_discriminant<'a, 'tcx>(&self, _: TyCtxt<'a, 'tcx, 'tcx>) -> Disr;
4641
}
4742

@@ -56,19 +51,6 @@ impl IntTypeExt for attr::IntType {
5651
fn initial_discriminant<'a, 'tcx>(&self, _: TyCtxt<'a, 'tcx, 'tcx>) -> Disr {
5752
0
5853
}
59-
60-
/// None = overflow
61-
fn disr_incr<'a, 'tcx>(&self, tcx: TyCtxt<'a, 'tcx, 'tcx>, val: Option<Disr>)
62-
-> Option<Disr> {
63-
if let Some(val) = val {
64-
match *self {
65-
SignedInt(it) => ConstInt::new_signed(val as i128, it, tcx.sess.target.int_type),
66-
UnsignedInt(it) => ConstInt::new_unsigned(val, it, tcx.sess.target.uint_type),
67-
}.and_then(|l| (l + ConstInt::Infer(1)).ok()).map(|v| v.to_u128_unchecked())
68-
} else {
69-
Some(self.initial_discriminant(tcx))
70-
}
71-
}
7254
}
7355

7456

src/librustc_trans/mir/rvalue.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,8 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
434434
let enum_ty = discr_lvalue.ty.to_ty(bcx.tcx());
435435
let discr_ty = rvalue.ty(&*self.mir, bcx.tcx()).unwrap();
436436
let discr_type = type_of::immediate_type_of(bcx.ccx, discr_ty);
437-
let discr = adt::trans_get_discr(&bcx, enum_ty, discr_lvalue.llval, None, true);
438-
let discr = if common::val_ty(discr) == Type::i1(bcx.ccx) {
439-
bcx.zext(discr, discr_type)
440-
} else {
441-
bcx.trunc(discr, discr_type)
442-
};
437+
let discr = adt::trans_get_discr(&bcx, enum_ty, discr_lvalue.llval,
438+
Some(discr_type), true);
443439
(bcx, OperandRef {
444440
val: OperandValue::Immediate(discr),
445441
ty: discr_ty

src/librustc_typeck/collect.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use rustc_const_eval::EvalHint::UncheckedExprHint;
6666
use rustc_const_eval::{ConstContext, report_const_eval_err};
6767
use rustc::ty::subst::Substs;
6868
use rustc::ty::{ToPredicate, ImplContainer, AssociatedItemContainer, TraitContainer};
69-
use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt, layout};
69+
use rustc::ty::{self, AdtKind, ToPolyTraitRef, Ty, TyCtxt};
7070
use rustc::ty::util::IntTypeExt;
7171
use rustc::dep_graph::DepNode;
7272
use util::common::{ErrorReported, MemoizationMap};
@@ -86,8 +86,6 @@ use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
8686
use rustc::hir::def::{Def, CtorKind};
8787
use rustc::hir::def_id::DefId;
8888

89-
use rustc_i128::i128;
90-
9189
///////////////////////////////////////////////////////////////////////////
9290
// Main entry point
9391

@@ -1030,7 +1028,7 @@ fn convert_union_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10301028
}
10311029

10321030
fn evaluate_disr_expr(ccx: &CrateCtxt, repr_ty: attr::IntType, body: hir::BodyId)
1033-
-> Option<ty::Disr> {
1031+
-> Option<ConstInt> {
10341032
let e = &ccx.tcx.hir.body(body).value;
10351033
debug!("disr expr, checking {}", ccx.tcx.hir.node_to_pretty_string(e.id));
10361034

@@ -1060,7 +1058,7 @@ fn evaluate_disr_expr(ccx: &CrateCtxt, repr_ty: attr::IntType, body: hir::BodyId
10601058
(attr::UnsignedInt(ast::UintTy::U64), ConstInt::U64(_)) |
10611059
(attr::UnsignedInt(ast::UintTy::U128), ConstInt::U128(_)) |
10621060
(attr::UnsignedInt(ast::UintTy::Us), ConstInt::Usize(_)) =>
1063-
Some(i.to_u128_unchecked()),
1061+
Some(i),
10641062
(_, i) => {
10651063
print_err(ConstVal::Integral(i));
10661064
None
@@ -1091,15 +1089,17 @@ fn convert_enum_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
10911089
let did = tcx.hir.local_def_id(it.id);
10921090
let repr_hints = tcx.lookup_repr_hints(did);
10931091
let repr_type = tcx.enum_repr_type(repr_hints.get(0));
1094-
let initial = repr_type.initial_discriminant(tcx);
1095-
let mut prev_disr = None::<ty::Disr>;
1096-
let (mut min, mut max) = (i128::max_value(), i128::min_value());
1092+
let initial = ConstInt::new_inttype(repr_type.initial_discriminant(tcx), repr_type,
1093+
tcx.sess.target.uint_type, tcx.sess.target.int_type)
1094+
.unwrap();
1095+
let mut prev_disr = None::<ConstInt>;
10971096
let variants = def.variants.iter().map(|v| {
1098-
let wrapped_disr = prev_disr.map_or(initial, |d| d.wrapping_add(1));
1097+
let wrapped_disr = prev_disr.map_or(initial, |d| d.wrap_incr());
10991098
let disr = if let Some(e) = v.node.disr_expr {
11001099
// FIXME: i128 discriminants
11011100
evaluate_disr_expr(ccx, repr_type, e)
1102-
} else if let Some(disr) = repr_type.disr_incr(tcx, prev_disr) {
1101+
} else if let Some(disr) = prev_disr.map_or(Some(initial),
1102+
|v| (v + ConstInt::Infer(1)).ok()) {
11031103
Some(disr)
11041104
} else {
11051105
struct_span_err!(tcx.sess, v.span, E0370,
@@ -1111,14 +1111,10 @@ fn convert_enum_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
11111111
None
11121112
}.unwrap_or(wrapped_disr);
11131113
prev_disr = Some(disr);
1114-
if (disr as i128) < min { min = disr as i128; }
1115-
if (disr as i128) > max { max = disr as i128; }
11161114
let did = tcx.hir.local_def_id(v.node.data.id());
1117-
convert_struct_variant(ccx, did, v.node.name, disr, &v.node.data)
1115+
convert_struct_variant(ccx, did, v.node.name, disr.to_u128_unchecked(), &v.node.data)
11181116
}).collect();
1119-
1120-
let (repr_int, signed) = layout::Integer::repr_discr(tcx, &repr_hints[..], min, max);
1121-
let adt = tcx.alloc_adt_def(did, AdtKind::Enum, Some(repr_int.to_attr(signed)), variants);
1117+
let adt = tcx.alloc_adt_def(did, AdtKind::Enum, Some(repr_type), variants);
11221118
tcx.adt_defs.borrow_mut().insert(did, adt);
11231119
adt
11241120
}

0 commit comments

Comments
 (0)