Skip to content

transpile: Compound assignment with unsigned RHS sometimes casts twice #1701

@Rua

Description

@Rua

C code:

enum E { A };

void test(void) {
    int i = 0;
    unsigned int u = 0;
    enum E e = A;
    e += u;
    i += u;
}

Transpiled result:

pub type E = ::core::ffi::c_uint;
pub const A: E = 0;
#[no_mangle]
pub unsafe extern "C" fn test() {
    unsafe {
        let mut i: ::core::ffi::c_int = 0 as ::core::ffi::c_int;
        let mut u: ::core::ffi::c_uint = 0 as ::core::ffi::c_uint;
        let mut e: E = A;
        e = (e as ::core::ffi::c_uint).wrapping_add(u) as E as E;
        i = (i as ::core::ffi::c_uint).wrapping_add(u) as ::core::ffi::c_int
            as ::core::ffi::c_int;
    }
}

The cast to E and c_int is added twice. This seems to occur if both conditions are met:

  • The RHS is unsigned.
  • The addition triggers automatic promotion of the LHS to the RHS type, requiring a cast back afterwards.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions