Skip to content

C#: Fix parsing of nullable type constraints#7077

Merged
knutwannheden merged 1 commit intomainfrom
deft-chinchilla
Mar 20, 2026
Merged

C#: Fix parsing of nullable type constraints#7077
knutwannheden merged 1 commit intomainfrom
deft-chinchilla

Conversation

@knutwannheden
Copy link
Contributor

Summary

  • Fix NullReferenceException when parsing nullable type constraints like where T : IComparable<T>?
  • ParseConstraintExpression used Visit() (Roslyn's visitor dispatch) which returned null for NullableTypeSyntax since there's no dedicated override — switched to VisitType() which handles it
  • Discovered via build failure on ardalis/GuardClauses where NullOrOutOfRangeInternal<T> has where T : IComparable<T>?

Test plan

  • Added 3 xUnit parser round-trip tests for nullable type constraints (class-level, method-level, combined with struct)
  • All 130 ClassDeclarationTests pass
  • All 21 constraint-specific tests pass

`ParseConstraintExpression` used `Visit()` (Roslyn's visitor dispatch) to
parse type constraints, but `NullableTypeSyntax` has no dedicated visitor
override — only `VisitType()` handles it. This caused `Visit()` to return
null, which propagated into the `JContainer<Expression>` and triggered a
`NullReferenceException` during RPC serialization.
@github-project-automation github-project-automation bot moved this to In Progress in OpenRewrite Mar 20, 2026
@knutwannheden knutwannheden merged commit 616e8c8 into main Mar 20, 2026
1 check passed
@knutwannheden knutwannheden deleted the deft-chinchilla branch March 20, 2026 15:11
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Mar 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant