Skip to content

Exported generators under -shared: print boxed objects and quote internal field names - #315

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-exported-generator-shared
Sep 13, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-exported-generator-shared

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Closes #314.

Problem

A generator exported from a shared library returned garbage (or the importer failed to compile) when used from a program built with -shared. Generator methods on exported classes (*items()) failed the same way. Static multi-file builds were not affected because they don't go through the embedded declaration text (__decls).

Cause

Two bugs in the declaration text a library embeds for importers:

  1. Boxed object printed as a value. A generator returns its object by reference (ObjectType over a tuple), but it was printed as a plain {...}, which the importer reads back as a value tuple. Library and importer disagreed on the layout.
  2. Internal field name printed unquoted. The generator object's .step field was printed as .step:s32, which is a syntax error. The importer dropped the whole declaration, and compilation then failed in for...of with "Element access with a non-constant index".

Fix

  • New built-in generic type BoxedObject<T> (MLIRGenTypes.cpp) that resolves to ObjectType over T. The declaration printer now writes objects held by reference as BoxedObject<{...}>.
    • Named BoxedObject rather than Boxed: user generic types resolve before built-in ones, and an existing test already declares class Boxed.
  • Field names that aren't identifiers are printed quoted (".step": s32), which parses back to the exact name.
  • Both are opt-in flags on MLIRPrinter (printBoxedObjectTypes, quoteNonIdentifierFieldNames), set only by MLIRDeclarationPrinter, so type printing in diagnostics is unchanged.
  • A top-level @boxed variable still prints its bare storage shape; @boxed already adds the extra dereference, so BoxedObject<> there would dereference twice.

Declaration text before and after:

function functionGenerator() : {.step:s32, next(): {value:s32, done:boolean}};
function functionGenerator() : BoxedObject<{".step":s32, next(): {value:s32, done:boolean}}>;

Tests

  • export_generators.ts / import_generators.ts: function*, const generator, class generator method, namespaced generator, direct next() calls. Registered as static, -shared and -jit -shared tests; all pass.
  • Unit tests in unittests/MLIRGen/DeclarationPrinter.cpp: boxed return type, bare shape for @boxed variables, quoted non-identifier field names.
  • Full release suite (Windows): 2716/2716 passed.

🤖 Generated with Claude Code

@ASDAlexander77
ASDAlexander77 merged commit ab34557 into main Sep 13, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-exported-generator-shared branch September 13, 2026 17:15
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.

Exported generator returns wrong values under -shared: __decls prints the boxed generator object as a value tuple

1 participant