Skip to content

Add unsafe evolution migration code fixers#130611

Open
EgorBo wants to merge 5 commits into
dotnet:mainfrom
EgorBo:unsafe-migrator-3
Open

Add unsafe evolution migration code fixers#130611
EgorBo wants to merge 5 commits into
dotnet:mainfrom
EgorBo:unsafe-migrator-3

Conversation

@EgorBo

@EgorBo EgorBo commented Jul 13, 2026

Copy link
Copy Markdown
Member

Spec: https://github.com/dotnet/csharplang/blob/main/proposals/unsafe-evolution.md

What it does:

  • Adds IL5005 to remove legacy or invalid unsafe modifiers.
    • Type declarations such as classes, structs, interfaces, records, record structs, and enums.
    • Delegate declarations, static constructors, and destructors.
    • Methods, instance constructors, operators, conversion operators, local functions, properties, indexers, and events when:
      • no pointer or function pointer in the signature (we keep those caller-unsafe)
      • no <safety> comment. It's a sign that this unsafe was from unsafe-v1.
    • Keeps unsafe when required by interop, partial members, overrides, or interface contracts.
  • Adds IL5006 to add required unsafe blocks, expressions, and contract modifiers.
  • Adds unsafe and <safety> placeholders to unannotated interop declarations.
  • Covers partial members, interfaces, accessors, stackalloc, async code, top-level statements, and scoped refs.

How to run it from the repo root:

C:\prj\dotnet-latest\dotnet.cmd build .\src\tools\illink\src\ILLink.CodeFix\ILLink.CodeFixProvider.csproj -c Debug --no-restore

$env:EnableUnsafeMigration = 'true'

C:\prj\dotnet-latest\dotnet.cmd format analyzers .\src\libraries\System.Text.Json\src\System.Text.Json.csproj -f net11.0 --diagnostics IL5005 --severity info --no-restore
C:\prj\dotnet-latest\dotnet.cmd format analyzers .\src\libraries\System.Text.Json\src\System.Text.Json.csproj -f net11.0 --diagnostics IL5006 --severity info --no-restore
C:\prj\dotnet-latest\dotnet.cmd format analyzers .\src\libraries\System.Text.Json\src\System.Text.Json.csproj -f net11.0 --diagnostics IL5006 --severity info --no-restore

C:\prj\dotnet-latest\dotnet.cmd build .\src\libraries\System.Text.Json\src\System.Text.Json.csproj -f net11.0 -c Debug --no-restore -p:BuildProjectReferences=false -p:EnableUnsafeMigration=true

I was using the latest daily SDK in C:\prj\dotnet-latest, not sure the bootstrapped one contains all the new C# features.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Copilot AI review requested due to automatic review settings July 13, 2026 09:23
@EgorBo EgorBo requested a review from sbomer as a code owner July 13, 2026 09:23
@github-actions github-actions Bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Jul 13, 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

This PR adds a DEBUG-only “unsafe evolution” migration analyzer + code fixers to ILLink, introducing two new IL* diagnostics (IL5005/IL5006) to (1) remove legacy/invalid unsafe modifiers and (2) migrate unsafe usages by adding explicit unsafe contexts and safety placeholders. It also wires up additional MSBuild properties and live-ILLink behavior to enable the migration experience, and adds/extends Roslyn analyzer/codefix tests.

