Fix anonymous-type lambda early-return emitting unresolvable cast#3752
Open
jakobhellermann wants to merge 2 commits into
Open
Fix anonymous-type lambda early-return emitting unresolvable cast#3752jakobhellermann wants to merge 2 commits into
jakobhellermann wants to merge 2 commits into
Conversation
When a lambda's inferred return type contains an anonymous type and one branch returns null, the decompiler emitted an explicit cast such as `return (IEnumerable<<>f__AnonymousType0<int>>)null;`, which is invalid C#. Skip the cast in IsPossibleLossOfTypeInformation for null literals whenever the expected type contains an anonymous type: null is implicitly convertible to any reference type, so no cast is needed, and the anonymous type has no nameable form to cast to anyway. Fixes icsharpcode#3751
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes a decompiler edge case where lambdas/anonymous methods with an inferred return type involving an anonymous type could emit an explicit cast on null (e.g., return (<>f__AnonymousType0<int>)null;), which is not valid C# syntax. The change prevents emitting such casts when the expected type contains an anonymous type, and adds a regression test for the reported scenario.
Changes:
- Adjusted
StatementBuilder.IsPossibleLossOfTypeInformation()to not treatnullas requiring an explicit cast when the expected type contains an anonymous type. - Added a new pretty test case reproducing Issue #3751.
- Registered the new Issue3751 test in
PrettyTestRunner.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| ICSharpCode.Decompiler/CSharp/StatementBuilder.cs | Skips explicit casts for null returns when the expected type contains an anonymous type, preventing unnameable cast emission. |
| ICSharpCode.Decompiler.Tests/TestCases/Pretty/Issue3751.cs | Adds a regression test source that would previously decompile to an invalid cast to an anonymous type. |
| ICSharpCode.Decompiler.Tests/PrettyTestRunner.cs | Registers the new Issue3751 pretty test to run across the default compiler option matrix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Author
|
CI seems to have failed during |
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.
Fixes #3751
Problem
When a lambda's inferred return type contains an anonymous type and one branch returns null, the decompiler emitted an explicit cast such as
return (IEnumerable<<>f__AnonymousType0<int>>)null;, which is invalid C#.Solution
Skip the cast in IsPossibleLossOfTypeInformation for null literals whenever the expected type contains an anonymous type:
null is implicitly convertible to any reference type, so no cast is needed, and the anonymous type has no nameable form to cast to anyway.