]> git.lizzy.rs Git - rust.git/commitdiff
librustc: Make uninhabited enums not castable to int
authorPatrick Walton <pcwalton@mimiga.net>
Fri, 3 May 2013 01:41:57 +0000 (18:41 -0700)
committerPatrick Walton <pcwalton@mimiga.net>
Fri, 3 May 2013 01:41:57 +0000 (18:41 -0700)
src/librustc/middle/ty.rs
src/test/compile-fail/uninhabited-enum-cast.rs [new file with mode: 0644]

index c42c4c3cf07587eb84a7bb235718e975e0f570e5..d28efcd55b00c408c8bddda57119526faf81b8f3 100644 (file)
@@ -2509,12 +2509,15 @@ pub fn type_is_enum(ty: t) -> bool {
 // constructors
 pub fn type_is_c_like_enum(cx: ctxt, ty: t) -> bool {
     match get(ty).sty {
-      ty_enum(did, _) => {
-        let variants = enum_variants(cx, did);
-        let some_n_ary = vec::any(*variants, |v| vec::len(v.args) > 0u);
-        return !some_n_ary;
-      }
-      _ => return false
+        ty_enum(did, _) => {
+            let variants = enum_variants(cx, did);
+            if variants.len() == 0 {
+                false
+            } else {
+                variants.all(|v| v.args.len() == 0)
+            }
+        }
+        _ => false
     }
 }
 
diff --git a/src/test/compile-fail/uninhabited-enum-cast.rs b/src/test/compile-fail/uninhabited-enum-cast.rs
new file mode 100644 (file)
index 0000000..c4a5dc4
--- /dev/null
@@ -0,0 +1,7 @@
+enum E {}
+
+fn f(e: E) {
+    println((e as int).to_str());   //~ ERROR non-scalar cast
+}
+
+fn main() {}