Skip to content

Preserve "notnull" and "allows ref struct" constraints in GenAPI#54455

Merged
ViktorHofer merged 3 commits into
mainfrom
fix-genapi-notnull-constraints
May 27, 2026
Merged

Preserve "notnull" and "allows ref struct" constraints in GenAPI#54455
ViktorHofer merged 3 commits into
mainfrom
fix-genapi-notnull-constraints

Conversation

@ViktorHofer

@ViktorHofer ViktorHofer commented May 27, 2026

Copy link
Copy Markdown
Member

Fixes #54450.
Fixes #47113.

Summary

  • Preserve notnull generic constraints when GenAPI generates C# declarations from metadata.
  • Preserve allows ref struct generic constraints and keep them last in generated constraint lists.
  • Cover generic types, methods, delegates, and existing struct dummy-field baselines.
  • Avoid emitting invalid inherited constraints on overrides and explicit interface implementations.

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*"

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>
Copilot AI review requested due to automatic review settings May 27, 2026 07:17
@ViktorHofer ViktorHofer requested a review from a team as a code owner May 27, 2026 07:17
@ViktorHofer ViktorHofer requested a review from MichaelSimons May 27, 2026 07:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 : notnull constraint clauses when ITypeParameterSymbol.HasNotNullConstraint is present (while suppressing emission for overrides and explicit interface implementations).
  • Adds/updates unit tests and baselines to validate notnull constraint 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>
@ViktorHofer ViktorHofer changed the title Preserve notnull constraints in GenAPI Preserve notnull and "allows ref struct" constraints in GenAPI May 27, 2026
@ViktorHofer ViktorHofer changed the title Preserve notnull and "allows ref struct" constraints in GenAPI Preserve "notnull" and "allows ref struct" constraints in GenAPI May 27, 2026
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>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot's findings

  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new

@ViktorHofer ViktorHofer merged commit 4705b82 into main May 27, 2026
26 checks passed
@ViktorHofer ViktorHofer deleted the fix-genapi-notnull-constraints branch May 27, 2026 13:47
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 11.0-preview6 milestone May 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GenAPI omits notnull generic constraints from generated reference source GenAPI: Does not preserve allows ref struct "unconstraint"

3 participants