rust-lang/rust · error

cannot cast a {:?} to non-native integer

Error message

cannot cast a {:?} to non-native integer

What it means

This panic fires inside float_to_int_cast when converting a floating-point value to a non-native (array-backed, i128/u128) integer, in the branch that selects the compiler-rt/libgcc fix helper name by the source float's TypeKind. Only Half (routed via f32), Float (f32), Double (f64), and FP128 (f128) have defined suffixes (sfti/dfti/tfti). Any other kind reaching this branch (vector floats, complex, or a misreported type) panics.

Source

Thrown at compiler/rustc_codegen_gcc/src/int.rs:965

    ) -> RValue<'gcc> {
        let value_type = value.get_type();
        if self.is_native_int_type_or_bool(dest_typ) {
            return self.context.new_cast(None, value, dest_typ);
        }

        debug_assert!(dest_typ.dyncast_array().is_some());
        let (dest_type, param_type) = match self.type_kind(value_type) {
            TypeKind::Half => (Some(self.float_type), self.float_type),
            _ => (None, value_type),
        };
        let name_suffix = match self.type_kind(value_type) {
            // cSpell:disable
            // Since we will cast Half to a float, we use sfti for both.
            TypeKind::Half | TypeKind::Float => "sfti",
            TypeKind::Double => "dfti",
            TypeKind::FP128 => "tfti",
            // cSpell:enable
            kind => panic!("cannot cast a {:?} to non-native integer", kind),
        };
        let sign = if signed { "" } else { "uns" };
        let func_name = format!("__fix{}{}", sign, name_suffix);
        let param = self.context.new_parameter(None, param_type, "n");
        let func = self.context.new_function(
            None,
            FunctionType::Extern,
            dest_typ,
            &[param],
            func_name,
            false,
        );
        if let Some(dest_type) = dest_type {
            value = self.context.new_cast(None, value, dest_type);
        }
        self.context.new_call(None, func, &[value])
    }

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Ensure the float operand is a scalar (f16/f32/f64/f128) before converting to i128/u128.
  2. Extract a lane from any vector float before casting to a 128-bit integer.
  3. Add the missing TypeKind arm to float_to_int_cast with the appropriate compiler-rt helper suffix if the float kind is genuinely supported.
  4. Upgrade libgccjit and confirm the float TypeKind matches one of the handled variants.

Example fix

// before
let n: i128 = vec_float as i128; // vector float incorrectly cast
// after (cast a scalar lane instead)
let n: i128 = vec_float.extract(0) as f32 as i128;
Defensive patterns

Strategy: validation

Validate before calling

// Validate the source is a supported float kind BEFORE casting into a 128-bit int.
pub enum FloatSrc { F32, F64 }
pub fn float_to_int128(src: FloatSrc, bits: u32, val: f64) -> i128 {
    match src {
        FloatSrc::F32 => (val as f32) as i128, // Half/Float -> sfti path
        FloatSrc::F64 => val as i128,          // Double -> dfti path
        // No other source kinds -> cannot reach the panic at int.rs:965
    }
}

Type guard

// Only allow float sources for float -> 128-bit-int conversion.
mod float_src {
    pub trait FloatSrc: Copy + private::Sealed {}
    mod private { pub trait Sealed {} }
    impl Sealed for f32 {} impl FloatSrc for f32 {}
    impl Sealed for f64 {} impl FloatSrc for f64 {}
}
pub fn float_to_int<F: float_src::FloatSrc>(x: F) -> i128 { x as i128 } // F is f32 or f64 only

Prevention

When it happens

Trigger: Casting a float whose TypeKind is not Half/Float/Double/FP128 into an i128/u128 destination on a target where the destination is array-backed. Triggered when the float operand is a vector-of-float, a complex float, or a type libgccjit reports with an unexpected TypeKind while the destination integer is non-native.

Common situations: SIMD-heavy code accidentally converting a vector float lane to a 128-bit int; cross-compiling for 32-bit targets where i128 is array-backed; libgccjit regressions in TypeKind reporting for float types.

Related errors


AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03). Data as JSON: /data/errors/083dcf6257fd8d86.json. Report an issue: GitHub.