fix(compiler): regex/super/dynamic-import in async & generator bodies (#1105)#1174
Merged
Conversation
…#1105) The four state-machine emitters derive from StatementEmitterBase, not ILEmitter, so a regex literal compiled to `null` in any async/generator body (the base EmitRegexLiteral pushed Ldnull), and the generator emitters additionally overrode EmitSuper/EmitDynamicImport to push `null`. Result: `/re/`, `super.x`, and `import()` inside an `async function`/`function*` silently evaluated to null at runtime — wrong result, no error. - Port the hoist-aware $RegExp emission from ILEmitter into the shared ExpressionEmitterBase (the per-site hoist fields already hang off the shared EmittedRuntime, so it needs no sync-only state). Regex now compiles identically in functions, arrows, and all four state machines. Drop the now-redundant ILEmitter override. - Remove the silent-null EmitSuper overrides in GeneratorMoveNextEmitter and AsyncGeneratorMoveNextEmitter; the base default (hoisted `this` -> GetSuperMethod) works in the state machine. `super.x` now resolves in generators and async generators, matching async functions. - Remove the silent-null EmitDynamicImport override in GeneratorMoveNextEmitter; the base default routes through the module registry, behaving identically to async functions (the remaining "module not pre-compiled" bundling limitation is orthogonal and not state-machine-specific). - Update EmitterSyncTests allowlist (the removed overrides) and add 8 differential-parity snippets pinning regex/super across the four bodies. Suite 14519/0, EmitterSyncTests + DifferentialParityTests green; verified interp==compiled for regex/super in every state-machine body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #1105.
Problem (reproduced firsthand)
A regex literal,
super.x, orimport()inside anasync function/function*body silently compiled tonullin compiled mode — wrong result at runtime, no error:Interpreted:
true / true. Compiled (before):false / false.Root cause
The four state-machine emitters (async / async-arrow / generator / async-generator) derive from
StatementEmitterBase, notILEmitter, so they inherited a baseEmitRegexLiteralthat pushedLdnull. The generator emitters additionally overrodeEmitSuper/EmitDynamicImportto pushnull. The real implementations existed only inILEmitter. The "base default pushes null, override only in ILEmitter" pattern guaranteed every such expression kind silently miscompiled in a state-machine body.Fix
$RegExpemission fromILEmitterinto the sharedExpressionEmitterBase.EmitRegexLiteral. The per-site hoist fields already hang off the sharedEmittedRuntime, so it needs no sync-only state. Removed the now-redundantILEmitteroverride. Regex now compiles identically in functions, arrows, and all four state machines.nullEmitSuperoverrides inGeneratorMoveNextEmitterandAsyncGeneratorMoveNextEmitter. The base default (load hoistedthis→GetSuperMethod, which resolves via the instance's runtime base type) works in the state machine. Verifiedsuper.xin generators and async generators now matches interpreted output and the existing async-function behavior.nullEmitDynamicImportoverride inGeneratorMoveNextEmitter. The base default routes through the module registry, behaving identically to async functions. (The remaining "module not pre-compiled" error is an orthogonal compiled-mode bundling limitation that affects async functions the same way — not state-machine-specific, so no super/import follow-up is needed.)EmitterSyncTestsallowlist (removed the three stale override entries) and added 8DifferentialParityTestssnippets pinning regex/super across the four bodies (each snippet runs through both interpreter and compiler and asserts identical output).Note: contrary to the issue's "make the base virtuals throw a
CompileException+ file follow-ups" suggestion,superactually works through the base default in state machines — so making them work beat failing loud, and no feature-gap follow-up is required.Verification
EmitterSyncTests(2) +DifferentialParityTests(91, incl. 8 new) green.interp == compiledfor regex/super in every state-machine body.CompiledBaselinetest host-crashes here (Internal CLR error0x80131506) under both parallel andSHARPTS_TEST262_WORKERS=1— confirmed pre-existing by stashing to cleanmain(same crash), so unrelated to this change. The committed subset'sRegExpfolder is top-level regex = byte-identical IL.