Skip to content

Commit

Permalink
[Foundation] Suppress trimmer warnings for NSObject.DynamicConformsTo…
Browse files Browse the repository at this point in the history
…Protocol members. (#20333)

Contributes towards #10405.
  • Loading branch information
rolfbjarne authored Mar 20, 2024
1 parent aea6526 commit 2cbc31f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Foundation/NSObject2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -541,9 +541,15 @@ public virtual bool ConformsToProtocol (NativeHandle protocol)
}
}

#if NET
// Note that this method does not work with NativeAOT, so throw an exception in that case.
// IL2075: 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.
[UnconditionalSuppressMessage ("", "IL2075", Justification = "The APIs this method tries to access are marked by other means, so this is linker-safe.")]
#endif
bool DynamicConformsToProtocol (NativeHandle protocol)
{
#if NET
// Note that this method does not work with NativeAOT, so throw an exception in that case.
if (Runtime.IsNativeAOT)
throw Runtime.CreateNativeAOTNotSupportedException ();
#endif
Expand Down
3 changes: 0 additions & 3 deletions tests/dotnet/UnitTests/TrimmerWarningsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void TrimmerWarningsDynamicRegistrar (ApplePlatform platform, string runt
case ApplePlatform.iOS:
case ApplePlatform.TVOS:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/Foundation/NSObject2.cs" /* line 554 */, "Foundation.NSObject.DynamicConformsToProtocol(NativeHandle): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/Blocks.cs" /* line 313 */, "ObjCRuntime.BlockLiteral.SetupBlock(Delegate, Delegate): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.MonoPInvokeCallbackAttribute.DelegateType.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 542 */, "Registrar.DynamicRegistrar.HasThisAttributeImpl(MethodBase): Attribute 'System.Runtime.CompilerServices.ExtensionAttribute' is being referenced in code but the trimmer was instructed to remove all instances of this attribute. If the attribute instances are necessary make sure to either remove the trimmer attribute XML portion which removes the attribute instances, or override the removal by using the trimmer XML descriptor to keep the attribute type (which in turn keeps all of its instances)."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
Expand All @@ -47,7 +46,6 @@ public void TrimmerWarningsDynamicRegistrar (ApplePlatform platform, string runt
break;
case ApplePlatform.MacOSX:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/Foundation/NSObject2.cs" /* line 554 */, "Foundation.NSObject.DynamicConformsToProtocol(NativeHandle): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 542 */, "Registrar.DynamicRegistrar.HasThisAttributeImpl(MethodBase): Attribute 'System.Runtime.CompilerServices.ExtensionAttribute' is being referenced in code but the trimmer was instructed to remove all instances of this attribute. If the attribute instances are necessary make sure to either remove the trimmer attribute XML portion which removes the attribute instances, or override the removal by using the trimmer XML descriptor to keep the attribute type (which in turn keeps all of its instances)."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.CoreCLR.cs" /* line 177 */, "ObjCRuntime.Runtime.ResolvingEventHandler(AssemblyLoadContext, AssemblyName): Using member 'System.Runtime.Loader.AssemblyLoadContext.LoadFromAssemblyPath(String)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Types and members the loaded assembly depends on might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
Expand All @@ -62,7 +60,6 @@ public void TrimmerWarningsDynamicRegistrar (ApplePlatform platform, string runt
break;
case ApplePlatform.MacCatalyst:
expectedWarnings = new ExpectedBuildMessage [] {
new ExpectedBuildMessage ("src/Foundation/NSObject2.cs" /* line 554 */, "Foundation.NSObject.DynamicConformsToProtocol(NativeHandle): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'System.Type.GetInterfaces()'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
new ExpectedBuildMessage ("src/ObjCRuntime/DynamicRegistrar.cs" /* line 542 */, "Registrar.DynamicRegistrar.HasThisAttributeImpl(MethodBase): Attribute 'System.Runtime.CompilerServices.ExtensionAttribute' is being referenced in code but the trimmer was instructed to remove all instances of this attribute. If the attribute instances are necessary make sure to either remove the trimmer attribute XML portion which removes the attribute instances, or override the removal by using the trimmer XML descriptor to keep the attribute type (which in turn keeps all of its instances)."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 714 */, "ObjCRuntime.Runtime.CollectReferencedAssemblies(List<Assembly>, Assembly): Using member 'System.Reflection.Assembly.GetReferencedAssemblies()' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code. Assembly references might be removed."),
new ExpectedBuildMessage ("src/ObjCRuntime/Runtime.cs" /* line 983 */, "ObjCRuntime.Runtime.GetBlockProxyAttributeMethod(MethodInfo, Int32): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicMethods' in call to 'System.Type.GetMethod(String)'. The return value of method 'ObjCRuntime.BlockProxyAttribute.Type.get' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to."),
Expand Down

7 comments on commit 2cbc31f

@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.

@vs-mobiletools-engineering-service2

This comment was marked as outdated.

Please sign in to comment.