From 651de42732d194cee5a45fae45feda37706a8c16 Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Wed, 13 Mar 2024 10:19:43 -0500 Subject: [PATCH] [Java.Interop] supress one instance of IL2072 (#1205) Context: https://github.com/xamarin/xamarin-android/pull/8805 When testing new defaults for `$(TrimMode)=full` in xamarin/xamarin-android#8805: % dotnet new android % dotnet build -c Release -r android-arm64 Results in a single warning: external\Java.Interop\src\Java.Interop\Java.Interop\JniRuntime.JniValueManager.cs(890,4): Trim analysis warning IL2072: Java.Interop.ProxyValueMarshaler.CreateGenericObjectReferenceArgumentState(Object, ParameterAttributes): 'type' argument does not satisfy 'DynamicallyAccessedMemberTypes.Interfaces' in call to 'Java.Interop.JniRuntime.JniValueManager.GetValueMarshaler(Type)'. 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. This appears to be a case where the trimmer (ILLink's analyzers) can detect a problem where the Roslyn analyzer could not. It is generally possible for the trimmer to discover some new warning, as it has a complete view of the final code. This appears to be similar to some other places we used the justification: [UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "This code path is not used in Android projects.")] --- src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs index d0d13c87c..cb044119b 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs @@ -884,12 +884,16 @@ sealed class ProxyValueMarshaler : JniValueMarshaler { public override JniValueMarshalerState CreateGenericObjectReferenceArgumentState ([MaybeNull]object? value, ParameterAttributes synchronize) { + [UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "This code path is not used in Android projects.")] + [return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.Interfaces)] + static Type GetType (object value) => value.GetType (); + if (value == null) return new JniValueMarshalerState (); var jvm = JniEnvironment.Runtime; - var vm = jvm.ValueManager.GetValueMarshaler (value.GetType ()); + var vm = jvm.ValueManager.GetValueMarshaler (GetType (value)); if (vm != Instance) { var s = vm.CreateObjectReferenceArgumentState (value, synchronize); return new JniValueMarshalerState (s, vm);