Skip to content

Comments

ILLink.RoslynAnalyzer: fix hole for new constraint#119291

Merged
sbomer merged 4 commits intodotnet:mainfrom
sbomer:newConstraintHole
Sep 15, 2025
Merged

ILLink.RoslynAnalyzer: fix hole for new constraint#119291
sbomer merged 4 commits intodotnet:mainfrom
sbomer:newConstraintHole

Conversation

@sbomer
Copy link
Member

@sbomer sbomer commented Sep 2, 2025

The new constraint processing was happening in a separate code path from the other generic argument processing, and that code path didn't see the implicit call to the base ctor (because it didn't use the IOperation tree). This fixes it by moving the new constraint processing into GenericArgumentDataFlow.

Unlike ILC, the analyzer can't just treat new() the same as PublicParameterlessConstructors, because it needs to produce analysis warnings for RequiresDynamicCode or RequiresAssemblyFiles accessed via new() constraint, even when trim analysis is disabled (the annotation is treated as reflection access, but new() is not).

Fixes #118869

@sbomer sbomer requested review from a team and Copilot September 2, 2025 23:35
@sbomer sbomer requested a review from marek-safar as a code owner September 2, 2025 23:35
@github-actions github-actions bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Sep 2, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes a hole in the ILLink.RoslynAnalyzer's handling of generic constraints. The analyzer was not properly detecting required capability violations (like RequiresDynamicCode or RequiresAssemblyFiles) when accessed through new() constraints on generic type parameters. The fix moves the new constraint processing from a separate code path into the existing GenericArgumentDataFlow class, ensuring that implicit constructor calls are properly analyzed.

Key changes:

  • Consolidates generic constraint processing into GenericArgumentDataFlow
  • Adds proper detection of new() constraint violations for required capabilities
  • Updates test cases to verify the fix works correctly

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
RequiresOnClass.cs Adds test cases for new constraint processing and updates expected warnings
TrimAnalysisVisitor.cs Updates method signatures to pass DataFlowAnalyzerContext
TrimAnalysisMethodCallPattern.cs Updates HandleCall invocation with new parameters
TrimAnalysisGenericInstantiationPattern.cs Refactors to use GenericArgumentDataFlow instance instead of static methods
TrimAnalysisAssignmentPattern.cs Updates RequireDynamicallyAccessedMembersAction constructor call
RequireDynamicallyAccessedMembersAction.cs Adds DataFlowAnalyzerContext and FeatureContext parameters
HandleCallAction.cs Updates constructor to accept and pass through new context parameters
GenericArgumentDataFlow.cs Major refactor from static to instance class with new constraint processing
RequiresAnalyzerBase.cs Removes old generic name syntax processing logic
DynamicallyAccessedMembersAnalyzer.cs Updates to use new GenericArgumentDataFlow instance

@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Sep 2, 2025
Copy link
Member

@jtschuster jtschuster left a comment

Choose a reason for hiding this comment

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

Sorry I got to this so late!

@sbomer
Copy link
Member Author

sbomer commented Sep 15, 2025

No problem, thanks for the review!

@sbomer sbomer merged commit db95b15 into dotnet:main Sep 15, 2025
78 checks passed
@github-actions github-actions bot locked and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILLink .NET linker development as well as trimming analyzers linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trim analyzer hole for new constraint in constructor

2 participants