rust-lang/rust · error

unexpected integer size

Error message

unexpected integer size

What it means

In `operation_with_overflow`, after recovering the element type, the code selects the result `Ty` based on `width`: `32 => i32`, `64 => i64`, `128 => i128`. Any other width (e.g. 16, or a non-power-of-two passed by a caller) hits `unreachable!("unexpected integer size")`. Width is propagated from `gcc_checked_binop`'s literal `32/64/128` constants, so a different value implies the caller was extended without updating this match.

Source

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

        let overflow_param_type = overflow_type.make_pointer();
        let res_type = a_type;

        let overflow_value =
            self.current_func().new_local(self.location, overflow_type, "overflow");
        let overflow_addr = overflow_value.get_address(self.location);

        let param_a = self.context.new_parameter(self.location, a_type, "a");
        let param_b = self.context.new_parameter(self.location, b_type, "b");
        let param_overflow =
            self.context.new_parameter(self.location, overflow_param_type, "overflow");

        let a_elem_type = a_type.dyncast_array().expect("non-array a value");
        debug_assert!(a_elem_type.is_integral());
        let res_ty = match width {
            32 => self.tcx.types.i32,
            64 => self.tcx.types.i64,
            128 => self.tcx.types.i128,
            _ => unreachable!("unexpected integer size"),
        };
        let layout = self
            .tcx
            .layout_of(ty::TypingEnv::fully_monomorphized().as_query_input(res_ty))
            .unwrap();

        let arg_abi = ArgAbi { layout, mode: PassMode::Direct(ArgAttributes::new()) };
        let mut fn_abi = FnAbi {
            args: vec![arg_abi.clone(), arg_abi.clone(), arg_abi.clone()].into_boxed_slice(),
            ret: arg_abi,
            c_variadic: false,
            fixed_count: 3,
            conv: CanonAbi::C,
            can_unwind: false,
        };
        fn_abi.adjust_for_foreign_abi(self.cx, ExternAbi::C { unwind: false });

        let ret_indirect = matches!(fn_abi.ret.mode, PassMode::Indirect { .. });

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Trace the `width` argument back to `gcc_checked_binop` and confirm which arm produced it.
  2. Add an arm here for the new width mapping to the corresponding `tcx.types.i*`.
  3. Alternatively, derive `res_ty` directly from `width`/element type instead of a hardcoded match, to keep the two sites in sync.

Example fix

// before
let res_ty = match width {
    32 => self.tcx.types.i32,
    64 => self.tcx.types.i64,
    128 => self.tcx.types.i128,
    _ => unreachable!("unexpected integer size"),
};

// after
let res_ty = match width {
    32 => self.tcx.types.i32,
    64 => self.tcx.types.i64,
    128 => self.tcx.types.i128,
    other => unreachable!("operation_with_overflow: unsupported width {}", other),
};
Defensive patterns

Strategy: validation

Validate before calling

// operation_with_overflow only accepts width in {32, 64, 128}.
const SUPPORTED: [u64; 3] = [32, 64, 128];
if !SUPPORTED.contains(&width) {
    return Err(format!("operation_with_overflow: width {} not in {{32,64,128}}", width));
}
builder.operation_with_overflow(func_name, lhs, rhs, width)

Prevention

When it happens

Trigger: `operation_with_overflow(func_name, lhs, rhs, width)` is invoked with a `width` not in `{32, 64, 128}`. Currently only reachable if a new arm is added to the upstream match in `gcc_checked_binop` (errors 134–136) passing a novel width.

Common situations: A patch adding support for a new non-native width (e.g. 16-bit overflow) without extending this match. Not user-facing; an internal invariant.

Related errors


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