Skip to content

Commit

Permalink
[NativeAOT] Remove unnecessary [DynamicDependency] attributes (#18808)
Browse files Browse the repository at this point in the history
This is a follow-up to #18666

This PR resolves the following warnings that I've seen in build logs
when running MySingleView with NativeAOT:

```
ILLink : warning IL2034: <Module>..cctor(): The 'DynamicDependencyAttribute' could not be analyzed. [/..../xamarin/xamarin-macios/tests/dotnet/MySingleView/MySingleView.csproj]
ILLink : warning IL2034: <Module>..cctor(): The 'DynamicDependencyAttribute' could not be analyzed. [/..../xamarin/xamarin-macios/tests/dotnet/MySingleView/MySingleView.csproj]
...
```

The generated module cctor code looks like this:

```c#
internal class <Module>
{
	[DynamicDependency("InvokeConformsToProtocol(ObjCRuntime.NativeHandle)", typeof(NSObject))]
	[DynamicDependency("Foundation.NSObject", null)]
	[DynamicDependency("ConformsToProtocol(ObjCRuntime.NativeHandle)", typeof(NSObject))]
	[DynamicDependency("Foundation.NSObject", null)]
        // ...
	static <Module>()
	{
		// ...
	}
}
```

The `[DynamicDependency("T", null)]` attributes are invalid. We could
change them to `[DynamicDependency(DynamicallyAccessedMemberTypes.None,
typeof(T))]` but I think that attribute is redundant because the type
itself is always already preserved through the attribute that precedes
it with a selector of one of its methods/fields.
  • Loading branch information
simonrozsival committed Aug 28, 2023
1 parent 88b0092 commit 6f9eea2
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tools/dotnet-linker/ApplyPreserveAttributeBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,12 @@ static bool IsConditionalAttribute (CustomAttribute? attribute)
void PreserveUnconditional (IMetadataTokenProvider provider)
{
Annotations.Mark (provider);
AddDynamicDependencyAttribute (provider);

// We want to add a dynamic dependency attribute to preserve methods and fields
// but not to preserve types while we're marking the chain of declaring types.
if (provider is not TypeDefinition) {
AddDynamicDependencyAttribute (provider);
}

var member = provider as IMemberDefinition;
if (member is null || member.DeclaringType is null)
Expand Down

6 comments on commit 6f9eea2

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.