Skip to content

Add RequiresUnsafe trimming analyzer capabilities - #123588

Merged
agocke merged 5 commits into
dotnet:mainfrom
agocke:unsafe-analzyer
Feb 15, 2026
Merged

agocke merged 5 commits into
dotnet:mainfrom
agocke:unsafe-analzyer

Conversation

@agocke

@agocke agocke commented Jan 25, 2026

Copy link
Copy Markdown
Member

This is a debug-only mode that we can use for annotating the framework. No intention of shipping this implementation.

Copilot AI review requested due to automatic review settings January 25, 2026 00:43
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Jan 25, 2026
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jan 25, 2026

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

Adds a debug-only “RequiresUnsafe” analyzer path (plus code fix + tests) to help annotate the framework, gated behind an MSBuild property and excluded from shipping builds.

Changes:

  • Extend the Roslyn analyzer test harness to optionally compile test inputs with allowUnsafe.
  • Add a debug-only RequiresUnsafeAnalyzer and a debug-only code fix provider.
  • Add diagnostics/resources for the new RequiresUnsafe warnings and introduce a new test suite for them (DEBUG-only).

Reviewed changes

Copilot reviewed 12 out of 12 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/Verifiers/CSharpAnalyzerVerifier`1.cs Adds an overload to pass allowUnsafe through the verifier into compilation creation.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs Adds allowUnsafe support to the Roslyn compilation options and refactors supported analyzer list creation.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeAnalyzerTests.cs New DEBUG-only tests validating RequiresUnsafe diagnostics and suppression behavior in unsafe { } blocks.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnreferencedCodeAnalyzerTests.cs Removes an unused diagnostic descriptor field.
src/tools/illink/src/ILLink.Shared/SharedStrings.resx Adds title/message resources for the new RequiresUnsafe diagnostics.
src/tools/illink/src/ILLink.Shared/DiagnosticId.cs Introduces DEBUG-only diagnostic IDs for RequiresUnsafe and adjusts AOT subcategory mapping.
src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresUnsafeAnalyzer.cs New DEBUG-only analyzer implementation, enabled via an MSBuild property, with special “unsafe block” suppression.
src/tools/illink/src/ILLink.RoslynAnalyzer/RequiresAnalyzerBase.cs Refactors scope suppression check to be overridable by derived analyzers.
src/tools/illink/src/ILLink.RoslynAnalyzer/MSBuildPropertyOptionNames.cs Adds a DEBUG-only MSBuild property name for enabling the new analyzer.
src/tools/illink/src/ILLink.RoslynAnalyzer/DynamicallyAccessedMembersAnalyzer.cs Includes RequiresUnsafeAnalyzer in the “requires analyzers” set in DEBUG builds.
src/tools/illink/src/ILLink.CodeFix/Resources.resx Adds a localized title for the RequiresUnsafe code fix.
src/tools/illink/src/ILLink.CodeFix/RequiresUnsafeCodeFixProvider.cs New DEBUG-only code fix provider: add RequiresUnsafe attribute and optionally wrap a statement in an unsafe block.

Comment thread src/tools/illink/src/ILLink.Shared/DiagnosticId.cs Outdated

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This needs some tests for the code fixer too I think.

Comment thread src/tools/illink/src/ILLink.Shared/DiagnosticId.cs Outdated
@agocke

agocke commented Jan 29, 2026

Copy link
Copy Markdown
Member Author

Moving to draft as I clean this up

Copilot AI review requested due to automatic review settings January 29, 2026 22:13

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

Copilot reviewed 20 out of 20 changed files in this pull request and generated 5 comments.

Comment thread src/tools/illink/src/ILLink.Shared/SharedStrings.resx Outdated
Comment thread src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml Outdated
Comment thread src/tools/illink/src/ILLink.CodeFix/RequiresUnsafeCodeFixProvider.cs Outdated
This is a debug-only mode that we can use for annotating the framework. No
intention of shipping this implementation.
Copilot AI review requested due to automatic review settings January 30, 2026 03:18

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

Copilot reviewed 19 out of 19 changed files in this pull request and generated 6 comments.

Comment thread src/tools/illink/src/ILLink.Shared/DiagnosticId.cs
@agocke
agocke marked this pull request as ready for review January 30, 2026 22:17
@agocke

agocke commented Jan 30, 2026

Copy link
Copy Markdown
Member Author

@sbomer OK, I think this is ready again. The code fixer code is kind of a mess, but it's not totally wrong, and it's working enough for local prototyping. We don't plan to ship it, so it seems fine.

Copilot AI review requested due to automatic review settings February 3, 2026 21:10

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

Copilot reviewed 19 out of 19 changed files in this pull request and generated 6 comments.

@sbomer sbomer left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A few nits, otherwise LGTM!

Comment thread src/tools/illink/src/ILLink.Shared/DiagnosticId.cs
Comment thread src/coreclr/System.Private.CoreLib/CompatibilitySuppressions.xml Outdated
@agocke
agocke merged commit a27de4a into dotnet:main Feb 15, 2026
172 checks passed
@github-project-automation github-project-automation Bot moved this to Done in AppModel Feb 15, 2026
@agocke
agocke deleted the unsafe-analzyer branch February 15, 2026 18:18
iremyux pushed a commit to iremyux/dotnet-runtime that referenced this pull request Mar 2, 2026
This is a debug-only mode that we can use for annotating the framework.
No intention of shipping this implementation.
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 18, 2026
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

Status: Done

Development

Successfully merging this pull request may close these issues.

5 participants