summary refs log tree commit diff
path: root/src/libstd
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2013-10-30 00:31:23 -0700
committerbors <bors@rust-lang.org>2013-10-30 00:31:23 -0700
commit5e1a69112584341522e2cc00bfc6dd1db6e0ec3a (patch)
treebc856e4b2bd4ac269cbfe593c190cd77beaa1dbf /src/libstd
parente42e378f32e212997fc42281112b1c9c4c247de0 (diff)
parent86a710e4545a383513c1247e48421142f3741984 (diff)
downloadrust-5e1a69112584341522e2cc00bfc6dd1db6e0ec3a.tar.gz
rust-5e1a69112584341522e2cc00bfc6dd1db6e0ec3a.zip
auto merge of #9613 : jld/rust/enum-discrim-size.r0, r=alexcrichton
Allows an enum with a discriminant to use any of the primitive integer types to store it.  By default the smallest usable type is chosen, but this can be overridden with an attribute: `#[repr(int)]` etc., or `#[repr(C)]` to match the target's C ABI for the equivalent C enum.

Also adds a lint pass for using non-FFI safe enums in extern declarations, checks that specified discriminants can be stored in the specified type if any, and fixes assorted code that was assuming int.
Diffstat (limited to 'src/libstd')
-rw-r--r--src/libstd/num/int_macros.rs3
-rw-r--r--src/libstd/reflect.rs10
-rw-r--r--src/libstd/repr.rs12
-rw-r--r--src/libstd/rt/io/signal.rs1
-rw-r--r--src/libstd/unstable/intrinsics.rs13
5 files changed, 23 insertions, 16 deletions
diff --git a/src/libstd/num/int_macros.rs b/src/libstd/num/int_macros.rs
index 694e5e7f6bf..ba1a7a4f912 100644
--- a/src/libstd/num/int_macros.rs
+++ b/src/libstd/num/int_macros.rs
@@ -29,7 +29,8 @@ pub static bits : uint = $bits;
 pub static bytes : uint = ($bits / 8);
 
 pub static min_value: $T = (-1 as $T) << (bits - 1);
