rust-lang/rust · error
cannot cast a non-native integer to type {:?}
Error message
cannot cast a non-native integer to type {:?} What it means
This panic fires inside int_to_float_cast when converting a non-native (array-backed, i128/u128) integer to a floating-point type whose TypeKind is neither Float (f32), Double (f64), nor FP128 (f128). For those three the backend emits a call to a compiler-rt/libgcc helper like __floatuntisf; any other kind (e.g. Half/f16, vectors, complex, or a misclassified type) hits the catch-all panic because no helper name is defined.
Source
Thrown at compiler/rustc_codegen_gcc/src/int.rs:914
fn int_to_float_cast(
&self,
signed: bool,
value: RValue<'gcc>,
dest_typ: Type<'gcc>,
) -> RValue<'gcc> {
let value_type = value.get_type();
if self.is_native_int_type_or_bool(value_type) {
return self.context.new_cast(None, value, dest_typ);
}
debug_assert!(value_type.dyncast_array().is_some());
let name_suffix = match self.type_kind(dest_typ) {
// cSpell:disable
TypeKind::Float => "tisf",
TypeKind::Double => "tidf",
TypeKind::FP128 => "titf",
// cSpell:enable
kind => panic!("cannot cast a non-native integer to type {:?}", kind),
};
let sign = if signed { "" } else { "un" };
let func_name = format!("__float{}{}", sign, name_suffix);
let param = self.context.new_parameter(None, value_type, "n");
let func = self.context.new_function(
None,
FunctionType::Extern,
dest_typ,
&[param],
func_name,
false,
);
self.context.new_call(None, func, &[value])
}
pub fn gcc_int_to_float_cast(&self, value: RValue<'gcc>, dest_typ: Type<'gcc>) -> RValue<'gcc> {
self.int_to_float_cast(true, value, dest_typ)
}View on GitHub (pinned to 22057b88b0)
Solutions
- Avoid casting i128/u128 directly to f16/f128-or-other unsupported float; convert through f32 or f64 first.
- Add the missing TypeKind arm to int_to_float_cast (e.g. TypeKind::Half => "tihf" or route via f32) if targeting a supported runtime helper.
- Confirm the destination type is a scalar float and not a vector/complex type masquerading as a float.
- Upgrade libgccjit; verify the float TypeKind reported matches Float/Double/FP128 for the destination.
Example fix
// before let f = (huge_u128 as f16).to_le_bits(); // after (convert through f32, then narrow to f16) let f = (huge_u128 as f32) as f16;
Defensive patterns
Strategy: validation
Validate before calling
// Validate the destination is a supported float kind BEFORE casting a 128-bit int.
pub enum CastDest { F32, F64 }
pub fn int128_to_float(x: i128, dest: CastDest) -> f64 {
match dest {
CastDest::F32 => x as f32 as f64, // OK: Float kind
CastDest::F64 => x as f64, // OK: Double kind
// No other branches -> cannot reach the panic at int.rs:914
}
} Type guard
// Only allow float destinations for 128-bit-int -> float conversion.
mod float_dest {
pub trait FloatRepr: Copy + private::Sealed {}
mod private { pub trait Sealed {} }
impl Sealed for f32 {} impl FloatRepr for f32 {}
impl Sealed for f64 {} impl FloatRepr for f64 {}
}
pub fn int_to_float<F: float_dest::FloatRepr>(x: i128) -> F { x as F } // F is f32 or f64 only Prevention
- Only ever cast i128/u128 to f32 or f64 (Float/Double kinds); the panic fires for any other TypeKind.
- Never cast 128-bit ints to custom newtype/fixed-point numeric types expecting a non-float codegen kind.
- Bound generic int->float helpers by a FloatRepr sealed trait so invalid destinations fail to compile.
- Add a unit test asserting the cast target type kind is Float/Double before enabling the GCC backend.
When it happens
Trigger: Casting i128/u128 to a 16-bit float (f16 / TypeKind::Half) or to a float-like type that is not f32/f64/f128, on a target where the 128-bit integer is non-native so the helper-call path is taken. Also triggered if libgccjit reports the destination float with an unexpected TypeKind.
Common situations: Code casting u128 to f16 (half-precision) for ML/graphics workloads; crates using half::f16 with 128-bit integer sources on 32-bit targets; libgccjit version changes that alter float TypeKind enumeration.
Related errors
- cannot cast a {:?} to non-native integer
- Called extract_element on a non-vector type
- Kind: {:?}
- first index in inbounds_gep
- vector type
AI-assisted analysis of rust-lang/rust@22057b88b0 (2026-08-03).
Data as JSON: /data/errors/ed8f5559b2038489.json.
Report an issue: GitHub.