Repository navigation
Enable multiple arguments for RemoveAttributeInstancesAttribute ctor - #2516
Conversation
Generates a constructor for each number of ctor arguments found in the LinkAttributes.xml file, and updates some of the logic around that.
| if (knownTypeDef.GetConstructorsOnType (_ => true).Any (m => m.Parameters.Count == ctorArgumentCount)) { | ||
| return knownTypeDef; | ||
| } else { | ||
| td = knownTypeDef; |
There was a problem hiding this comment.
Here the type was recognized but a constructor with the number of parameters wasn't found, I think this is because you can delete attributes that match things like the first argument even if the constructor accepts 3 arguments. For example the test
<type fullname="Mono.Linker.Tests.Cases.LinkAttributes.TestConditionalRemoveAttribute">
<attribute internal="RemoveAttributeInstances">
<argument type="System.Object">
<argument>remove1</argument>
</argument>
</attribute>
</type>[TestConditionalRemove ("remove1", '1', 3)] //This gets removed because the first arg matches
static void Removed_2 ()
{
}The problem here is that I think there is nothing preventing from creating a malformed version of the type constructor, something that doesn't exist, for example:
<type fullname="Mono.Linker.Tests.Cases.LinkAttributes.TestConditionalRemoveAttribute">
<attribute internal="RemoveAttributeInstances">
<argument type="System.Object">
<argument>arg1</argument>
</argument>
<argument type="System.Object">
<argument>arg2</argument>
</argument>
<argument type="System.Object">
<argument>arg3</argument>
</argument>
<argument type="System.Object">
<argument>arg4</argument>
</argument>
<argument type="System.Object">
<argument>arg5</argument>
</argument>
</attribute>
</type>There was a problem hiding this comment.
Here, knownTypeDef is the type definition of RemoveAttributeInstancesAttribute, not the TestConditionalRemoveAttribute type. From my understanding, later in the linker it checks the arguments passed to the constructor of each instance of TestConditionalRemoveAttribute (the ones placed on the methods), against the arguments to the constructor of RemoveAttributeInstancesAttribute (which is placed on the TestConditionalRemoveAttribute definition by the xml). So we are generating a new ctor for that RemoveAttributeInstancesAttribute type to enable constructors of that to enable the xml to specify multiple arguments to match, not a ctor for TestConditionalRemoveAttribute.
So yes, it enables the xml with 5 arguments, but it's not generating a new ctor for the TestConditionalRemoveAttribute that shouldn't be there, and if the xml has more arguments than any of the TestConditionalRemoveAttribute ctors, then none are removed.
There was a problem hiding this comment.
For me, it's generating a new fake constructor with 5 parameters and adding it to _context.MarkedKnownMembers.RemoveAttributeInstancesAttributeDefinition.
There is no validation afterward, so it gets added and no warning is produced. Before it would warn saying that having more than 1 argument in the XML is not allowed, which is also not a great experience.
I do understand that TestConditionalRemoveAttribute won't have the constructor and therefore you cannot create an attribute with 5 constructors without getting a compiler error, but I think we should produce a warning for constructors that don't exist. Otherwise, we could have people thinking that what they wrote in the XML was correct and is actually doing nothing, or even worse they could write something that at some point is invalid but later on is deleting something that might not be needed to be removed.
I think that's not what we want to do. The fact that the ctor needs to be generated is only "temporary" fix before someone add the exact attribute to the framework and we can more convenient way to remove attributes conditionally. It'd be beneficial if the signature of such type ctors would be same to avoid problems with 2 types doing the same thing. |
|
I updated this to allow @marek-safar @vitek-karas Do you have opinions here? |
|
I must admit I'm a bit confused. Why don't we use just one constructor the RemoveAttributeInstance - the one which takes |
|
After realizing that <attribute internal="RemoveAttributeInstances">
<argument type="System.Object[]">
<argument type="System.Int32">99</argument>
<argument type="System.String">remove3</argument>
</argument>
</attribute>instead of <attribute internal="RemoveAttributeInstances">
<argument type="System.Object[]">
<argument type="System.Object">
<argument type="System.Int32">99</argument>
</argument>
<argument type="System.Object">
<argument type="System.String">remove3</argument>
</argument>
</argument>
</attribute>Also, to make matching |
@vitek-karas I've updated |
…r for RemoveAttributeInstancesAttribute (dotnet/linker#2516) Commit migrated from dotnet/linker@ecf2028
Generates a constructor for each number of ctor arguments found in the
LinkAttributes.xml file, and updates some of the logic around that.
Previously we would create a single
RemoveAttributeInstancesAttributewith one ctor of type Object. This meant the xml could only have a single argument for that attribute, and thus made it harder to be specific with which attributes to remove. This generates ctors with an arbitrary number of arguments depending on theILLink.LinkAttributes.xml.At the moment, the arguments to the
RemoveAttributeInstancesAttributein xml must be "boxed" in an Object type because that is how we got around having a single constructor for many argument types previously. Now that we generate arbitrary ctors for the attribute, it should be possible to allow the arguments in the xml to be unboxed without the Object wrapper to simplify things, and can probably be done without breaking "boxed" implementations of the xml. Curious to hear thoughts on that and if it's worth the effort and potential for breaking.Ex. Boxed argument to remove
[TestConditionalRemove(100, ...)]vs. Unboxed