Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement type name resolution for ILLink analyzer #106209

Merged
merged 8 commits into from
Aug 12, 2024

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Aug 9, 2024

Fixes #95118

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Aug 9, 2024
@dotnet-policy-service dotnet-policy-service bot added the linkable-framework Issues associated with delivering a linker friendly framework label Aug 9, 2024
@sbomer sbomer requested a review from a team August 9, 2024 23:42
@sbomer sbomer marked this pull request as ready for review August 9, 2024 23:42
Copy link
Member

@jtschuster jtschuster left a comment

Choose a reason for hiding this comment

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

A couple style/organization nits, but LGTM otherwise, thank you!

Comment on lines +18 to +19
readonly Location _location;
readonly Action<Diagnostic>? _reportDiagnostic;
Copy link
Member

Choose a reason for hiding this comment

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

Do we need these in addition to the _diagnosticContext? I don't see anywhere where they would have different values

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, _location needs to be separate so that we can pass it through to ReflectionAccessAnalyzer, see #105956 for more context.


namespace ILLink.Shared.TrimAnalysis
{
internal partial struct RequireDynamicallyAccessedMembersAction
{
readonly Compilation _compilation;
Copy link
Member

Choose a reason for hiding this comment

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

nit: It looks like this Compilation is only ever passed down to ReflectionAccessAnalyzer.TryResolveTypeNameAndMark() to create the same TypeNameResolver. Maybe we could just have a TypeNameResolver field instead? And we could then call TryResolveTypeNameAndMark directly on that field and remove ReflectionAccessAnalyzer.TryResolveTypeNameAndMark().

Copy link
Member Author

Choose a reason for hiding this comment

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

Changed this to pass around TypeNameResolver instead of Compilation, and made it a field of ReflectionAccessAnalyzer. I didn't get rid of TryResolveTypeNameAndMark from ReflectionAccessAnalyzer, to keep the code structure similar to ReflectionMarker.

- Remove unused using
- Simplify string concatenation
- Remove unnecessary partial
- Pass around TypeNameResolver instead of Compilation
@sbomer sbomer merged commit e5b1d02 into dotnet:main Aug 12, 2024
75 of 77 checks passed
@jakobbotsch
Copy link
Member

The repo no longer builds locally for me after this PR. I get

...
  LibraryImportGenerator -> C:\dev\dotnet\runtime4\artifacts\bin\LibraryImportGenerator\Release\netstandard2.0\Microsoft.Interop.LibraryImportGenerator.dll
  System.Private.CoreLib -> C:\dev\dotnet\runtime4\artifacts\bin\System.Private.CoreLib\ref\Checked\net9.0\System.Private.CoreLib.dll
CSC : error CS8032: An instance of analyzer ILLink.RoslynAnalyzer.RequiresAssemblyFilesAnalyzer cannot be created from C:\dev\dotnet\runtime4\artifacts\bin\ILLink.RoslynAnalyzer\Release\netstandard2.0\ILLink.RoslynAnal
yzer.dll : Could not load file or assembly 'System.Collections.Immutable, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. [C:\dev\dotnet\runtime4\src\core
clr\System.Private.CoreLib\System.Private.CoreLib.csproj]
CSC : error CS8032: An instance of analyzer ILLink.RoslynAnalyzer.RequiresDynamicCodeAnalyzer cannot be created from C:\dev\dotnet\runtime4\artifacts\bin\ILLink.RoslynAnalyzer\Release\netstandard2.0\ILLink.RoslynAnalyz
er.dll : Could not load file or assembly 'System.Collections.Immutable, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. [C:\dev\dotnet\runtime4\src\corecl
r\System.Private.CoreLib\System.Private.CoreLib.csproj]
CSC : error CS8032: An instance of analyzer ILLink.RoslynAnalyzer.COMAnalyzer cannot be created from C:\dev\dotnet\runtime4\artifacts\bin\ILLink.RoslynAnalyzer\Release\netstandard2.0\ILLink.RoslynAnalyzer.dll : Could n
ot load file or assembly 'System.Collections.Immutable, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. [C:\dev\dotnet\runtime4\src\coreclr\System.Private
.CoreLib\System.Private.CoreLib.csproj]
CSC : error CS8032: An instance of analyzer ILLink.RoslynAnalyzer.DynamicallyAccessedMembersAnalyzer cannot be created from C:\dev\dotnet\runtime4\artifacts\bin\ILLink.RoslynAnalyzer\Release\netstandard2.0\ILLink.Rosly
nAnalyzer.dll : Could not load file or assembly 'System.Collections.Immutable, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. [C:\dev\dotnet\runtime4\src
\coreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]
CSC : error CS8032: An instance of analyzer ILLink.RoslynAnalyzer.RequiresUnreferencedCodeAnalyzer cannot be created from C:\dev\dotnet\runtime4\artifacts\bin\ILLink.RoslynAnalyzer\Release\netstandard2.0\ILLink.RoslynA
nalyzer.dll : Could not load file or assembly 'System.Collections.Immutable, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.. [C:\dev\dotnet\runtime4\src\c
oreclr\System.Private.CoreLib\System.Private.CoreLib.csproj]

Any idea why?

@AlexRadch
Copy link
Contributor

@sbomer I can not build runtime after this PR #106321

@@ -28,6 +31,7 @@
<PrivateAssets>all</PrivateAssets>
<ExcludeAssets>contentfiles</ExcludeAssets> <!-- We include our own copy of the ClosedAttribute to work in source build -->
</PackageReference>
<PackageReference Include="System.Reflection.Metadata" Version="$(SystemReflectionMetadataVersion)" />
Copy link
Member

@ViktorHofer ViktorHofer Aug 13, 2024

Choose a reason for hiding this comment

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

This change broke the repo build. See #106321. I think this version of SRM is too new. cc @ericstj

Copy link
Member

@ericstj ericstj Aug 13, 2024

Choose a reason for hiding this comment

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

Yeah, an analyzer should only be using the version of SRM that roslyn provides for it.

It could be possible for an analyzer to carry a newer version of SRM, but it would need to be copied/deployed with the analyzer (and have a different version than the one in roslyn) - and we'd need to make sure to never try to exchange SRM types with the compiler (I don't think any SRM types are part of roslyn's public API but I can't be certain).

It might be possible to have this work once we upgrade to preview7 SDK, but I think it might still be broken in the editor/design-time build if VS itself doesn't have the new SRM.

sbomer added a commit that referenced this pull request Aug 13, 2024
jtschuster added a commit to jtschuster/runtime that referenced this pull request Aug 16, 2024
sbomer added a commit that referenced this pull request Aug 20, 2024
Adds tests from #106209. This
includes test coverage for
#95118 and
#106214.
@github-actions github-actions bot locked and limited conversation to collaborators Sep 14, 2024
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
None yet
Development

Successfully merging this pull request may close these issues.

Trim analyzer: Implement type name parsing
6 participants