Fix JsonSourceGenerator crash with predefined type aliases#126944
Closed
Fix JsonSourceGenerator crash with predefined type aliases#126944
Conversation
Contributor
|
Tagging subscribers to this area: @dotnet/area-meta |
… int) In CSharpSyntaxHelper.AddAliases, store usingDirective.Name in a local variable and use the null-conditional operator when calling GetUnqualifiedName. This prevents the "Unreachable" exception when processing C# 12+ type aliases that target predefined types (e.g., using A = int;), where Name returns null. Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/2c604c37-485d-4953-8e7d-8edbf70f4d1f Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix JsonSourceGenerator unreachable error for predefined types
Fix JsonSourceGenerator crash with predefined type aliases
Apr 15, 2026
| // With C# 12+, aliases can target predefined/built-in types (e.g. `using A = int;`), | ||
| // in which case Name will be null. Skip those. | ||
| var name = usingDirective.Name; | ||
| if (name?.GetUnqualifiedName() is not SimpleNameSyntax simpleName) |
Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/16c1618e-a229-4042-9b1e-f0ff206a4903 Co-authored-by: eiriktsarpalis <2813363+eiriktsarpalis@users.noreply.github.com>
This was referenced Apr 15, 2026
Open
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.
Description
JsonSourceGeneratorthrowsInvalidOperationException("Unreachable")when a compilation unit contains a C# 12+ using alias targeting a predefined type (e.g.,using A = int;) alongside any attribute-annotated class.In
CSharpSyntaxHelper.AddAliases,usingDirective.Namereturnsnullfor predefined type aliases sincePredefinedTypeSyntaxis not aNameSyntax. The null value was then passed to theGetUnqualifiedNameextension method, which doesn't match anyNameSyntaxsubtype in its switch expression and hits the defaultthrow.Fix:
usingDirective.Namein a local and use?.GetUnqualifiedName()with pattern matching to safely skip null/non-NameSyntax valuesint,double,object,string,nuintThe fix applies to
CSharpSyntaxHelper.csinCommon/src/Roslyn/, which is shared by both theSystem.Text.JsonandMicrosoft.Extensions.LoggingRoslyn 4.0 source generators.