Repository navigation
instanceof asks a union or an optional that may hold a class at run time - #451
Merged
Merged
Conversation
…ime (#450) `x instanceof C` with `x: C | null`, `C | D`, `C | number` or `C | undefined` was always false, under every memory model, even after narrowing. mlirGenInstanceOfLogic dispatched on the left operand's type - a class, `any`, opaque or an interface - and a union or an optional fell through to the compile-time comparison of the two types, which emitted the constant `false`. Now such a value is asked at run time, through the instance's own vtable (slot 0, `.instanceOf`), as an `any` is: - an optional, when it has a value; - `C | null`, which needs no tag, when the pointer is set; - a tagged union, when its tag is "class" (every class member's tag is), its payload read as the first class member's pointer. Anything else it may hold is an instance of nothing. Test: 00instanceof_union (compile and JIT, and in the rc/none corpus). 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.
Fixes #450.
x instanceof Cwithx: C | null,C | D,C | numberorC | undefinedwas alwaysfalse, under every memory model, even after narrowing (if (e) e instanceof C).mlirGenInstanceOfLogicdispatched on the left operand's type: a class,any, opaque or an interface. A union or an optional fell through to the compile-time comparison of the two types, which emitted the constantfalse.Now a union or an optional with a class among its members is asked at run time, through the instance's own vtable (slot 0,
.instanceOf), as ananyalready is:C | null, which needs no tag, when the pointer is set;"class"(every class member's tag is), its payload read as the first class member's pointer. The vtable then decides which class it is, soC | Dholding aDgivesinstanceof Dtrue andinstanceof Cfalse.Anything else it may hold (null, undefined, a number) is an instance of nothing.
Tests
tests/00instanceof_union.ts: each shape holding a class and holding something else, a subclass held as its nullable base, aC | Dparameter, a narrowed value, and a plain class for comparison. It failed at its first assert before the change. Registered as compile and JIT tests, and in the rc/none corpus list.-noopt.test-compile-gc-defaultlib-collector, the known local LNK2005 against the stale installed default library (fails on main the same way).-mm=owncorpus: unchanged except the new file, which compiles under own too.🤖 Generated with Claude Code