Compiled write-captured block-scope shadow shares name-keyed $functionDC (residual of #767/#674)
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 outerrcollide on the name-keyed$functionDCfield)
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
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from nickna/SharpTS
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 65/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 57/100
Maintainers usually reply within 1 day
Similar issues
-
elsa 3
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
elsa-workflows/elsa-core#9349 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 64/100
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
PereViader/ManualDi#200 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Forrest-Technologies/drover#1040 ·
Maintainers usually reply within 1 day
-
todo
Difficulty 1/5 Under an hour Newbie friendliness 72/100
tonybaloney/CSnakes#864 ·