Skip to content

Commit 501eea0

Browse files
committed
auto merge of #8744 : jld/rust/discr64, r=nikomatsakis
This is in preparation for making discriminants not always be int (#1647), but it also makes compiles for a 64-bit target not behave differently — with respect to how many bits of discriminants are preserved — depending on the build host's word size, which is a nice property to have. We may want to standardize how to abbreviate "discriminant" in a followup change.
2 parents bdd188d + 0db2b19 commit 501eea0

File tree

11 files changed

+79
-47
lines changed

11 files changed

+79
-47
lines changed

src/librustc/metadata/decoder.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use middle::astencode::vtable_decoder_helpers;
2626

2727

2828
use std::hash::HashUtil;
29-
use std::uint;
29+
use std::u64;
3030
use std::io::WriterUtil;
3131
use std::io;
3232
use std::option;
@@ -207,9 +207,9 @@ fn each_reexport(d: ebml::Doc, f: &fn(ebml::Doc) -> bool) -> bool {
207207
reader::tagged_docs(d, tag_items_data_item_reexport, f)
208208
}
209209

210-
fn variant_disr_val(d: ebml::Doc) -> Option<uint> {
210+
fn variant_disr_val(d: ebml::Doc) -> Option<ty::Disr> {
211211
do reader::maybe_get_doc(d, tag_disr_val).chain |val_doc| {
212-
do reader::with_doc_data(val_doc) |data| { uint::parse_bytes(data, 10u) }
212+
do reader::with_doc_data(val_doc) |data| { u64::parse_bytes(data, 10u) }
213213
}
214214
}
215215

src/librustc/metadata/encoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn encode_symbol(ecx: &EncodeContext,
292292

293293
fn encode_disr_val(_: &EncodeContext,
294294
ebml_w: &mut writer::Encoder,
295-
disr_val: uint) {
295+
disr_val: ty::Disr) {
296296
ebml_w.start_tag(tag_disr_val);
297297
let s = disr_val.to_str();
298298
ebml_w.writer.write(s.as_bytes());

src/librustc/middle/trans/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub enum VecLenOpt {
245245
// range)
246246
enum Opt {
247247
lit(Lit),
248-
var(/* disr val */ uint, @adt::Repr),
248+
var(ty::Disr, @adt::Repr),
249249
range(@ast::expr, @ast::expr),
250250
vec_len(/* length */ uint, VecLenOpt, /*range of matches*/(uint, uint))
251251
}
@@ -992,7 +992,7 @@ struct ExtractedBlock {
992992

993993
fn extract_variant_args(bcx: @mut Block,
994994
repr: &adt::Repr,
995-
disr_val: uint,
995+
disr_val: ty::Disr,
996996
val: ValueRef)
997997
-> ExtractedBlock {
998998
let _icx = push_ctxt("match::extract_variant_args");

src/librustc/middle/trans/adt.rs

+29-24
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ use middle::trans::common::*;
5555
use middle::trans::machine;
5656
use middle::trans::type_of;
5757
use middle::ty;
58+
use middle::ty::Disr;
5859
use syntax::ast;
5960
use util::ppaux::ty_to_str;
6061

@@ -64,7 +65,7 @@ use middle::trans::type_::Type;
6465
/// Representations.
6566
pub enum Repr {
6667
/// C-like enums; basically an int.
67-
CEnum(uint, uint), // discriminant range
68+
CEnum(Disr, Disr), // discriminant range
6869
/**
6970
* Single-case variants, and structs/tuples/records.
7071
*
@@ -89,7 +90,7 @@ pub enum Repr {
8990
* is represented such that `None` is a null pointer and `Some` is the
9091
* identity function.
9192
*/
92-
NullablePointer{ nonnull: Struct, nndiscr: uint, ptrfield: uint,
93+
NullablePointer{ nonnull: Struct, nndiscr: Disr, ptrfield: uint,
9394
nullfields: ~[ty::t] }
9495
}
9596

@@ -140,7 +141,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
140141
return Univariant(mk_struct(cx, ftys, packed), dtor)
141142
}
142143
ty::ty_enum(def_id, ref substs) => {
143-
struct Case { discr: uint, tys: ~[ty::t] };
144+
struct Case { discr: Disr, tys: ~[ty::t] };
144145
impl Case {
145146
fn is_zerolen(&self, cx: &mut CrateContext) -> bool {
146147
mk_struct(cx, self.tys, false).size == 0
@@ -177,7 +178,7 @@ fn represent_type_uncached(cx: &mut CrateContext, t: ty::t) -> Repr {
177178
// Since there's at least one
178179
// non-empty body, explicit discriminants should have
179180
// been rejected by a checker before this point.
180-
if !cases.iter().enumerate().all(|(i,c)| c.discr == i) {
181+
if !cases.iter().enumerate().all(|(i,c)| c.discr == (i as Disr)) {
181182
cx.sess.bug(fmt!("non-C-like enum %s with specified \
182183
discriminants",
183184
ty::item_path_str(cx.tcx, def_id)))
@@ -305,16 +306,16 @@ pub fn trans_get_discr(bcx: @mut Block, r: &Repr, scrutinee: ValueRef)
305306
-> ValueRef {
306307
match *r {
307308
CEnum(min, max) => load_discr(bcx, scrutinee, min, max),
308-
Univariant(*) => C_uint(bcx.ccx(), 0),
309-
General(ref cases) => load_discr(bcx, scrutinee, 0, cases.len() - 1),
309+
Univariant(*) => C_disr(bcx.ccx(), 0),
310+
General(ref cases) => load_discr(bcx, scrutinee, 0, (cases.len() - 1) as Disr),
310311
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
311312
ZExt(bcx, nullable_bitdiscr(bcx, nonnull, nndiscr, ptrfield, scrutinee),
312313
Type::enum_discrim(bcx.ccx()))
313314
}
314315
}
315316
}
316317

317-
fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: uint, ptrfield: uint,
318+
fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: Disr, ptrfield: uint,
318319
scrutinee: ValueRef) -> ValueRef {
319320
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
320321
let llptr = Load(bcx, GEPi(bcx, scrutinee, [0, ptrfield]));
@@ -323,7 +324,7 @@ fn nullable_bitdiscr(bcx: @mut Block, nonnull: &Struct, nndiscr: uint, ptrfield:
323324
}
324325

325326
/// Helper for cases where the discriminant is simply loaded.
326-
fn load_discr(bcx: @mut Block, scrutinee: ValueRef, min: uint, max: uint)
327+
fn load_discr(bcx: @mut Block, scrutinee: ValueRef, min: Disr, max: Disr)
327328
-> ValueRef {
328329
let ptr = GEPi(bcx, scrutinee, [0, 0]);
329330
if max + 1 == min {
@@ -347,16 +348,16 @@ fn load_discr(bcx: @mut Block, scrutinee: ValueRef, min: uint, max: uint)
347348
*
348349
* This should ideally be less tightly tied to `_match`.
349350
*/
350-
pub fn trans_case(bcx: @mut Block, r: &Repr, discr: uint) -> _match::opt_result {
351+
pub fn trans_case(bcx: @mut Block, r: &Repr, discr: Disr) -> _match::opt_result {
351352
match *r {
352353
CEnum(*) => {
353-
_match::single_result(rslt(bcx, C_uint(bcx.ccx(), discr)))
354+
_match::single_result(rslt(bcx, C_disr(bcx.ccx(), discr)))
354355
}
355356
Univariant(*) => {
356357
bcx.ccx().sess.bug("no cases for univariants or structs")
357358
}
358359
General(*) => {
359-
_match::single_result(rslt(bcx, C_uint(bcx.ccx(), discr)))
360+
_match::single_result(rslt(bcx, C_disr(bcx.ccx(), discr)))
360361
}
361362
NullablePointer{ _ } => {
362363
assert!(discr == 0 || discr == 1);
@@ -370,11 +371,11 @@ pub fn trans_case(bcx: @mut Block, r: &Repr, discr: uint) -> _match::opt_result
370371
* representation. The fields, if any, should then be initialized via
371372
* `trans_field_ptr`.
372373
*/
373-
pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
374+
pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: Disr) {
374375
match *r {
375376
CEnum(min, max) => {
376377
assert!(min <= discr && discr <= max);
377-
Store(bcx, C_uint(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
378+
Store(bcx, C_disr(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
378379
}
379380
Univariant(ref st, true) => {
380381
assert_eq!(discr, 0);
@@ -385,7 +386,7 @@ pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
385386
assert_eq!(discr, 0);
386387
}
387388
General(*) => {
388-
Store(bcx, C_uint(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
389+
Store(bcx, C_disr(bcx.ccx(), discr), GEPi(bcx, val, [0, 0]))
389390
}
390391
NullablePointer{ nonnull: ref nonnull, nndiscr, ptrfield, _ } => {
391392
if discr != nndiscr {
@@ -401,7 +402,7 @@ pub fn trans_start_init(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint) {
401402
* The number of fields in a given case; for use when obtaining this
402403
* information from the type or definition is less convenient.
403404
*/
404-
pub fn num_args(r: &Repr, discr: uint) -> uint {
405+
pub fn num_args(r: &Repr, discr: Disr) -> uint {
405406
match *r {
406407
CEnum(*) => 0,
407408
Univariant(ref st, dtor) => {
@@ -416,7 +417,7 @@ pub fn num_args(r: &Repr, discr: uint) -> uint {
416417
}
417418

418419
/// Access a field, at a point when the value's case is known.
419-
pub fn trans_field_ptr(bcx: @mut Block, r: &Repr, val: ValueRef, discr: uint,
420+
pub fn trans_field_ptr(bcx: @mut Block, r: &Repr, val: ValueRef, discr: Disr,
420421
ix: uint) -> ValueRef {
421422
// Note: if this ever needs to generate conditionals (e.g., if we
422423
// decide to do some kind of cdr-coding-like non-unique repr
@@ -494,13 +495,13 @@ pub fn trans_drop_flag_ptr(bcx: @mut Block, r: &Repr, val: ValueRef) -> ValueRef
494495
* this could be changed in the future to avoid allocating unnecessary
495496
* space after values of shorter-than-maximum cases.
496497
*/
497-
pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: uint,
498+
pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: Disr,
498499
vals: &[ValueRef]) -> ValueRef {
499500
match *r {
500501
CEnum(min, max) => {
501502
assert_eq!(vals.len(), 0);
502503
assert!(min <= discr && discr <= max);
503-
C_uint(ccx, discr)
504+
C_disr(ccx, discr)
504505
}
505506
Univariant(ref st, _dro) => {
506507
assert_eq!(discr, 0);
@@ -509,7 +510,7 @@ pub fn trans_const(ccx: &mut CrateContext, r: &Repr, discr: uint,
509510
General(ref cases) => {
510511
let case = &cases[discr];
511512
let max_sz = cases.iter().map(|x| x.size).max().unwrap();
512-
let discr_ty = C_uint(ccx, discr);
513+
let discr_ty = C_disr(ccx, discr);
513514
let contents = build_const_struct(ccx, case,
514515
~[discr_ty] + vals);
515516
C_struct(contents + &[padding(max_sz - case.size)])
@@ -581,15 +582,15 @@ fn roundup(x: u64, a: u64) -> u64 { ((x + (a - 1)) / a) * a }
581582

582583
/// Get the discriminant of a constant value. (Not currently used.)
583584
pub fn const_get_discrim(ccx: &mut CrateContext, r: &Repr, val: ValueRef)
584-
-> uint {
585+
-> Disr {
585586
match *r {
586-
CEnum(*) => const_to_uint(val) as uint,
587+
CEnum(*) => const_to_uint(val) as Disr,
587588
Univariant(*) => 0,
588-
General(*) => const_to_uint(const_get_elt(ccx, val, [0])) as uint,
589+
General(*) => const_to_uint(const_get_elt(ccx, val, [0])) as Disr,
589590
NullablePointer{ nndiscr, ptrfield, _ } => {
590591
if is_null(const_struct_field(ccx, val, ptrfield)) {
591592
/* subtraction as uint is ok because nndiscr is either 0 or 1 */
592-
(1 - nndiscr) as uint
593+
(1 - nndiscr) as Disr
593594
} else {
594595
nndiscr
595596
}
@@ -605,7 +606,7 @@ pub fn const_get_discrim(ccx: &mut CrateContext, r: &Repr, val: ValueRef)
605606
* raw LLVM-level structs and arrays.)
606607
*/
607608
pub fn const_get_field(ccx: &mut CrateContext, r: &Repr, val: ValueRef,
608-
_discr: uint, ix: uint) -> ValueRef {
609+
_discr: Disr, ix: uint) -> ValueRef {
609610
match *r {
610611
CEnum(*) => ccx.sess.bug("element access in C-like enum const"),
611612
Univariant(*) => const_struct_field(ccx, val, ix),
@@ -644,3 +645,7 @@ pub fn is_newtypeish(r: &Repr) -> bool {
644645
_ => false
645646
}
646647
}
648+
649+
fn C_disr(cx: &CrateContext, i: Disr) -> ValueRef {
650+
return C_integral(cx.int_type, i, false);
651+
}

src/librustc/middle/trans/base.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2014,7 +2014,7 @@ pub fn trans_enum_variant(ccx: @mut CrateContext,
20142014
_enum_id: ast::NodeId,
20152015
variant: &ast::variant,
20162016
args: &[ast::variant_arg],
2017-
disr: uint,
2017+
disr: ty::Disr,
20182018
param_substs: Option<@param_substs>,
20192019
llfndecl: ValueRef) {
20202020
let _icx = push_ctxt("trans_enum_variant");
@@ -2063,7 +2063,7 @@ pub fn trans_enum_variant_or_tuple_like_struct<A:IdAndTy>(
20632063
ccx: @mut CrateContext,
20642064
ctor_id: ast::NodeId,
20652065
args: &[A],
2066-
disr: uint,
2066+
disr: ty::Disr,
20672067
param_substs: Option<@param_substs>,
20682068
llfndecl: ValueRef)
20692069
{

src/librustc/middle/trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ fn const_expr_unadjusted(cx: @mut CrateContext, e: &ast::expr) -> ValueRef {
453453
(expr::cast_enum, expr::cast_float) => {
454454
let repr = adt::represent_type(cx, basety);
455455
let discr = adt::const_get_discrim(cx, repr, v);
456-
let iv = C_uint(cx, discr);
456+
let iv = C_integral(cx.int_type, discr, false);
457457
let ety_cast = expr::cast_type_kind(ety);
458458
match ety_cast {
459459
expr::cast_integral => {

src/librustc/middle/trans/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ pub fn trans_local_var(bcx: @mut Block, def: ast::def) -> Datum {
11181118
pub fn with_field_tys<R>(tcx: ty::ctxt,
11191119
ty: ty::t,
11201120
node_id_opt: Option<ast::NodeId>,
1121-
op: &fn(uint, (&[ty::field])) -> R) -> R {
1121+
op: &fn(ty::Disr, (&[ty::field])) -> R) -> R {
11221122
match ty::get(ty).sty {
11231123
ty::ty_struct(did, ref substs) => {
11241124
op(0, struct_fields(tcx, did, substs))
@@ -1235,7 +1235,7 @@ struct StructBaseInfo {
12351235
* - `optbase` contains information on the base struct (if any) from
12361236
* which remaining fields are copied; see comments on `StructBaseInfo`.
12371237
*/
1238-
fn trans_adt(bcx: @mut Block, repr: &adt::Repr, discr: uint,
1238+
fn trans_adt(bcx: @mut Block, repr: &adt::Repr, discr: ty::Disr,
12391239
fields: &[(uint, @ast::expr)],
12401240
optbase: Option<StructBaseInfo>,
12411241
dest: Dest) -> @mut Block {

src/librustc/middle/trans/reflect.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ impl Reflector {
317317
for (i, v) in variants.iter().enumerate() {
318318
let name = ccx.sess.str_of(v.name);
319319
let variant_args = ~[this.c_uint(i),
320-
this.c_uint(v.disr_val),
320+
C_integral(self.bcx.ccx().int_type, v.disr_val, false),
321321
this.c_uint(v.args.len()),
322322
this.c_slice(name)];
323323
do this.bracketed("enum_variant", variant_args) |this| {

src/librustc/middle/ty.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ use syntax::abi::AbiSet;
4848
use syntax;
4949
use extra::enum_set::{EnumSet, CLike};
5050

51-
pub static INITIAL_DISCRIMINANT_VALUE: uint = 0;
51+
pub type Disr = u64;
52+
53+
pub static INITIAL_DISCRIMINANT_VALUE: Disr = 0;
5254

5355
// Data types
5456

@@ -3803,7 +3805,7 @@ pub struct VariantInfo {
38033805
ctor_ty: t,
38043806
name: ast::ident,
38053807
id: ast::def_id,
3806-
disr_val: uint,
3808+
disr_val: Disr,
38073809
vis: visibility
38083810
}
38093811

@@ -3814,7 +3816,7 @@ impl VariantInfo {
38143816
/// Does not do any caching of the value in the type context.
38153817
pub fn from_ast_variant(cx: ctxt,
38163818
ast_variant: &ast::variant,
3817-
discriminant: uint) -> VariantInfo {
3819+
discriminant: Disr) -> VariantInfo {
38183820

38193821
let ctor_ty = node_id_to_type(cx, ast_variant.node.id);
38203822

@@ -4008,7 +4010,7 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[@VariantInfo] {
40084010
node: ast::item_enum(ref enum_definition, _),
40094011
_
40104012
}, _) => {
4011-
let mut last_discriminant: Option<uint> = None;
4013+
let mut last_discriminant: Option<Disr> = None;
40124014
@enum_definition.variants.iter().map(|variant| {
40134015

40144016
let mut discriminant = match last_discriminant {
@@ -4018,8 +4020,8 @@ pub fn enum_variants(cx: ctxt, id: ast::def_id) -> @~[@VariantInfo] {
40184020

40194021
match variant.node.disr_expr {
40204022
Some(e) => match const_eval::eval_const_expr_partial(&cx, e) {
4021-
Ok(const_eval::const_int(val)) => discriminant = val as uint,
4022-
Ok(const_eval::const_uint(val)) => discriminant = val as uint,
4023+
Ok(const_eval::const_int(val)) => discriminant = val as Disr,
4024+
Ok(const_eval::const_uint(val)) => discriminant = val as Disr,
40234025
Ok(_) => {
40244026
cx.sess.span_err(e.span, "expected signed integer constant");
40254027
}

src/librustc/middle/typeck/check/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ use middle::pat_util;
8383
use middle::lint::unreachable_code;
8484
use middle::ty::{FnSig, VariantInfo};
8585
use middle::ty::{ty_param_bounds_and_ty, ty_param_substs_and_ty};
86-
use middle::ty::{substs, param_ty, ExprTyProvider};
86+
use middle::ty::{substs, param_ty, Disr, ExprTyProvider};
8787
use middle::ty;
8888
use middle::typeck::astconv::AstConv;
8989
use middle::typeck::astconv::{ast_region_to_region, ast_ty_to_ty};
@@ -3000,8 +3000,8 @@ pub fn check_enum_variants(ccx: @mut CrateCtxt,
30003000

30013001
let rty = ty::node_id_to_type(ccx.tcx, id);
30023002
let mut variants: ~[@ty::VariantInfo] = ~[];
3003-
let mut disr_vals: ~[uint] = ~[];
3004-
let mut prev_disr_val: Option<uint> = None;
3003+
let mut disr_vals: ~[ty::Disr] = ~[];
3004+
let mut prev_disr_val: Option<ty::Disr> = None;
30053005

30063006
for v in vs.iter() {
30073007

@@ -3024,8 +3024,8 @@ pub fn check_enum_variants(ccx: @mut CrateCtxt,
30243024
// handle, so we may still get an internal compiler error
30253025

30263026
match const_eval::eval_const_expr_partial(&ccx.tcx, e) {
3027-
Ok(const_eval::const_int(val)) => current_disr_val = val as uint,
3028-
Ok(const_eval::const_uint(val)) => current_disr_val = val as uint,
3027+
Ok(const_eval::const_int(val)) => current_disr_val = val as Disr,
3028+
Ok(const_eval::const_uint(val)) => current_disr_val = val as Disr,
30293029
Ok(_) => {
30303030
ccx.tcx.sess.span_err(e.span, "expected signed integer constant");
30313031
}

0 commit comments

Comments
 (0)