Repository navigation
#[TypeDef] repr is enforced where the value is created: out of range is a ValueError, f32 rounds to binary32 - #111
Merged
Merged
Conversation
…of range is a ValueError instead of a silent truncation in a narrow slot, and f32 rounds to binary32. Fixes #110
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 anInt8Array<I8>.What changes for users
new C(…)of a TypeDef with a narrow integerrepr(i8,i16,i32,u8,u16,u32) checks the value its constructor or normaliser produced. Out of range isValueError("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).f32TypeDef rounds to binary32 at creation, so a register and a slot hold the same number; a finite value beyond binary32's range is theValueError, infinities and NaN pass.i64,u64,f64and a TypeDef withoutreprare unchanged.docs/attributes.mddescribes the check.How
One synthesized function per narrow TypeDef,
C____range(carrier): C, beside the synthesized normaliser;new C(…)lowers toC____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_typedefcases green after the rebase ontov0.13.0. Linux: this PR's CI.Fixes #110