Repository navigation
Errors in a generated cast are reported where the cast is - #541
Merged
Merged
Conversation
castFromUnion and castPrimitiveTypeFromAny generate a function from source text and set the call site as the location while it is parsed. But its body is generated later, when the call instantiates it, and by then the location was reset: a diagnostic in the body took the generated text's offset as a position in the user's file. `const s: string = x` with `x: number | string | null` reported "can't cast from 'null' to 'string'" at 1:59, inside a comment. The call site now stays the location until the call has instantiated the function. Noticed while reviewing docs/bugs/native-ts-graphics-findings.md (issue 6's first error pointed into a comment). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ASDAlexander77
added a commit
that referenced
this pull request
Oct 17, 2026
…de for (#581) A node the compiler builds (a for...of's or for...in's loop, the call of `[Symbol.iterator]`, a `using`'s dispose call) has no position, so an error in it was reported at 1:1: for (const x of 5) {} // 1:1: Can't resolve property 'length' of type s32 using x = 5; // 1:1: Can't resolve property 'dispose' of type s32 Each statement now records where it is (without the debug scope), and loc() gives a node with no position that location, in the current scope; a statement the compiler made keeps the anchor of the one it was made for. The dispose call, made at the scope's exit, is anchored at its declaration. #541 and #557 had stamped positions on the generated cast and constructor nodes one at a time; this covers the rest. 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
An error inside a cast function the compiler generates was reported at a position that has nothing to do with the code:
Issue 6 of
docs/bugs/native-ts-graphics-findings.mdshows the same thing: its first error points into the comment on line 1.Cause
castFromUnionandcastPrimitiveTypeFromAnybuild a generic__castfunction from source text, parse it withoverwriteLocset to the call site, and then call it. The guard that setoverwriteLocwas scoped to the parse only. The body is generated later, insidemlirGenCallExpression, when the call instantiates it. By thenoverwriteLocwas unknown again, soloc()mapped the generated text's offsets onto the user's source file.Fix
Keep the guard alive until the call has instantiated the function, at both sites. With
--di,parsePartialStatementsalready strips the debug metadata fromoverwriteLoc, and that stripped value is what stays in effect.Testing
test-compile-diagnostic-location-generated-cast(error-text test, like thecall-arityones): it requiresgenerated_cast.ts:6:23: error: can't cast from 'null' to 'string'and fails ongenerated_cast.ts:1:. The current release prints1:59.anytonumber,number | stringtostring) runs the same with no flags,--di,--di --opt, and as a--diexe.🤖 Generated with Claude Code