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

Enum comparisons receive superflous and verbose casts #128

Open
TheDan64 opened this issue Jul 12, 2019 · 0 comments
Open

Enum comparisons receive superflous and verbose casts #128

TheDan64 opened this issue Jul 12, 2019 · 0 comments
Labels
enhancement New feature or request readability Generated code is hard to read and can be simplified

Comments

@TheDan64
Copy link
Contributor

typedef enum {
    FIND, ENTER
} ACTION;

int main(void) {
    ACTION action = FIND;
    if (action == FIND) {
        return 2;
    }

    return 1;
}

becomes

pub type ACTION = libc::c_uint;
pub const ENTER: ACTION = 1;
pub const FIND: ACTION = 0;
unsafe fn main_0() -> libc::c_int {
    let mut action: ACTION = FIND;
    if action as libc::c_uint == FIND as libc::c_int as libc::c_uint {
        return 2i32
    }
    return 1i32;
}

This does seem to be true to the AST (but is unnecessarily verbose):

  `-CompoundStmt 0x5293368 <col:16, line:12:1>
    |-DeclStmt 0x52931d0 <line:6:5, col:25>
    | `-VarDecl 0x5293130 <col:5, col:21> col:12 used action 'ACTION':'ACTION' cinit
    |   `-ImplicitCastExpr 0x52931b8 <col:21> 'ACTION':'ACTION' <IntegralCast>
    |     `-DeclRefExpr 0x5293190 <col:21> 'int' EnumConstant 0x5292dd0 'FIND' 'int'
    |-IfStmt 0x52932f8 <line:7:5, line:9:5>
    | |-<<<NULL>>>
    | |-<<<NULL>>>
    | |-BinaryOperator 0x5293280 <line:7:9, col:19> 'int' '=='
    | | |-ImplicitCastExpr 0x5293250 <col:9> 'unsigned int' <IntegralCast>
    | | | `-ImplicitCastExpr 0x5293238 <col:9> 'ACTION':'ACTION' <LValueToRValue>
    | | |   `-DeclRefExpr 0x52931e8 <col:9> 'ACTION':'ACTION' lvalue Var 0x5293130 'action' 'ACTION':'ACTION'
    | | `-ImplicitCastExpr 0x5293268 <col:19> 'unsigned int' <IntegralCast>
    | |   `-DeclRefExpr 0x5293210 <col:19> 'int' EnumConstant 0x5292dd0 'FIND' 'int'
@TheDan64 TheDan64 added bug Something isn't working enhancement New feature or request readability Generated code is hard to read and can be simplified and removed bug Something isn't working labels Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request readability Generated code is hard to read and can be simplified
Projects
None yet
Development

No branches or pull requests

1 participant