Repository navigation
Warn when a fraction is stored into a variable inferred from an integer literal - #544
Merged
Merged
Conversation
Integer literals are s32, so `let s = 0` is an integer and `s += 1.5`, `s = s + x` or `let a = 0; a += values[i]` drop the fraction without a word, where TypeScript keeps it. The inference stays; storing a number with a fraction into such a variable now warns, and says to declare it `number`. A whole-number constant (`s = 3.0`), a variable with a declared integer type and one inferred from `0.0` are not warned about. The declaration marks its storage (ts.Variable or ts.Global) with INTEGER_LITERAL_INFERRED_ATTR_NAME; the store checks the mark. Reported in docs/bugs/native-ts-graphics-findings.md (issue 3). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…variable-truncation # Conflicts: # tslang/test/tester/CMakeLists.txt
…variable-truncation # Conflicts: # tslang/test/tester/CMakeLists.txt
ASDAlexander77
added a commit
that referenced
this pull request
Oct 12, 2026
…558) Every repro in docs/bugs passes on main, or fails as TypeScript does: the native_ts_graphics findings were fixed by #535-#544 and #549 (the `let s = 0` one by design, with a warning since #544), testFallThrough, generic_in_generic and array_of_ifaces_cast by #538, #542 and #543, and bug14 and bug21 aren't valid TypeScript. Each fix has its regression test in tslang/test/tester, and the findings document is in the history and in those PRs, so the folder goes. Its interop notes stay, in docs/c-cpp-header-import.md: a `string` returned by a C function is its `char*`, not a copy; `number` is a `double`; and a nullable `Ref<T>` field is spelled `Ref<T> | null`. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
Integer literals are
s32in tslang, solet s = 0is an integer and a fraction stored into it is dropped silently:Reported in
docs/bugs/native-ts-graphics-findings.md, issue 3. Inferrings32from integer literals is deliberate, and this PR keeps it. The report asks for what this PR adds: a warning when ordinary TypeScript code changes meaning because of it.Change
When a store into a variable converts a value of type
number(or another float type) to the variable's integer type, and that type was inferred from an integer literal, it warns:No warning for:
s = 3.0);let d: s32 = 0; d = 2.5), since that truncation was asked for;0.0, or declarednumber.How it works:
adjustLocalVariableType/adjustGlobalVariableTypenote that the type was inferred from an integer literal (no type given, and the initializer has a literal integer type).registerVariablemarks the storage (ts.Variable, orts.Globalfor a global) withINTEGER_LITERAL_INFERRED_ATTR_NAME, andmlirGenSaveLogicOneItemchecks that mark before it casts the stored value. This covers=,+=and the other compound assignments. Code generation doesn't change.Stores from inside a closure (
a.forEach(v => { s += v; })) are warned about too.Testing
test-compile-integer-literal-truncation-globaland-localrequire the warning at the store's line and column;-quietfails on any warning, for the cases listed above. The current release prints no warning, so-globaland-localfail on it.tslang/test/tester/tests(about 630). 27 of them warn, in 1 to 5 places each. All are realnumber-to-s32stores into alet x = 0variable, such asx /= 5,total = total + n.v,glb1 += xanddist = v - Math.sqrt(disc). The compiler can't know whether those values have a fraction at run time, which is why the message says "any fraction". If that's too noisy for code that relies on the integer inference on purpose, the fix is to declare the type (let x: s32 = 0), which turns the warning off.🤖 Generated with Claude Code