Skip to content

Object literal into an interface union; fewer-parameter callback fixes; design notes - #738

Merged
ASDAlexander77 merged 5 commits into
mainfrom
interface-union-literal
Oct 29, 2026
Merged

ASDAlexander77 merged 5 commits into
mainfrom
interface-union-literal

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Stacked on #737 (in/instanceof on interface unions). After #737 merges, the base moves to main.

1. An object literal or class object passed to a union of interfaces. For example, move({ swim: () => "s" }) or move(new Crow()) for move(a: Fish | Bird).

  • Before: both failed with "'ts.Cast' op type [...] can't be stored in [...]".
  • Now: the value is cast to the first interface in the union whose required fields and methods it all has (matched by name). The union's tag then records which interface it holds, so "swim" in a works on it (in and instanceof on a union of interfaces #737).
  • Test: 00interface_union_literal.

2. Two bugs in the fewer-parameters wrapper (#733, #735). Giving the default library's array callbacks an index parameter turned these up (companion DefaultLib PR).

  • Generic inference: a capturing arrow passed for (v: T, i: number) => void had its captures paired with T, so T came out as string | Reference<...>. Inference now skips the parameter that holds the captures.
  • Own mode: a heap value (such as a string) returned through the wrapper was rejected as a second reference. The wrapper's call result is now marked owned and handed to its return, like return f(x).
  • Test: 00closure_fewer_parameters, extended.

3. Design notes for later.

  • docs/generic-interface-methods-design.md covers generic methods in interfaces, and the silent bug that generic class methods aren't virtual: an override is never reached through a base-class reference.
  • docs/classes-as-values-design.md covers class values (typeof A) and local classes that capture variables.
  • docs/tuple-object-shape-ambiguity-design.md is re-checked and still open.

Validation: ctest 6333/6333. DefaultLib, with its companion change, release JIT: gc 182/182, rc and none 181 (1 skipped), own 171 (11 skipped).

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits October 29, 2026 14:52
`"swim" in a` for `a: Fish | Bird` was "not supported", and
`a instanceof Fish` was always false (the types compared, `Fish | Bird`
against `Fish`). Every interface's union tag reads "interface", so
nothing at run time said which interface the union held.

Which members have the key, or are the interface, is decided when
compiling: every one or none folds to a constant. Otherwise the union's
tag is asked: an interface's descriptor now has an id of its name in
TYPE_DESCR_LAYOUT, and ts.IsTypeTagOf compares a tag's with a type's.
`"k" in a` narrows `a` to the members with the key (and the `else` to
the others), as `instanceof` already narrowed.

Test: 00in_interface_union (compile and JIT; gc, rc, none, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`move({ swim: () => "s" })` for `move(a: Fish | Bird)` failed with
"'ts.Cast' op type [...] can't be stored in [...]", and so did a class
object. castToTaggedUnionType now casts such a value to the first
interface member whose every required field and method it has
(interfaceMemberFitting, by name, as `"k" in v` asks), and the union's
tag says which (`"swim" in a`, #737).

Test: 00interface_union_literal (compile and JIT; gc, rc, none, own).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two bugs in the fewer-parameters thunk (#733, #735), found by giving the
default library's array callbacks an index parameter:

- A closure given for a generic callback `(v: T, i: number) => void` had
  its captures paired with `T` (`string | Reference<{...}>`): its type is
  its function bound over its captures. Inference now drops the bound
  first parameter, of the closure's type and of the arrow type rebuilt.
- Under -mm=own a heap result the thunk hands on (a string a `map`
  callback builds) was "a second reference": the thunk's call is now
  marked owned and consumed by its return, as `return f(x)` is.

Test: 00closure_fewer_parameters (a generic callback, a string result).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- docs/generic-interface-methods-design.md: generic methods in interfaces
  (rejected today) and in class hierarchies (not virtual: an override is
  never reached through a base reference, silently). Options: erased
  `any` slots, a closed-world table, dictionary passing; recommends an
  error for overridden generic class methods now, erased slots later.
- docs/classes-as-values-design.md: `typeof A` values (ts.ClassRef has no
  value lowering) and local classes that capture (members lifted with an
  extra parameter nobody passes). Recommends the vtable as the class
  value with a `.constructor` slot, and the captures in a hidden
  `.captured` field of the instance.
- docs/tuple-object-shape-ambiguity-design.md: re-checked, still open.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 181c991 into main Oct 29, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the interface-union-literal branch October 29, 2026 16:03
ASDAlexander77 added a commit that referenced this pull request Oct 31, 2026
…ttent tslang crash) (#764)

* A generic callback's T no longer takes in the closure's captures

An arrow that reads `this`, given to a generic function in a class method
(`this.particles.filter((p) => p.d > this.lifetime)`), is rebuilt while the method's return type
is discovered, and its type then has the captures ahead of its parameters. Inference dropped that
input only when the arrow was already bound (#738), not on this first pass, where it is still a
bare generic reference: `T` became `Element | captures`. With the element's class declared later
in the file that failed every time ("cast from array type ... to array type
'!ts.array<!ts.union<..., !ts.ref<!ts.tuple<{"this",...'"); with the default library's
Array.filter the union's members came in an order of their types' addresses, and when the
captures came first the arrow's parameter was taken for them: tslang crashed (0xC0000005; "Invalid
index!" in a debug build) in about one run in four on native_ts_graphics'
core/ray_tracing_extended_scene.ts, and pre-alpha111's own build too. The type helper now says
whether the type it built has captures first, and inference drops them in either case.

Test: tests/00generic_callback_closure_late_class.ts (gc, and rc/none through the corpus).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Anonymous function names hash the file's name, not its attribute's address

hash_value of the location's StringAttr hashes the attribute's storage, so an arrow's name
(`..afL89C49FH<hash>`) was different in every run. It is now xxh3 of the file name.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Register the llvm dialect's inliner interface (debug JIT failed its promise check)

The llvm dialect promises a DialectInlinerInterface that only registerAllDialects provides. A
debug (assertions) tslang ended every --emit=jit with "checking for an interface
(`mlir::DialectInlinerInterface`) that was promised by dialect 'llvm' but never implemented".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* .gitignore: every JIT cache folder (__jit*/), the debug one (__jit_dbg_gc) too

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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