Skip to content

#[TypeDef] repr is enforced where the value is created: out of range is a ValueError, f32 rounds to binary32 - #111

Merged
blacktrs merged 1 commit into
mainfrom
typedef-range
Oct 7, 2026
Merged

blacktrs merged 1 commit into
mainfrom
typedef-range

Conversation

@blacktrs

@blacktrs blacktrs commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

Why

A #[TypeDef(repr: 'i8')] value was a full 64-bit int everywhere except in a narrow slot, and nothing checked its range — so the same value meant different things depending on where it was stored: new I8(300) read 300 from a local, 44 from a 1-byte property (silently truncated), and threw in an Int8Array<I8>.

What changes for users

  • Every new C(…) of a TypeDef with a narrow integer repr (i8, i16, i32, u8, u16, u32) checks the value its constructor or normaliser produced. Out of range is ValueError("Value 300 is out of range for I8 (-128..127)") — never a silent wrap. Wrap-around is written out: new I8((($x + 128) & 0xFF) - 128).
  • An f32 TypeDef 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 without repr are unchanged.
  • docs/attributes.md describes the check.

How

One synthesized function per narrow TypeDef, C____range(carrier): C, beside the synthesized normaliser; new C(…) lowers to C____range(C____invoke(…)). LLVM inlines both; a constant argument folds the check away.

Testing

New case typedef_repr_range (bounds, method arithmetic overflow, explicit wrap, property slot, normaliser result, f32 rounding / overflow / infinities / NaN) — expected output hand-written, a TypeDef has no php oracle. macOS: AOT suite 1712 passed / 0 failed on the commit before rebase; typedef, ds_, generics_typedef cases green after the rebase onto v0.13.0. Linux: this PR's CI.

Fixes #110

…of range is a ValueError instead of a silent truncation in a narrow slot, and f32 rounds to binary32. Fixes #110
@blacktrs
blacktrs merged commit f910f19 into main Oct 7, 2026
13 of 14 checks passed
@blacktrs
blacktrs deleted the typedef-range branch October 7, 2026 13:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant