Skip to content

Commit

Permalink
librustc: Make uninhabited enums not castable to int
Browse files Browse the repository at this point in the history
  • Loading branch information
pcwalton committed May 3, 2013
1 parent dc5df61 commit c0f587d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
15 changes: 9 additions & 6 deletions src/librustc/middle/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
7 changes: 7 additions & 0 deletions src/test/compile-fail/uninhabited-enum-cast.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
enum E {}

fn f(e: E) {
println((e as int).to_str()); //~ ERROR non-scalar cast
}

fn main() {}

0 comments on commit c0f587d

Please sign in to comment.