Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong code generation when using the zero variant of an enum in if clauses #158

Open
skade opened this issue Jul 4, 2017 · 0 comments
Open

Comments

@skade
Copy link

skade commented Jul 4, 2017

The following code:

typedef enum
{
    false,
    true
} boolean;

boolean mayFail() {
    return false;
}

void avoidErasure() {

}

void checksForFail() {
    if (mayFail()) {
        avoidErasure();
    }
}

is converted to:

#[derive(Clone, Copy)]
#[repr(i32)]
pub enum Enum1 {
    false_,
    true_,
}

#[no_mangle]
pub unsafe extern fn mayFail() -> Enum1 { Enum1::false_ }

#[no_mangle]
pub unsafe extern fn avoidErasure() { }

#[no_mangle]
pub unsafe extern fn checksForFail() {
    if mayFail() != 0 {
        avoidErasure();
    }
}

which doesn't compile due to this:

$ rustc lets-play-bool.rs
error[E0369]: binary operation `!=` cannot be applied to type `Enum1`
  --> lets-play-bool.rs:16:8
   |
16 |     if mayFail() != 0 {
   |        ^^^^^^^^^
   |
   = note: an implementation of `std::cmp::PartialEq` might be missing for `Enum1`

error: aborting due to previous error

This implementation would work:

#[no_mangle]
pub unsafe extern fn checksForFail() {
    if (mayFail() as u32) != 0 {
        avoidErasure();
    }
}
@skade skade changed the title Wrong code generation when using the zero veriant of an enum Wrong code generation when using the zero variant of an enum in if clauses Jul 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant