Skip to content

C2Rust generates calls to f128 float methods without importing required traits #1690

@rikki322

Description

@rikki322

C2Rust generates Rust code that calls floating-point methods on f128 (e.g., is_infinite, is_sign_positive) without importing the traits that provide these methods.
As a result, the generated Rust code fails to compile unless the user manually adds the required trait import.
Reproducer

#include <stdio.h>
#include <math.h>

int main(void) {
    long double huge = __builtin_huge_val();
    printf("huge_val: Input=(none), Result=%s\n",
           isinf(huge) ? "INF" : "NOT_INF");
    return 0;
}

Transpiled Rust

#![allow(
    dead_code,
    non_camel_case_types,
    non_snake_case,
    non_upper_case_globals,
    unused_assignments,
    unused_mut
)]
extern "C" {
    fn printf(__format: *const ::core::ffi::c_char, ...) -> ::core::ffi::c_int;
}
unsafe fn main_0() -> ::core::ffi::c_int {
    let mut huge: ::f128::f128 = ::f128::f128::new(::core::f64::INFINITY);
    printf(
        b"huge_val: Input=(none), Result=%s\n\0" as *const u8
            as *const ::core::ffi::c_char,
        if if huge.is_infinite() {
            if huge.is_sign_positive() { 1 } else { -1 }
        } else {
            0
        } != 0
        {
            b"INF\0" as *const u8 as *const ::core::ffi::c_char
        } else {
            b"NOT_INF\0" as *const u8 as *const ::core::ffi::c_char
        },
    );
    return 0 as ::core::ffi::c_int;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

Observed Behavior
The generated Rust code fails to compile:

error[E0599]: no method named `is_infinite` found for struct `f128`
error[E0599]: no method named `is_sign_positive` found for struct `f128`

The compiler indicates that these methods are provided by traits such as:

num_traits::float::Float

but the generated code does not import them.
Expected Behavior
C2Rust should generate the necessary trait imports when emitting calls to trait-provided floating-point methods on f128.
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