librustc: Make uninhabited enums not castable to int
This commit is contained in:
parent
dc5df61bc1
commit
c0f587de34
2 changed files with 16 additions and 6 deletions
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
7
src/test/compile-fail/uninhabited-enum-cast.rs
Normal file
7
src/test/compile-fail/uninhabited-enum-cast.rs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
enum E {}
|
||||
|
||||
fn f(e: E) {
|
||||
println((e as int).to_str()); //~ ERROR non-scalar cast
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
Loading…
Add table
Add a link
Reference in a new issue