-pub static max_value: $T = min_value - 1 as $T;
+// FIXME(#9837): Compute min_value like this so the high bits that shouldn't exist are 0.
+pub static max_value: $T = !min_value;
 
 impl CheckedDiv for $T {
     #[inline]
diff --git a/src/libstd/reflect.rs b/src/libstd/reflect.rs
index d63b14f982d..19fa9abc0da 100644
--- a/src/libstd/reflect.rs
+++ b/src/libstd/reflect.rs
@@ -16,7 +16,7 @@ Runtime type reflection
 
 #[allow(missing_doc)];
 
-use unstable::intrinsics::{Opaque, TyDesc, TyVisitor};
+use unstable::intrinsics::{Disr, Opaque, TyDesc, TyVisitor};
 use libc::c_void;
 use mem;
 use unstable::raw;
@@ -396,7 +396,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
     }
 
     fn visit_enter_enum(&mut self, n_variants: uint,
-                        get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         sz: uint, align: uint)
                      -> bool {
         self.align(align);
@@ -407,7 +407,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
     }
 
     fn visit_enter_enum_variant(&mut self, variant: uint,
-                                disr_val: int,
+                                disr_val: Disr,
                                 n_fields: uint,
                                 name: &str) -> bool {
         if ! self.inner.visit_enter_enum_variant(variant, disr_val,
@@ -426,7 +426,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
     }
 
     fn visit_leave_enum_variant(&mut self, variant: uint,
-                                disr_val: int,
+                                disr_val: Disr,
                                 n_fields: uint,
                                 name: &str) -> bool {
         if ! self.inner.visit_leave_enum_variant(variant, disr_val,
@@ -437,7 +437,7 @@ impl<V:TyVisitor + MovePtr> TyVisitor for MovePtrAdaptor<V> {
     }
 
     fn visit_leave_enum(&mut self, n_variants: uint,
-                        get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         sz: uint, align: uint) -> bool {
         if ! self.inner.visit_leave_enum(n_variants, get_disr, sz, align) {
             return false;
diff --git a/src/libstd/repr.rs b/src/libstd/repr.rs
index d03621eb60d..dd68c57e37e 100644
--- a/src/libstd/repr.rs
+++ b/src/libstd/repr.rs
@@ -29,7 +29,7 @@ use reflect::{MovePtr, align};
 use str::StrSlice;
 use to_str::ToStr;
 use vec::OwnedVector;
-use unstable::intrinsics::{Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc};
+use unstable::intrinsics::{Disr, Opaque, TyDesc, TyVisitor, get_tydesc, visit_tydesc};
 use unstable::raw;
 
 /// Representations
@@ -92,7 +92,7 @@ num_repr!(f64, "f64")
 // New implementation using reflect::MovePtr
 
 enum VariantState {
-    SearchingFor(int),
+    SearchingFor(Disr),
     Matched,
     AlreadyFound
 }
@@ -473,7 +473,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
 
     fn visit_enter_enum(&mut self,
                         _n_variants: uint,
-                        get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         _sz: uint,
                         _align: uint) -> bool {
         let disr = unsafe {
@@ -484,7 +484,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
     }
 
     fn visit_enter_enum_variant(&mut self, _variant: uint,
-                                disr_val: int,
+                                disr_val: Disr,
                                 n_fields: uint,
                                 name: &str) -> bool {
         let mut write = false;
@@ -531,7 +531,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
     }
 
     fn visit_leave_enum_variant(&mut self, _variant: uint,
-                                _disr_val: int,
+                                _disr_val: Disr,
                                 n_fields: uint,
                                 _name: &str) -> bool {
         match self.var_stk[self.var_stk.len() - 1] {
@@ -547,7 +547,7 @@ impl<'self> TyVisitor for ReprVisitor<'self> {
 
     fn visit_leave_enum(&mut self,
                         _n_variants: uint,
-                        _get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        _get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         _sz: uint,
                         _align: uint)
                         -> bool {
diff --git a/src/libstd/rt/io/signal.rs b/src/libstd/rt/io/signal.rs
index 4c6c675df03..b782d713950 100644
--- a/src/libstd/rt/io/signal.rs
+++ b/src/libstd/rt/io/signal.rs
@@ -26,6 +26,7 @@ use result::{Err, Ok};
 use rt::io::io_error;
 use rt::rtio::{IoFactory, RtioSignal, with_local_io};
 
+#[repr(int)]
 #[deriving(Eq, IterBytes)]
 pub enum Signum {
     /// Equivalent to SIGBREAK, delivered when the user presses Ctrl-Break.
diff --git a/src/libstd/unstable/intrinsics.rs b/src/libstd/unstable/intrinsics.rs
index 1900d9d5801..20563718a6c 100644
--- a/src/libstd/unstable/intrinsics.rs
+++ b/src/libstd/unstable/intrinsics.rs
@@ -75,6 +75,11 @@ pub struct TyDesc {
 #[cfg(not(test))]
 pub enum Opaque { }
 
+#[cfg(stage0)]
+pub type Disr = int;
+#[cfg(not(stage0))]
+pub type Disr = u64;
+
 #[lang="ty_visitor"]
 #[cfg(not(test))]
 pub trait TyVisitor {
@@ -140,19 +145,19 @@ pub trait TyVisitor {
                        sz: uint, align: uint) -> bool;
 
     fn visit_enter_enum(&mut self, n_variants: uint,
-                        get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         sz: uint, align: uint) -> bool;
     fn visit_enter_enum_variant(&mut self, variant: uint,
-                                disr_val: int,
+                                disr_val: Disr,
                                 n_fields: uint,
                                 name: &str) -> bool;
     fn visit_enum_variant_field(&mut self, i: uint, offset: uint, inner: *TyDesc) -> bool;
     fn visit_leave_enum_variant(&mut self, variant: uint,
-                                disr_val: int,
+                                disr_val: Disr,
                                 n_fields: uint,
                                 name: &str) -> bool;
     fn visit_leave_enum(&mut self, n_variants: uint,
-                        get_disr: extern unsafe fn(ptr: *Opaque) -> int,
+                        get_disr: extern unsafe fn(ptr: *Opaque) -> Disr,
                         sz: uint, align: uint) -> bool;
 
     fn visit_enter_fn(&mut self, purity: uint, proto: uint,