Compiled write-captured block-scope shadow shares name-keyed $functionDC (residual of #767/#674)

Open
#838 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

A pull request for this has already been merged.

  • #849 by @nickna — merged

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp, typescript
Domain
compilers

Research direction

Read Compilation/ClosureAnalyzer.cs and Compilation/LocalVariableResolver.cs alongside the function and suspension emitters listed in the issue. Trace how the inner shadow is renamed for body access and how the arrow resolves its captured r; compare compiled output with the supplied generator repro. Done means compiled mode yields 6,6,100 and the fix covers the listed async and generator contexts.

Written by the indexing model from the issue text.

Description

Summary

Residual left open by #767. A nested-block let/const that shadows an enclosing binding and is written by a nested closure leaks in compiled mode in all suspension contexts (generator #674, async-gen #725, async function). Read-only captures are fixed by #767 for (async) generators; the write case is off-limits everywhere.

Repro

function* mut(): Generator<number> {
  const r = 100;
  {
    let r = 5;
    const f = () => { r = r + 1; return r; };
    yield f();
    yield r;
  }
  yield r;
}
console.log([...mut()].join(","));
  • Interpreted: 6,6,100 (correct)
  • Compiled: 6,6,6 (wrong — inner and outer r collide on the name-keyed $functionDC field)

Root cause

A captured-AND-mutated local is lifted into the name-keyed $functionDC (#674/#725) so the closure's write and the body share storage. The #711/#767 renamer gives the inner shadow its own storage (<r>__bs0) and routes the body's inner-r access there, but the DC field stays keyed r, so the closure read/write and the outer binding collide. Renaming only the body side would desync the closure, so write-captured shadows are kept off-limits to renaming in every mode.

Suggested fix (Follow-up B)

Make the function DC rename-aware / per-binding: register the DC field under the renamed storage name (<r>__bs0), key CapturedFunctionLocals / FunctionDisplayClassFields by it (body side already retokens via #711), and update the arrow's capture resolution so its capture name r maps to functionDC.<r>__bs0 — the "program-wide ClosureAnalyzer rename awareness" the #767 deferral named. Spans DC registration + body emit + arrow resolver + ClosureAnalyzer/LocalVariableResolver.

Files: Compilation/ILCompiler.Functions.cs, Compilation/ILCompiler.Generators.cs, Compilation/ILCompiler.AsyncGenerators.cs, Compilation/ILCompiler.Async.cs, Compilation/ClosureAnalyzer.cs, Compilation/LocalVariableResolver.cs.

Priority

Low / astronomically rare, but highest blast radius of the closure-residual fixes (edits the capture hub #674/#625/#682/#725 depend on). No TypeScript conformance impact (TSConformance type-checker only; Test262 interpreter already correct). Subsumes #837. Found while implementing #767.

Dominant language
C#
Stars
156
Forks
4
Avg merge
14h 45m
Merged PRs (30d)
40

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from nickna/SharpTS

All issues in nickna/SharpTS

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.