Ruta graveolens  ·  notes from a language experiment  ·  cultivated since 2025

E0950: Bit cast width mismatch

CodeCategoryStability
E0950ArrayPermanent

Explanation

@bitCast reinterprets an integer value's bits at another integer type of the same width. It is the bit-preserving counterpart to @intCast: @bitCast keeps the representation and lets the number change, while @intCast keeps the number and rejects the values the target cannot represent. A reinterpretation neither invents nor discards bits, so it is defined only between the same-width pairs — i8/u8, i16/u16, i32/u32, and i64/u64 — in either direction, and between an integer type and itself. E0950 reports a width disagreement specifically: a target type that cannot be inferred reports E0709 instead, and a non-integer argument or target reports E0702.

Likely cause

The target type — taken from the surrounding annotation, parameter, or return position exactly as @intCast's is — is narrower or wider than the argument's type, as in let narrow: u32 = @bitCast(wide); for a u64 source. Reinterpret at the same-width partner of the argument's type when only a signedness change was intended, or use @intCast when the width change was intended.

Examples

Reinterpret across a width change

fn main() -> i32 {
    let wide: u64 = 1;
    let narrow: u32 = @bitCast(wide);
    0
}

Reinterpret at the same width

fn main() -> i32 {
    let bits: u32 = 1;
    let signed: i32 = @bitCast(bits);
    signed
}

References