Skip to content

Fix crash lowering a global const object literal with a mutating method - #246

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-global-const-object-method-crash
Jul 18, 2026
Merged

Fix crash lowering a global const object literal with a mutating method#246
ASDAlexander77 merged 1 commit into
mainfrom
fix-global-const-object-method-crash

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Summary

  • A top-level const obj = { ..., method() { this.x = ... } } crashed the compiler (0xC0000005) at JIT materialization time. let at global scope and const declared locally inside a function were both unaffected, which is why this went unnoticed until now.
  • Root cause: GlobalOpLowering decides whether a global needs a runtime constructor function or can be a plain static LLVM constant initializer by walking its initializer region against a fixed allowlist of op kinds — but had no case for a ConstantOp whose TupleType/ConstTupleType result has a bound-method field. Such a field lowers via LLVM::AddressOfOp + LLVM::InsertValueOp (LLVMCodeHelper::getTupleFromArrayAttr), which is not valid inside a static global initializer region — only inside real code (a function) — so the global ended up with a corrupted struct layout and crashed on the first indirect call through it.
  • Fix is narrowly scoped to TupleType/ConstTupleType constants with a bound-method field (MLIRTypeHelper::hasBoundMethodField, from Give const bindings with bound-method fields (generators) real identity storage #244), not "any constant referencing a symbol." An earlier, broader attempt keyed on "does the constant's attribute contain a FlatSymbolRefAttr anywhere" also caught MSVC RTTI/EH type-descriptor globals (e.g. ??_R0PEAD@8, which reference the type_info vtable symbol) that were already lowering correctly as static data — forcing those through the constructor path broke JIT symbol materialization for the whole module. That regression was caught and fixed before this PR by narrowing the check to the type-based predicate.

Test plan

  • Original crash repro (top-level const object with a mutating method) now runs cleanly
  • Regression test added: test/tester/tests/00global_const_object_method.ts (compile + JIT)
  • Full suite: 351/351 JIT + 355/355 compile, 0 failures
  • Confirmed the RTTI/EH regression this fix's first attempt introduced (00try_finally_break_continue and 11 others) is resolved

🤖 Generated with Claude Code

@ASDAlexander77
ASDAlexander77 merged commit 7d9f8d6 into main Jul 18, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-global-const-object-method-crash branch July 18, 2026 14:22
ASDAlexander77 added a commit that referenced this pull request Jul 18, 2026
The #246 isBoundMethodTupleConstant special case in GlobalOpLowering
decided whether a global's initializer needs the constructor path based on
whether any ConstantOp in it had a bound-method-shaped tuple field. Since
PR #249 boxes every method-bearing object literal, such a literal's
initializer always contains a NewOp (the boxing recipe), which the walk's
existing NewOp case already forces onto the constructor path -- making the
bound-method check redundant. Verified via a diagnostic probe at that call
site across every test file (zero hits) before removing it for real.

Investigated but did NOT remove needsIdentityStorage/boundRefMaterializedCache:
contrary to the original plan (docs/object-literal-boxing-design.md), these
are still load-bearing. Whenever a boxed literal is unboxed back into a
plain tuple to match a declared/inferred type (annotated const, parameter,
interface-cast fallback, generic intersection/inference), the resulting
tuple's method field is still bound-method-shaped even though the tuple
itself is a plain value -- that machinery is what makes calling through it
work correctly instead of crash. Root-caused via site-tagged diagnostic
probes across the full test corpus; documented in the design doc so this
isn't re-attempted without re-deriving the same finding.

Full suite green: 353/353 JIT + 357/357 AOT.

Co-authored-by: Claude Sonnet 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