Detect closure structs from local functions and throw helpful TranspileException#231
Merged
jonathanpeppers merged 4 commits intomainfrom Mar 16, 2026
Merged
Conversation
…leException When local functions capture outer byte[] variables, the C# compiler generates closure structs (display classes). The transpiler now detects these early in DetectStructLayouts() and throws a TranspileException with clear guidance: - Explains what closures are and why they happen - Suggests passing captured variables as parameters instead - Suggests inlining the local function code Also improves the fallback error in ResolveStructType() and the Newobj message to mention closures as a possible cause. Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
…spileException> Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix transpiler issue with closure structs from local functions
Detect closure structs from local functions and throw helpful TranspileException
Mar 15, 2026
jonathanpeppers
approved these changes
Mar 15, 2026
Owner
jonathanpeppers
left a comment
There was a problem hiding this comment.
🤖 Looks good! Clean, focused change with solid coverage.
What I reviewed:
- Closure detection in
DetectStructLayouts()correctly catches struct-based display classes before they'd be silently skipped. TheDisplayClassname check matches Roslyn's naming convention. - Class-based closures would still hit the updated
Newobjerror message path — good defense in depth. ResolveStructType()fallback changed fromInvalidOperationExceptiontoTranspileException— consistent error type and provides closure-specific guidance.- Test
ClosureCapturingByteArrayThrowsexercises the exact scenario from issue #226. - All 499 tests pass locally (64 analyzer + 435 transpiler).
- CI is green.
Ready for human review.
Contributor
There was a problem hiding this comment.
Pull request overview
Improves transpiler diagnostics for C# local-function closures (compiler-generated display-class structs) by failing fast with actionable TranspileException messages, instead of later/unclear failures.
Changes:
- Detect
DisplayClassvalue types duringDetectStructLayouts()and throw a targetedTranspileExceptionlisting captured fields + workaround guidance. - Update
LocalVariableManager.ResolveStructType()to throwTranspileExceptionwith closure-related guidance instead ofInvalidOperationException. - Expand the unsupported
newobjguidance text to mention closure generation; add an end-to-end Roslyn test for the closure path.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/dotnes.tasks/Utilities/Transpiler.cs | Adds early detection of compiler-generated closure structs (DisplayClass) and throws a more actionable error. |
| src/dotnes.tasks/Utilities/LocalVariableManager.cs | Replaces a generic InvalidOperationException with a closure-aware TranspileException. |
| src/dotnes.tasks/Utilities/IL2NESWriter.Helpers.cs | Improves newobj unsupported-opcode message to mention closures as a common cause. |
| src/dotnes.tests/RoslynTests.cs | Adds an end-to-end test validating closure capture produces a TranspileException. |
| src/dotnes.tests/LocalVariableManagerTests.cs | Updates test to expect TranspileException from ResolveStructType() failure path. |
Assert that the error message includes the captured variable name ('palette')
and workaround guidance, not just the word 'closure'.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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.
When non-static local functions capture outer
byte[]variables, the C# compiler generates closure structs (display classes). The transpiler skipped these inDetectStructLayouts()as compiler-generated types, then failed later with a confusingInvalidOperationException: Cannot resolve struct type for local 0 with field 'palette'.Changes
Transpiler.cs— Detect closure structs early inDetectStructLayouts()by checking forDisplayClassin type names. ThrowsTranspileExceptionnaming the captured variables and suggesting workarounds.LocalVariableManager.cs—ResolveStructType()fallback now throwsTranspileExceptionmentioning closures as a possible cause instead of a genericInvalidOperationException.IL2NESWriter.Helpers.cs—Newobjerror message updated to mention closures (covers class-based display classes).ClosureCapturingByteArrayThrowsvalidates the error path end-to-end.Example error
Triggered by:
Fix: make it
staticand pass as parameter:Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.