Changes:

  • Add UnsafeMigrationAnalyzer + UnsafeMigrationAnalysis to detect modifier removals, required declaration updates, and unsafe-operation sites (behind #if DEBUG).
  • Add two new code fix providers (modifier removal + usage migration) and supporting document/AST rewrite helpers (behind #if DEBUG).
  • Add new diagnostic strings/IDs and update build/targets/tests to enable and validate the migration workflow.

Reviewed changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/RequiresUnsafeCodeFixTests.cs Adds test helpers and extensive test coverage for IL5005/IL5006 codefix behavior.
src/tools/illink/src/ILLink.Shared/SharedStrings.resx Adds user-facing title/message strings for the new unsafe migration diagnostics.
src/tools/illink/src/ILLink.Shared/DiagnosticId.cs Adds DEBUG-only diagnostic IDs and introduces an “Unsafe” category range mapping.
src/tools/illink/src/ILLink.Shared/DiagnosticCategory.cs Adds DEBUG-only Unsafe category constant.
src/tools/illink/src/ILLink.RoslynAnalyzer/UnsafeMigrationAnalyzer.cs New analyzer that reports file-level migration diagnostics when migration is enabled.
src/tools/illink/src/ILLink.RoslynAnalyzer/UnsafeMigrationAnalysis.cs New analysis engine for modifier removals, contract/declaration updates, and unsafe operation locations.
src/tools/illink/src/ILLink.RoslynAnalyzer/MSBuildPropertyOptionNames.cs Adds DEBUG-only MSBuild property names used to toggle unsafe migration behavior.
src/tools/illink/src/ILLink.RoslynAnalyzer/build/Microsoft.NET.ILLink.Analyzers.props Exposes additional compiler-visible MSBuild properties used by the migration flow.
src/tools/illink/src/ILLink.CodeFix/UnsafeUsageMigrationCodeFixProvider.cs New code fix provider that migrates unsafe usage sites (wrap blocks/expressions, etc.).
src/tools/illink/src/ILLink.CodeFix/UnsafeUsageDocumentFixer.cs New document fixer implementing the migration transformations and heuristics.
src/tools/illink/src/ILLink.CodeFix/UnsafeModifierMigrationCodeFixProvider.cs New code fix provider that removes legacy unsafe modifiers when eligible.
src/tools/illink/src/ILLink.CodeFix/UnsafeCodeFixHelpers.cs New shared helpers for adding unsafe contexts and safety documentation.
src/tools/illink/src/ILLink.CodeFix/Resources.resx Adds code fix titles for the new migration fixers.
eng/liveILLink.targets Updates live-ILLink wiring to support enabling unsafe migration and design-time analyzer referencing.

Comment thread src/tools/illink/src/ILLink.Shared/DiagnosticId.cs
Comment thread src/tools/illink/src/ILLink.CodeFix/UnsafeCodeFixHelpers.cs Outdated
Comment thread src/tools/illink/src/ILLink.CodeFix/UnsafeUsageDocumentFixer.cs
@dotnet-policy-service dotnet-policy-service Bot added the linkable-framework Issues associated with delivering a linker friendly framework label Jul 13, 2026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Copilot AI review requested due to automatic review settings July 13, 2026 09:36

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 14 out of 14 changed files in this pull request and generated 3 comments.

Comment thread src/tools/illink/src/ILLink.CodeFix/UnsafeCodeFixHelpers.cs
Comment thread src/tools/illink/src/ILLink.CodeFix/UnsafeUsageDocumentFixer.cs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Copilot AI review requested due to automatic review settings July 13, 2026 10:00

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 14 out of 14 changed files in this pull request and generated 3 comments.

Comment thread eng/liveILLink.targets
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Copilot AI review requested due to automatic review settings July 13, 2026 10:38

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 14 out of 14 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings July 13, 2026 18:12

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 14 out of 14 changed files in this pull request and generated 2 comments.

Comment on lines +69 to +83
private static SyntaxNode AddUnsafeModifier(
SyntaxNode declaration,
SyntaxGenerator generator)
=> declaration is AccessorDeclarationSyntax accessor
? accessor
.WithModifiers(accessor.Modifiers.Insert(
0,
SyntaxFactory.Token(SyntaxKind.UnsafeKeyword)
.WithTrailingTrivia(SyntaxFactory.Space)))
.WithKeyword(accessor.Keyword.WithLeadingTrivia(SyntaxFactory.TriviaList()))
.WithLeadingTrivia(accessor.GetLeadingTrivia())
: generator.WithModifiers(
declaration,
generator.GetModifiers(declaration).WithIsUnsafe(true))
.WithLeadingTrivia(declaration.GetLeadingTrivia());
Comment on lines +589 to +594
if (expression.DescendantNodesAndSelf().Any(static node => node is AwaitExpressionSyntax))
return false;

ITypeSymbol? type = semanticModel.GetTypeInfo(expression).Type;
return type?.SpecialType != SpecialType.System_Void;
}
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

None yet

Development

Successfully merging this pull request may close these issues.

2 participants