Skip to content

Add RequiresUnsafe trimming analyzer capabilities#123588

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

Add RequiresUnsafe trimming analyzer capabilities#123588
agocke merged 5 commits intodotnet:mainfrom
agocke:unsafe-analzyer

Conversation

@agocke
Copy link
Member

@agocke agocke commented Jan 25, 2026

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

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.

Copy link
Member

@sbomer sbomer left a comment

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.

@agocke
Copy link
Member Author

agocke commented Jan 29, 2026

Moving to draft as I clean this up

Copilot AI review requested due to automatic review settings January 29, 2026 22:13
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

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

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

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

@agocke agocke marked this pull request as ready for review January 30, 2026 22:17
@agocke
Copy link
Member Author

agocke commented Jan 30, 2026

@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
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

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

Copy link
Member

@sbomer sbomer left a comment

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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.

4 participants