Preserve "notnull" and "allows ref struct" constraints in GenAPI#54455
Merged
Conversation
Fix GenAPI reference source generation so generic type, method, and delegate declarations preserve notnull constraints from metadata. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Updates GenAPI’s C# syntax generation so notnull generic constraints are preserved when emitting declarations from metadata, addressing missing constraints that can cause downstream API-compat deltas.
Changes:
- Augments generated type/method/delegate declarations to add
where T : notnullconstraint clauses whenITypeParameterSymbol.HasNotNullConstraintis present (while suppressing emission for overrides and explicit interface implementations). - Adds/updates unit tests and baselines to validate
notnullconstraint preservation across types, methods, delegates, and synthesized struct dummy-field scenarios.
Show a summary per file
| File | Description |
|---|---|
| test/Microsoft.DotNet.GenAPI.Tests/CSharpFileBuilderTests.cs | Adds a focused notnull constraint generation test and updates struct baseline expectations to include where T : notnull. |
| src/Compatibility/GenAPI/Microsoft.DotNet.GenAPI/SyntaxGeneratorExtensions.cs | Implements post-processing to inject notnull constraint clauses into generated syntax nodes, with special-casing to avoid invalid emission on overrides/explicit impls. |
Copilot's findings
- Files reviewed: 2/2 changed files
- Comments generated: 2
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Preserve generated constraint clause ordering when adding missing metadata-only constraints, and add regression coverage for explicit interface implementations. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced May 27, 2026
MichaelSimons
approved these changes
May 27, 2026
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 #54450.
Fixes #47113.
Summary
notnullgeneric constraints when GenAPI generates C# declarations from metadata.allows ref structgeneric constraints and keep them last in generated constraint lists.Testing
.\.dotnet\dotnet.exe build test\Microsoft.DotNet.GenAPI.Tests\Microsoft.DotNet.GenAPI.Tests.csproj -p:SdkTargetFramework=net10.0 --no-restore.\.dotnet\dotnet.exe exec artifacts\bin\Microsoft.DotNet.GenAPI.Tests\Debug\net10.0\Microsoft.DotNet.GenAPI.Tests.dll -method "*TestAllowsRefStructGenericConstraintGeneration*" -method "*TestNotNullGenericConstraintGeneration*" -method "*TestSynthesizePrivateFieldsForNestedGenericTypes*" -method "*TestSynthesizePrivateFieldsAngleBrackets*"