Skip to content

#[TypeDef] repr is not enforced: an out-of-range value is silently truncated in a narrow slot #110

Closed

Description

@blacktrs

Problem

A #[TypeDef(repr: 'i8')] value is a full 64-bit int everywhere except in a narrow slot, and nothing checks its range, so the same value means different things depending on where it is stored:

#[TypeDef(repr: 'i8')]
final class I8 { public function __construct(public readonly int $value) {} }
final class Holder { public function __construct(public I8 $v) {} }

echo (new I8(300))->value;                  // 300  (register)
echo (new Holder(new I8(300)))->v->value;   // 44   (1-byte property slot: silently truncated)
// and in an Int8Array<I8> element:            ValueError

Arithmetic in a method (new I8($a->value + $b->value)) silently changes meaning the same way once the result is stored.

Proposal

Enforce repr once, where the value is created: every new C(…) of a narrow TypeDef checks the value its constructor or normaliser produced.

  • Out of range is a ValueError("Value 300 is out of range for I8 (-128..127)") — never a silent wrap. Wrap-around is written out by the programmer ((($x + 128) & 0xFF) - 128).
  • f32 rounds to binary32 at creation, so a register and a slot hold the same number; a finite value beyond binary32's range is the ValueError, infinities and NaN pass.
  • i64, u64, f64 and a TypeDef with no repr are unchanged.

Acceptance criteria

  • new I8(300), new I8(-129) and an out-of-range normaliser result throw ValueError with the class name and range.
  • An in-range value reads the same from a local, a property slot and a typed-array element.
  • f32 values are binary32-rounded at creation; 1e39 throws; INF and NAN pass.
  • docs/attributes.md describes the check.

Activity

  1. added
    bug
    on Oct 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions