Skip to content

x[k] and k in x of a value of type any, through a keys adapter in the type descriptor - #940

Merged
ASDAlexander77 merged 7 commits into
mainfrom
any-get-by-key
Nov 19, 2026
Merged

ASDAlexander77 merged 7 commits into
mainfrom
any-get-by-key

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Nov 19, 2026 •

Copy link
Copy Markdown
Owner

x[k] and k in x of a value of type any, looked up at run time (user ruling 11-19). Follow-up to #939.

How

  • New type-descriptor slot TYPE_DESCR_KEYS (index 6; the block header moves to 7). It holds an adapter (box: any, op: number, key: any, value: any) => any for each tuple, object literal, class, interface or array type boxed into any. The boxing records the type; the adapters are made once the module's statements and entry code are generated. Made at the boxing site itself (inside an arrow, or a generic being inferred), they were generated twice or used the enclosing function's values. Op 0 reads box[key]; op 1 answers key in box. value is reserved for a later write op, so adding one won't change the layout again.
  • x[k] on any calls __any_key(box, 0, key):
    • a string answers its characters and length itself;
    • null and undefined throw a TypeError;
    • otherwise the adapter is called.
    • A key the value doesn't have reads undefined.
    • A number key reads a tuple's position or an array element; length gives an array's length.
  • k in x with x: any goes through the same adapter. So does a key known only at run time when x's type knows its keys (an object literal, tuple, class or interface), which used to be "the key must be a constant".
  • Which keys:
    • A class gives its public fields and getters, including inherited ones.
    • Methods count for in, but reading one throws a TypeError.
    • An optional field that isn't set doesn't count as present.
    • The keys are those of the type at the point the value was boxed: a Derived boxed as a Base has only Base's keys.
  • A constant array literal boxed into any is now boxed as a real array. const a: any = [1, 2]; <number[]>a used to crash, on main as well.
  • Ownership: adapters return an owned box (they share the call adapters' OwnedReturnConsumptionPass handling). An rc leak probe (class and object literal boxed, fields and an array element read, in) stays flat at 61 MB from 300k to 3M iterations.
  • Not under -mm=own yet: it's a clear compile error there, the same as calls of any in Call a value of type any through an adapter made when its function was boxed; checked casts from any to a function #922.

Also fixed: wasm legs failing on the default library's new tests (DL #108)

  • The wasm host lacked ldexp/ldexpf. LLVM turns pow(2, e) into these, as in DataView.getFloat16 (dataview.ts).
  • The wasm runner ignored the // not rc marker that tests.ps1 honours. finalization_registry_models.ts is built on WeakRef, which -mm=rc doesn't have.
  • rc bug: a let of a heap type declared without a value borrowed whatever it was given. In DisposableStack.dispose(), failure = e in a catch clause took no reference; the clause released the caught value, and throw failure threw a freed block. The wasm host's heap check caught the second free; natively it went unnoticed. Such a let now starts empty and owns what it is given (not catch variables, which the landing pad writes). It is on main too, not caused by this PR. New test: 00let_without_initializer_owns. test-rc-conditional-named-const-release now expects the value inside the if to be released there, since the outer let holds its own reference.
  • Locally: the wasm32 rc default-library suite (232 pass, 0 fail), full ctest, and the native default library on gc, rc, none and own.

Layout change

The descriptor record grew by one pointer. The default library, and the wasm and Android archives, have to be rebuilt with this compiler. A program built with it and linked against an older library reads that library's type tags at the wrong offset: the rc and own default-library tests fail that way.

Limits

  • x.name (dot access) on any is still an error: the ruling covered x[k] and in.
  • Writes through any (x[k] = v, +=, ++) give a clear error: "writing 'x[k]' of a value of type 'any' is not supported".
  • A protected or private class field reads undefined (the adapter reads only what code outside the class can read).
  • A value boxed by an older default-library build has no adapter, so reading a key of it throws a TypeError ("keys ... not known").
  • An integer array literal is s32[], so <number[]> of its box reads the wrong element width. That's an existing problem, not touched here.

Tested

🤖 Generated with Claude Code

ASDAlexander77 and others added 7 commits November 19, 2026 17:33
… a keys adapter in the type descriptor (TYPE_DESCR_KEYS); a constant array is boxed as the array it makes

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e: made inside it, it was never recorded

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… value was boxed (an arrow, a generic being inferred) they were made twice or used the enclosing function's values; writing x[k] of an any says it is not supported; own keeps the constant-key message

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…'s generation, before clearTempModule: made after it, their parameters were left unlowered (LLVM Translation failed; Map/Set default library tests)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…runner skips tests marked // not rc under -mm=rc, as tests.ps1 does

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ns what it is given; it borrowed, and a caught value kept in one past its catch clause was thrown freed (DisposableStack on wasm)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… own, which refuses its copy of an any holding an object by design (ctest runs it under gc, rc and none)

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