rust-lang/rust · error

obj_size_bound: unknown pointer bit size {bits}

Error message

obj_size_bound: unknown pointer bit size {bits}

What it means

TargetDataLayout::obj_size_bound returns the exclusive upper bound on object size for the default address space (1<<15, 1<<31, 1<<61 bytes for 16/32/64-bit pointers). The match covers only those three widths; any other pointer_size.bits() panics. The bound keeps isize offset arithmetic and LLVM's 64-bit bit-size representation well-defined.

Source

Thrown at compiler/rustc_abi/src/lib.rs:655

    }

    /// Returns **exclusive** upper bound on object size in bytes, in the default data address
    /// space.
    ///
    /// The theoretical maximum object size is defined as the maximum positive `isize` value.
    /// This ensures that the `offset` semantics remain well-defined by allowing it to correctly
    /// index every address within an object along with one byte past the end, along with allowing
    /// `isize` to store the difference between any two pointers into an object.
    ///
    /// LLVM uses a 64-bit integer to represent object size in *bits*, but we care only for bytes,
    /// so we adopt such a more-constrained size bound due to its technical limitations.
    #[inline]
    pub fn obj_size_bound(&self) -> u64 {
        match self.pointer_size().bits() {
            16 => 1 << 15,
            32 => 1 << 31,
            64 => 1 << 61,
            bits => panic!("obj_size_bound: unknown pointer bit size {bits}"),
        }
    }

    /// Returns **exclusive** upper bound on object size in bytes.
    ///
    /// The theoretical maximum object size is defined as the maximum positive `isize` value.
    /// This ensures that the `offset` semantics remain well-defined by allowing it to correctly
    /// index every address within an object along with one byte past the end, along with allowing
    /// `isize` to store the difference between any two pointers into an object.
    ///
    /// LLVM uses a 64-bit integer to represent object size in *bits*, but we care only for bytes,
    /// so we adopt such a more-constrained size bound due to its technical limitations.
    #[inline]
    pub fn obj_size_bound_in(&self, address_space: AddressSpace) -> u64 {
        match self.pointer_size_in(address_space).bits() {
            16 => 1 << 15,
            32 => 1 << 31,
            64 => 1 << 61,

View on GitHub (pinned to 22057b88b0)

Solutions

  1. Inspect the target's pointer width: rustc --print target-spec-json (or -C target-spec) and check the 'p:size:align' token in data-layout.
  2. If a non-16/32/64 width is legitimate for your target, extend the match arms in obj_size_bound and obj_size_bound_in.
  3. Otherwise fix the malformed target spec / data-layout string that produced the wrong pointer size.

Example fix

// before
match self.pointer_size().bits() {
    16 => 1 << 15,
    32 => 1 << 31,
    64 => 1 << 61,
    bits => panic!("obj_size_bound: unknown pointer bit size {bits}"),
}

// after: add support for the new width
match self.pointer_size().bits() {
    16 => 1 << 15,
    32 => 1 << 31,
    64 => 1 << 61,
    128 => 1 << 121,
    bits => panic!("obj_size_bound: unknown pointer bit size {bits}"),
}
Defensive patterns

Strategy: validation

Validate before calling

// Validate BEFORE calling obj_size_bound():
use rustc_abi::TargetDataLayout;

#[inline]
fn check_pointer_bits_for_obj_size_bound(dl: &TargetDataLayout) -> Result<(), String> {
    match dl.pointer_size().bits() {
        16 | 32 | 64 => Ok(()),
        bits => Err(format!("obj_size_bound: unsupported pointer bit size {bits}")),
    }
}

check_pointer_bits_for_obj_size_bound(&dl)?;
let bound = dl.obj_size_bound(); // safe now

Type guard

// Predicate narrowing TargetDataLayout to one obj_size_bound supports.
fn supports_obj_size_bound(dl: &rustc_abi::TargetDataLayout) -> bool {
    matches!(dl.pointer_size().bits(), 16 | 32 | 64)
}

// Usage:
// if supports_obj_size_bound(&dl) { Some(dl.obj_size_bound()) } else { None }

Prevention

When it happens

Trigger: Calling obj_size_bound() on a TargetDataLayout whose default pointer_size is not 16/32/64 bits — for example a custom target spec with pointer_width = "128" (CHERI / experimental ISAs) or a hand-built layout parsed from a malformed data-layout string.

Common situations: Adding or using an experimental target whose pointer width differs from the supported set without extending the match arms; constructing a TargetDataLayout in a codegen tool (cranelift/miri) from a hand-written spec; a typo in the target's data-layout 'p:size:align' token.

Related errors


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