Skip to content

Reapply "Implement type name resolution for ILLink analyzer (#106209)" (#106343) - #116505

Merged
sbomer merged 9 commits into
dotnet:mainfrom
sbomer:analyzerTypeNameParserRedo
Aug 6, 2025
Merged

Reapply "Implement type name resolution for ILLink analyzer (#106209)" (#106343)#116505
sbomer merged 9 commits into
dotnet:mainfrom
sbomer:analyzerTypeNameParserRedo

Conversation

@sbomer

Copy link
Copy Markdown
Member

Fixes#95118

Reapplies #106209 which was reverted due to #106321. Now that roslyn has updated to reference System.Reflection.Metadata 9.0, we can use TypeName. However, the code was using
TypeNameHelpers.Unescape which was moved to TypeName.Unescape in .NET 10, so
we need to preserve a copy of the Unescape logic for use by the analyzer.

sbomer added 4 commits June 10, 2025 22:14
- Remove TypeNameHelpers
- Pass resolver through new code
The referenced issue has since been fixed
The analyzer shouldn't use TypeName.Unescape which was
added in .NET 10 (since Roslyn references Sytem.Reflection.Metadata
9.0.0).
@sbomer
sbomer requested review from a team and CopilotJune 10, 2025 22:33
@sbomer
sbomer requested a review from marek-safar as a code ownerJune 10, 2025 22:33
@github-actionsgithub-actionsBot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Jun 10, 2025
@dotnet-policy-servicedotnet-policy-serviceBot added the linkable-framework Issues associated with delivering a linker friendly framework label Jun 10, 2025

CopilotAI 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 re-applies and updates changes from a previous fix for type name resolution in the ILLink analyzer. Key changes include the removal of tool-specific parameters from ExpectedWarning attributes in tests, propagation of a new TypeNameResolver parameter throughout the analyzer code, and updates to project configuration for new dependencies.

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated no comments.

Show a summary per file
FileDescription
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.csRemoved tool parameters from the ExpectedWarning attribute to simplify warning checks.
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.csSimilar removal of extra parameters from ExpectedWarning attributes.
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.csRemoved extra warning parameters from ExpectedWarning, updating test expectations.
src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AssemblyQualifiedNameDataflow.csUpdated ExpectedWarning attribute message by removing extra parameters.
src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.csChanged the assemblyName from a generated GUID to a static "test" string.
(Multiple analyzer files)Introduced a new TypeNameResolver parameter and propagated it for improved type resolution logic.
src/tools/illink/src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csprojAdded AllowUnsafeBlocks and a new package reference for System.Reflection.Metadata.
Comments suppressed due to low confidence (7)

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributePropertyDataflow.cs:58

  • Removed tool-specific parameters from the ExpectedWarning attribute. Ensure that the revised warning expectation matches the analyzer's output in all scenarios.
[ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethodsKeptByName--")]

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributeFieldDataflow.cs:43

  • Removed extra parameters from the ExpectedWarning attribute to simplify warning checks. Confirm that this change aligns with the intended analyzer behavior.
[ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethods--")]

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AttributeConstructorDataflow.cs:28

  • Simplified the ExpectedWarning attribute by removing redundant tool information. Verify that the analyzer still triggers the expected warning.
[ExpectedWarning ("IL2026", "--ClassWithKeptPublicMethods--")]

src/tools/illink/test/Mono.Linker.Tests.Cases/DataFlow/AssemblyQualifiedNameDataflow.cs:61

  • By removing extra diagnostic parameters, ensure that the test still properly verifies the need for assembly-qualified type names.
[ExpectedWarning ("IL2122", "Type 'System.Invalid.TypeName' is not assembly qualified. Type name strings used for dynamically accessing a type should be assembly qualified.")]

src/tools/illink/test/ILLink.RoslynAnalyzer.Tests/TestCaseCompilation.cs:47

  • Changed assemblyName from a generated GUID to a static value. Confirm that this change does not disrupt tests that may rely on unique assembly identifiers.
assemblyName: "test",

src/tools/illink/src/ILLink.RoslynAnalyzer/TrimAnalysis/RequireDynamicallyAccessedMembersAction.cs:40

  • Ensure that the type resolution logic integrates correctly with the new TypeNameResolver parameter and that generic argument processing remains robust.
if (_reflectionAccessAnalyzer.TryResolveTypeNameAndMark (typeName, diagnosticContext, needsAssemblyName, out ITypeSymbol? foundType)) {

src/tools/illink/src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj:33

  • New project settings include AllowUnsafeBlocks and a package reference for System.Reflection.Metadata. Verify that these changes are documented appropriately and that all required dependencies are satisfied.
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />

@jtschuster

Copy link
Copy Markdown
Member

SystemReflectionMetadataVersion is 10.0.0-preview.6 now, does that affect whether this will work?
https://github.com/sbomer/runtime/blob/4f1aebea81794329a365e8c5f202e6cd510833c9/eng/Versions.props#L131

The repo builds for me with a global install, but not sure if this would still cause issues in VS.

Comment threadsrc/tools/illink/src/ILLink.RoslynAnalyzer/ILLink.RoslynAnalyzer.csproj Outdated

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

LGTM

@agocke

Copy link
Copy Markdown
Member

Any more testing we need for this?

@agocke

Copy link
Copy Markdown
Member

@sbomer do you want to resolve the merge conflicts?

@sbomer

Copy link
Copy Markdown
MemberAuthor

Will do - I need to revisit the questions about the SRM version and ideally check that this doesn't hit an issue similar to #106321. I had some trouble reproing that last time and want to make sure the issue is actually fixed.

@sbomer

Copy link
Copy Markdown
MemberAuthor

Looked into the SRM version - our SystemReflectionMetadataToolsetVersion is still on 8.0.0 which is too old. For that to move forward, I think sdk needs to move forward first: https://github.com/dotnet/sdk/blob/a23f97a248669048845d7648e552c7417e3a9ea0/eng/Versions.props#L141.

But I believe that version is used for MSBuild tasks, not roslyn. Roslyn is already on 9.0.0. However, we don't have a LatestVS version for SRM in runtime - I think that would need to be introduced (or we'd need to unify on 9.0.0 for SystemReflectionMetadataToolsetVersion).

@jkoritzinsky and @AaronRobinsonMSFT recommended just including the TypeName parser via source, so that's what I've done.

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

Thank you!

@sbomer
sbomer merged commit fdf73d8 into dotnet:mainAug 6, 2025
94 of 97 checks passed
@sbomer
sbomer deleted the analyzerTypeNameParserRedo branch August 6, 2025 20:28
@github-actionsgithub-actionsBot locked and limited conversation to collaborators Sep 6, 2025
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-Tools-ILLink.NET linker development as well as trimming analyzerslinkable-frameworkIssues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trim analyzer: Implement type name parsing

4 participants

@sbomer@jtschuster@agocke