Repository navigation
Fix type parsing issues in ILLink and ILC #104060
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
Changes from all commits
2f7d958
5be1063
c0b9690
e8338e0
3985530
7816744
f02bf62
e48b940
970d2c4
7a1be9c
7c63654
0f3187f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,7 +53,7 @@ public DebuggerProxy(MyClass instance) | |
| } | ||
| } | ||
|
|
||
| [DebuggerTypeProxy("Program+MyClassWithProxyStringProxy")] | ||
| [DebuggerTypeProxy("Program+MyClassWithProxyStringProxy, project")] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). |
||
| public class MyClassWithProxyString | ||
| { | ||
| public string Name { get; set; } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,7 +72,7 @@ public void Parse (AttributeInfo xmlInfo) | |
| continue; | ||
| } | ||
|
|
||
| CustomAttribute? customAttribute = CreateCustomAttribute (attributeNav, attributeType); | ||
| CustomAttribute? customAttribute = CreateCustomAttribute (attributeNav, attributeType, provider); | ||
| if (customAttribute != null) { | ||
| _context.LogMessage ($"Assigning external custom attribute '{FormatCustomAttribute (customAttribute)}' instance to '{provider}'."); | ||
| customAttributesBuilder.Add (customAttribute); | ||
|
|
@@ -153,9 +153,9 @@ static string FormatCustomAttribute (CustomAttribute ca) | |
| return _context.MarkedKnownMembers.RemoveAttributeInstancesAttributeDefinition = td; | ||
| } | ||
|
|
||
| CustomAttribute? CreateCustomAttribute (XPathNavigator nav, TypeDefinition attributeType) | ||
| CustomAttribute? CreateCustomAttribute (XPathNavigator nav, TypeDefinition attributeType, ICustomAttributeProvider provider) | ||
| { | ||
| CustomAttributeArgument[] arguments = ReadCustomAttributeArguments (nav, attributeType); | ||
| CustomAttributeArgument[] arguments = ReadCustomAttributeArguments (nav, provider); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 ( I found the issue because our tests construct a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| MethodDefinition? constructor = FindBestMatchingConstructor (attributeType, arguments); | ||
| if (constructor == null) { | ||
|
|
@@ -223,22 +223,22 @@ void ReadCustomAttributeProperties (XPathNavigator nav, TypeDefinition attribute | |
| } | ||
| } | ||
|
|
||
| CustomAttributeArgument[] ReadCustomAttributeArguments (XPathNavigator nav, TypeDefinition attributeType) | ||
| CustomAttributeArgument[] ReadCustomAttributeArguments (XPathNavigator nav, ICustomAttributeProvider provider) | ||
| { | ||
| ArrayBuilder<CustomAttributeArgument> args = default; | ||
|
|
||
| foreach (XPathNavigator argumentNav in nav.SelectChildren ("argument", string.Empty)) { | ||
| CustomAttributeArgument? caa = ReadCustomAttributeArgument (argumentNav, attributeType); | ||
| CustomAttributeArgument? caa = ReadCustomAttributeArgument (argumentNav, provider); | ||
| if (caa is not null) | ||
| args.Add (caa.Value); | ||
| } | ||
|
|
||
| return args.ToArray () ?? Array.Empty<CustomAttributeArgument> (); | ||
| } | ||
|
|
||
| CustomAttributeArgument? ReadCustomAttributeArgument (XPathNavigator nav, IMemberDefinition memberWithAttribute) | ||
| CustomAttributeArgument? ReadCustomAttributeArgument (XPathNavigator nav, ICustomAttributeProvider provider) | ||
| { | ||
| TypeReference? typeref = ResolveArgumentType (nav, memberWithAttribute); | ||
| TypeReference? typeref = ResolveArgumentType (nav, provider); | ||
| if (typeref is null) | ||
| return null; | ||
|
|
||
|
|
@@ -295,8 +295,8 @@ CustomAttributeArgument[] ReadCustomAttributeArguments (XPathNavigator nav, Type | |
| if (!typeref.IsTypeOf (WellKnownType.System_Type)) | ||
| goto default; | ||
|
|
||
| var diagnosticContext = new DiagnosticContext (new MessageOrigin (memberWithAttribute), diagnosticsEnabled: true, _context); | ||
| if (!_context.TypeNameResolver.TryResolveTypeName (svalue, diagnosticContext, out TypeReference? type, out _)) { | ||
| var diagnosticContext = new DiagnosticContext (new MessageOrigin (provider), diagnosticsEnabled: true, _context); | ||
| if (!_context.TypeNameResolver.TryResolveTypeName (svalue, diagnosticContext, out TypeReference? type, out _, needsAssemblyName: false)) { | ||
| _context.LogError (GetMessageOriginForPosition (nav), DiagnosticId.CouldNotResolveCustomAttributeTypeValue, svalue); | ||
| return null; | ||
| } | ||
|
|
@@ -308,7 +308,7 @@ CustomAttributeArgument[] ReadCustomAttributeArguments (XPathNavigator nav, Type | |
| var arrayArgumentIterator = nav.SelectChildren ("argument", string.Empty); | ||
| ArrayBuilder<CustomAttributeArgument> elements = default; | ||
| foreach (XPathNavigator elementNav in arrayArgumentIterator) { | ||
| if (ReadCustomAttributeArgument (elementNav, memberWithAttribute) is CustomAttributeArgument arg) { | ||
| if (ReadCustomAttributeArgument (elementNav, provider) is CustomAttributeArgument arg) { | ||
| // To match Cecil, elements of a list that are subclasses of the list type must be boxed in the base type | ||
| // e.g. object[] { 73 } translates to Cecil.CAA { Type: object[] : Value: CAA{ Type: object, Value: CAA{ Type: int, Value: 73} } } | ||
| if (arg.Type == elementType) { | ||
|
|
@@ -338,14 +338,14 @@ CustomAttributeArgument[] ReadCustomAttributeArguments (XPathNavigator nav, Type | |
| return null; | ||
| } | ||
|
|
||
| TypeReference? ResolveArgumentType (XPathNavigator nav, IMemberDefinition memberWithAttribute) | ||
| TypeReference? ResolveArgumentType (XPathNavigator nav, ICustomAttributeProvider provider) | ||
| { | ||
| string typeName = GetAttribute (nav, "type"); | ||
| if (string.IsNullOrEmpty (typeName)) | ||
| typeName = "System.String"; | ||
|
|
||
| var diagnosticContext = new DiagnosticContext (new MessageOrigin (memberWithAttribute), diagnosticsEnabled: true, _context); | ||
| if (!_context.TypeNameResolver.TryResolveTypeName (typeName, diagnosticContext, out TypeReference? typeref, out _)) { | ||
| var diagnosticContext = new DiagnosticContext (new MessageOrigin (provider), diagnosticsEnabled: true, _context); | ||
| if (!_context.TypeNameResolver.TryResolveTypeName (typeName, diagnosticContext, out TypeReference? typeref, out _, needsAssemblyName: false)) { | ||
| _context.LogError (GetMessageOriginForPosition (nav), DiagnosticId.TypeUsedWithAttributeValueCouldNotBeFound, typeName, nav.Value); | ||
| return null; | ||
| } | ||
|
|
@@ -501,7 +501,7 @@ void ProcessParameters (MethodDefinition method, XPathNavigator nav) | |
| foreach (ParameterDefinition parameter in method.Parameters) { | ||
| if (paramName == parameter.Name) { | ||
| if (parameter.HasCustomAttributes || _attributeInfo.CustomAttributes.ContainsKey (parameter)) | ||
| LogWarning (parameterNav, DiagnosticId.XmlMoreThanOneValyForParameterOfMethod, paramName, method.GetDisplayName ()); | ||
| LogWarning (parameterNav, DiagnosticId.XmlMoreThanOneValueForParameterOfMethod, paramName, method.GetDisplayName ()); | ||
| _attributeInfo.AddCustomAttributes (parameter, attributes, origins); | ||
| break; | ||
| } | ||
|
|
@@ -513,11 +513,15 @@ void ProcessParameters (MethodDefinition method, XPathNavigator nav) | |
|
|
||
| void ProcessReturnParameters (MethodDefinition method, XPathNavigator nav) | ||
| { | ||
| Debug.Assert (_attributeInfo != null); | ||
| bool firstAppearance = true; | ||
| foreach (XPathNavigator returnNav in nav.SelectChildren ("return", string.Empty)) { | ||
| if (firstAppearance) { | ||
| firstAppearance = false; | ||
| PopulateAttributeInfo (method.MethodReturnType, returnNav); | ||
| var (attributes, origins) = ProcessAttributes (returnNav, method); | ||
| if (attributes != null && origins != null) { | ||
| _attributeInfo.AddCustomAttributes (method.MethodReturnType, attributes, origins); | ||
| } | ||
| } else { | ||
| LogWarning (returnNav, DiagnosticId.XmlMoreThanOneReturnElementForMethod, method.GetDisplayName ()); | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.