Skip to content

Do not narrow a typed value to Opaque on typeof "function" - #328

Merged
ASDAlexander77 merged 2 commits into
mainfrom
safe-cast-skip-generic-function-ref
Sep 14, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
safe-cast-skip-generic-function-ref

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

typeof f === "function" (also "class", "interface", "object") narrows f to Opaque. For a value whose type is already known (not any, 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.
  • 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".

Fix

addSafeCastStatement leaves a value of known type as it is when the narrowed type is Opaque. any is 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 a typeof ... === "function" branch, an uninstantiated generic function, an arrow function in ?:, and any still narrowed at run time.

Full Windows debug suite (ctest -R "^test-"): 2739/2739 passed.

Independent of #327; both touch MLIRGenImpl.h in different functions.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits September 14, 2026 23:22
`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
@ASDAlexander77
ASDAlexander77 merged commit b5b522b into main Sep 14, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the safe-cast-skip-generic-function-ref branch September 14, 2026 22:44
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.

1 participant