Skip to content
This repository was archived by the owner on Sep 25, 2026. It is now read-only.

Enable multiple arguments for RemoveAttributeInstancesAttribute ctor - #2516

Merged
jtschuster merged 16 commits into
dotnet:mainfrom
jtschuster:removeattributemultipleargs
Feb 4, 2022
Merged

jtschuster merged 16 commits into
dotnet:mainfrom
jtschuster:removeattributemultipleargs

Conversation

@jtschuster

Copy link
Copy Markdown
Member

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 RemoveAttributeInstancesAttribute with 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 the ILLink.LinkAttributes.xml.

At the moment, the arguments to the RemoveAttributeInstancesAttribute in 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, ...)]

<type fullname="Mono.Linker.Tests.Cases.LinkAttributes.TestConditionalRemoveAttribute">
  <attribute internal="RemoveAttributeInstances">
    <argument type="System.Object">
      <argument type="System.Int32">100</argument>
    </argument>
  </attribute>
</type>

vs. Unboxed

<type fullname="Mono.Linker.Tests.Cases.LinkAttributes.TestConditionalRemoveAttribute">
  <attribute internal="RemoveAttributeInstances">
    <argument type="System.Int32">100</argument>
  </attribute>
</type>

Generates a constructor for each number of ctor arguments found in the
LinkAttributes.xml file, and updates some of the logic around that.
@jtschuster jtschuster changed the title Removeattributemultipleargs Enable multiple arguments for RemoveAttributeInstancesAttribute ctor Jan 18, 2022
@jtschuster
jtschuster requested review from LakshanF, agocke, marek-safar, sbomer, tlakollo and vitek-karas and removed request for marek-safar and sbomer January 18, 2022 23:42
if (knownTypeDef.GetConstructorsOnType (_ => true).Any (m => m.Parameters.Count == ctorArgumentCount)) {
return knownTypeDef;
} else {
td = knownTypeDef;

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.

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>

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

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.

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.

Comment thread docs/data-formats.md Resolved
Comment thread src/linker/Linker.Steps/MarkStep.cs Resolved
Comment thread docs/data-formats.md Resolved
@marek-safar

Copy link
Copy Markdown
Contributor

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.

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.

@jtschuster

Copy link
Copy Markdown
Member Author

I updated this to allow RemoveAttributeInstances to match a constructor with params. I did this by flattening the params array from the attribute to be removed in MarkStep when we check if the arguments match, but I think it might make more sense to instead support arrays as an argument to RemoveAttributeInstances. Then, to match an attribute constructed with a params array, you would have to pass an array as the last argument to RemoveAttributeInstances.

@marek-safar @vitek-karas Do you have opinions here?

@vitek-karas

Copy link
Copy Markdown
Member

I must admit I'm a bit confused. Why don't we use just one constructor the RemoveAttributeInstance - the one which takes params object[]? Yes - everything will be boxed, but that's a technical detail which we can easily hide. I guess I don't see the value of being able to generate different .ctor overloads... it seems to just complicate things.

Comment thread src/linker/Linker.Steps/MarkStep.cs Resolved
Comment thread src/linker/Linker.Steps/MarkStep.cs Resolved
@jtschuster

Copy link
Copy Markdown
Member Author

After realizing that params arrays are just C# sugar and end up looking like a regular array in IL, I decided to pivot force users to match a param array with a regular array in the xml. I had to add functionality for building array CustomAttributeArguments in the GetCustomAttributeArgument function. There I also added automatic boxing of arguments within object[] arrays. This way, you could write

<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 CustomAttributeArguments easier, I created an extension method IsEqualTo() for the struct. It ignores any boxing in object types and checks only the value (or values for arrays) contained in the "botttom most" CustomAttributeArgument.

@jtschuster

Copy link
Copy Markdown
Member Author

I must admit I'm a bit confused. Why don't we use just one constructor the RemoveAttributeInstance - the one which takes params object[]? Yes - everything will be boxed, but that's a technical detail which we can easily hide. I guess I don't see the value of being able to generate different .ctor overloads... it seems to just complicate things.

@vitek-karas I've updated RemoveAttributeInstances to have .ctors that either take void, object (to not break runtime), and params object[]. This did really simplify things, and I think this addresses your concern, but let me know if it doesn't.

@jtschuster
jtschuster merged commit ecf2028 into dotnet:main Feb 4, 2022
@jtschuster
jtschuster deleted the removeattributemultipleargs branch February 17, 2022 17:26
agocke pushed a commit to dotnet/runtime that referenced this pull request Nov 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants