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

Fix type parsing issues in ILLink and ILC #104060

Merged
merged 12 commits into from
Jul 12, 2024
Merged

Conversation

sbomer
Copy link
Member

@sbomer sbomer commented Jun 26, 2024

This replaces the existing warning IL2105. I left that one alone instead of repurposing it, because we already documented IL2105 and older versions of ILLink will produce it. Best to avoid any confusion about them.

- Revert change to needsAssemblyName for types in attribute XML
- Update SharedStrings.resx to match renamed DiagnosticId
@sbomer sbomer requested a review from jtschuster June 26, 2024 22:34
@dotnet-issue-labeler dotnet-issue-labeler bot added the area-Tools-ILLink .NET linker development as well as trimming analyzers label Jun 26, 2024
Copy link
Contributor

Tagging subscribers to this area: @agocke, @sbomer, @vitek-karas
See info in area-owners.md if you want to be subscribed.

{
CustomAttributeArgument[] arguments = ReadCustomAttributeArguments (nav, attributeType);
CustomAttributeArgument[] arguments = ReadCustomAttributeArguments (nav, provider);
Copy link
Member Author

@sbomer sbomer Jun 26, 2024

Choose a reason for hiding this comment

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

I changed the type resolver to return null when trying to resolve a non-assembly-qualified type name and we didn't know the starting assembly. This uncovered a problem where we were passing the wrong origin here - we were passing the attribute type down into ReadCustomAttributeArgument which expected to receive the attributed member (memberWithAttribute before the diff).

I found the issue because our tests construct a RemoveAttributeInstancesAttribute in memory that doesn't have an associated assembly, so we were passing null to the type resolver and failing to resolve some types specified in the XML.

Copy link
Member Author

@sbomer sbomer Jun 27, 2024

Choose a reason for hiding this comment

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

Turns out this broke the "library mode" trimming, which was relying on being able to resolve System.String from the same assembly as the attribute type (which we look for in any input assembly). This allowed types to be resolved from System.Runtime in library mode, whereas with this change we only look in System.Private.CoreLib. We probably should clarify the type resolution policy for XML - I'll give it some more thought.

Copy link
Member Author

Choose a reason for hiding this comment

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

I made a fix to use System.Runtime as the system assembly in library mode.

- Test array/byref/pointer that's not assembly-qualified

Also:
- Fix ILCompiler.Trimming.Tests build
- Clean up usings
- Fix check for array/pointer/byref types, update tests
- Fix ILCompiler.Trimming.Tests build
- Replace IL2105 suppressions with IL2122
- Fix trimming tests that were not using fully-qualified type names
@@ -53,7 +53,7 @@ public DebuggerProxy(MyClass instance)
}
}

[DebuggerTypeProxy("Program+MyClassWithProxyStringProxy")]
[DebuggerTypeProxy("Program+MyClassWithProxyStringProxy, project")]
Copy link
Member Author

Choose a reason for hiding this comment

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

@eerhardt PTAL. This change will start producing trim warnings for non-assembly-qualified string passed to DAM-annotated locations.

Copy link
Member

Choose a reason for hiding this comment

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

I think I'm fine with this behavior. I searched a few places that use Type.GetType currently, and they all appear to be using assembly qualified names. If we see new warnings, we should fix them.

Copy link
Member Author

Choose a reason for hiding this comment

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

Note that a string passed directly to Type.GetType is ok - the new warning is only for when it's passed to a DAM-annotated location. That's because at that point we don't know where to look up a non-qualified type (since it could flow to Type.GetType in another assembly).

@sbomer sbomer requested a review from eerhardt June 27, 2024 21:34
Comment on lines 90 to 91
// Library mode builds against System.Runtime ref assembly
Context.SystemModuleName = "System.Runtime";
Copy link
Member

Choose a reason for hiding this comment

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

We should know what the core assembly is (it's the one that defines System.Object). Why is this extra configuration needed? I think I didn't fully understand #104060 (comment).

Copy link
Member Author

Choose a reason for hiding this comment

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

We just needed some way to let "library mode" resolve types from System.Runtime (the XML parsing logic relies on it). I think your suggestion to look where System.Object is defined works better, thanks!

- Resolve object to find core library
@sbomer sbomer merged commit 9407c9c into dotnet:main Jul 12, 2024
143 of 163 checks passed
@jonathanpeppers
Copy link
Member

@sbomer is it possible IL2122 might still appear even if SuppressTrimAnalysisWarnings=true / --notrimwarn was passed in?

image

We noticed this here:

sbomer added a commit that referenced this pull request Jul 29, 2024
With #104060 there will be
trim warnings whenever a non-qualified type name is used with
this API, so the call to `_type.AssemblyGetType` is effectively
unreachable in a trimmed app with no trim warnings and it is safe
to suppress.
@github-actions github-actions bot locked and limited conversation to collaborators Aug 17, 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
Projects
None yet
5 participants