Skip to content

Incorrect translation of long doublebool causes Rust type mismatch #1692

@lynae99

Description

@lynae99

When translating a cast from long double to bool, the transpiler generates a comparison against an untyped floating-point literal (0.).
However, the long double value is translated to ::f128::f128, which results in a type mismatch (f128 vs default float literal) and causes the generated Rust code to fail compilation.
Reproducer

#include <stdbool.h>

int main(void) {
    long double ld_zero = 0.0L;
    long double ld_non_zero = 0.1L;

    bool ld_zero_as_bool = (bool)ld_zero;
    bool ld_non_zero_as_bool = (bool)ld_non_zero;
}

Generated Rust

#![allow(
    dead_code,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
unsafe fn main_0() -> ::core::ffi::c_int {
    let mut ld_zero: ::f128::f128 = ::f128::f128::new(0.0);
    let mut ld_non_zero: ::f128::f128 = ::f128::f128::new(0.1);
    let mut ld_zero_as_bool: bool = ld_zero != 0.;
    let mut ld_non_zero_as_bool: bool = ld_non_zero != 0.;
    return 0;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

Observed Behavior
The generated Rust code fails to compile:

error[E0308]: mismatched types
  --> src/main.rs:12:48
   |
12 |     let mut ld_zero_as_bool: bool = ld_zero != 0.;
   |                                                ^^ expected struct `f128`, found floating-point number

error[E0308]: mismatched types
  --> src/main.rs:13:56
   |
13 |     let mut ld_non_zero_as_bool: bool = ld_non_zero != 0.;
   |                                                        ^^ expected struct `f128`, found floating-point number

Environment

  • c2rust version: v0.21.0
  • platform: Ubuntu 24.04
  • Rust: nightly-2022-08-08
  • Clang version: 17.0.6

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