Do not narrow a typed value to Opaque on typeof "function" - #328
Merged
Merged
Conversation
`typeof f === "function"` (also "class", "interface", "object") narrows to Opaque. For a value whose type is already known - not `any`, not a union - that cast only lost the type: - `if (typeof f === "function") f()` failed with "not a function to call", for a plain function as well; - for a generic function nobody instantiated, the cast referenced the never emitted generic and lowering failed with "'llvm.mlir.addressof' op must reference a global". addSafeCastStatement now leaves such a value as it is. `any` is still unboxed and unions are unchanged. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
…c-function-ref # Conflicts: # tslang/test/tester/CMakeLists.txt
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
typeof f === "function"(also"class","interface","object") narrowsftoOpaque. For a value whose type is already known (notany, not a union) that cast only throws the type away:if (typeof f === "function") f()failed with "not a function to call", for a plain function too.llvm.mlir.addressofop must reference a global".Fix
addSafeCastStatementleaves a value of known type as it is when the narrowed type isOpaque.anyis still unboxed at run time and unions are unchanged.Tests
New
00typeof_function_narrowing.ts(compile, JIT and corpus variants): calling a plain function and a generic function inside atypeof ... === "function"branch, an uninstantiated generic function, an arrow function in?:, andanystill narrowed at run time.Full Windows debug suite (
ctest -R "^test-"): 2739/2739 passed.Independent of #327; both touch
MLIRGenImpl.hin different functions.🤖 Generated with Claude Code