Skip to content

Commit 5a2c126

Browse files
cknittclaude
andcommitted
Trust Symbol.hasInstance only for objects and functions
Why: With the primitive cases tested first, a class whose `Symbol.hasInstance` accepts a primitive no longer wins over a case of that primitive kind, while the spec still said `Symbol.hasInstance` is trusted without qualification. Rule 8 now says that `Symbol.hasInstance` is trusted for objects and functions, and that a primitive whose kind is a case lands in that case first. The values of an `@instanceOf` type are instances, so a primitive of that type already contradicts its annotation. Representation modified (a primitive accepted by Symbol.hasInstance): Example: %%raw(`globalThis.Even = class { static [Symbol.hasInstance](x) { return x % 2 === 0 } }`) @instanceof("Even") type even @unboxed type t = E(even) | N(float) | S(string) Before (representation): 4 ↦ E After (representation): 4 ↦ N Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Fc7LEuyEfynjaKCVEBPmTF Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent 4558f61 commit 5a2c126

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,10 @@ if (v instanceof Animal && !(v instanceof Dog)) { ... }
321321
A value upcast to the superclass and wrapped in its case lands in the
322322
subclass case, as a payload equal to a literal lands in the literal's case
323323
(rule 7). Classes not declared to be related are tested in declaration order.
324-
`instanceof` consults `Symbol.hasInstance`, which is trusted.
324+
`instanceof` consults `Symbol.hasInstance`, which is trusted for objects and
325+
functions. A primitive whose kind is a case of the variant lands in that case
326+
before any class check runs, even if a class's `Symbol.hasInstance` would
327+
accept it: the values of a class type are instances, never primitives.
325328
An `int` case is recognized by `typeof x === "number"` and so receives every
326329
number, integral or not.
327330

0 commit comments

Comments
 (0)