Add unsafe evolution migration code fixers#130611
Open
EgorBo wants to merge 5 commits into
Open
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Contributor
There was a problem hiding this comment.
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+UnsafeMigrationAnalysisto 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. |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: c3ccf7c6-8a32-4c31-9e31-12f0f1d2a026
This was referenced Jul 13, 2026
Open
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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Spec: https://github.com/dotnet/csharplang/blob/main/proposals/unsafe-evolution.md
What it does:
unsafemodifiers.<safety>comment. It's a sign that thisunsafewas from unsafe-v1.unsafewhen required by interop, partial members, overrides, or interface contracts.unsafeand<safety>placeholders to unannotated interop declarations.How to run it from the repo root:
I was using the latest daily SDK in
C:\prj\dotnet-latest, not sure the bootstrapped one contains all the new C# features.