From 45437e2204a004c78d15d5d8a336cd14d7afddcd Mon Sep 17 00:00:00 2001 From: Jonathan Peppers Date: Mon, 11 Mar 2024 12:48:50 -0500 Subject: [PATCH] [Java.Interop] suppress IL3050 with `#pragma` (#1201) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Context: https://github.com/xamarin/xamarin-android/pull/8758#discussion_r1503086757 Context: https://github.com/xamarin/java.interop/issues/1192 Context: 2197579478152fbc815eb15195977f808cd6bde4 In 7d1e7057 and b8f6f8884, we suppressed IL3050, an AOT-related warning with: // FIXME: https://github.com/xamarin/java.interop/issues/1192 [UnconditionalSuppressMessage ("AOT", "IL3050")] // Problematic code here We don't immediately *plan* to support NativeAOT on Android in .NET 9, so it would be nice if we could suppress the warning *for now*. However, if/when anyone tried to use this code in a NativeAOT context such as 21975794, they *should* get a warning (but didn't). We should instead use: // FIXME: https://github.com/xamarin/java.interop/issues/1192 #pragma warning disable IL3050 // Problematic code here #pragma warning restore IL3050 This will prevent us from introducing new `IL3050` and related warnings, but if anyone consumes `Java.Interop.dll` from NativeAOT they will see the warning. This (re-)introduces IL3050 warnings in the build for `samples/Hello-NativeAOTFromJNI` (21975794): % dotnet publish -c Release -r osx-x64 … …/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(665): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniValueManager.g__MakeGenericMethod|41_0(MethodInfo,Type): Using member 'System.Reflection.MethodInfo.MakeGenericMethod(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. …/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(381): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniValueManager.g__MakeGenericType|31_1(Type,Type[]): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. …/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs(789): AOT analysis warning IL3050: Java.Interop.JavaPeerableValueMarshaler.CreateParameterToManagedExpression(JniValueMarshalerContext,ParameterExpression,ParameterAttributes,Type): Using member 'System.Linq.Expressions.Expression.Call(Expression,String,Type[],Expression[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. Calling a generic method requires dynamic code generation. This can be suppressed if the method is not generic. …/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs(284): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniTypeManager.MakeGenericType(Type,Type): Using member 'System.Type.MakeGenericType(Type[])' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The native code for this instantiation might not be available at runtime. …/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs(277): AOT analysis warning IL3050: Java.Interop.JniRuntime.JniTypeManager.MakeArrayType(Type): Using member 'System.Type.MakeArrayType()' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. The code for an array of the specified type might not be available. --- .../Java.Interop/JniRuntime.JniTypeManager.cs | 16 ++++++++++------ .../Java.Interop/JniRuntime.JniValueManager.cs | 10 ++++++---- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs index 6e4867192..7d5584f9f 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniTypeManager.cs @@ -271,14 +271,18 @@ protected virtual IEnumerable GetSimpleReferences (Type type) static readonly Type[] EmptyTypeArray = Array.Empty (); const string NotUsedInAndroid = "This code path is not used in Android projects."; - // FIXME: https://github.com/xamarin/java.interop/issues/1192 - [UnconditionalSuppressMessage ("AOT", "IL3050", Justification = NotUsedInAndroid)] - static Type MakeArrayType (Type type) => type.MakeArrayType (); + static Type MakeArrayType (Type type) => + // FIXME: https://github.com/xamarin/java.interop/issues/1192 + #pragma warning disable IL3050 + type.MakeArrayType (); + #pragma warning restore IL3050 - // FIXME: https://github.com/xamarin/java.interop/issues/1192 [UnconditionalSuppressMessage ("Trimming", "IL2055", Justification = NotUsedInAndroid)] - [UnconditionalSuppressMessage ("AOT", "IL3050", Justification = NotUsedInAndroid)] - static Type MakeGenericType (Type type, Type arrayType) => type.MakeGenericType (arrayType); + static Type MakeGenericType (Type type, Type arrayType) => + // FIXME: https://github.com/xamarin/java.interop/issues/1192 + #pragma warning disable IL3050 + type.MakeGenericType (arrayType); + #pragma warning restore IL3050 [UnconditionalSuppressMessage ("Trimming", "IL2073", Justification = "Types returned here should be preserved via other means.")] [return: DynamicallyAccessedMembers (MethodsConstructorsInterfaces)] diff --git a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs index b26e3b614..d0d13c87c 100644 --- a/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs +++ b/src/Java.Interop/Java.Interop/JniRuntime.JniValueManager.cs @@ -370,15 +370,16 @@ static Type GetPeerType ([DynamicallyAccessedMembers (Constructors)] Type type) static Type? AssemblyGetType (Assembly assembly, string typeName) => assembly.GetType (typeName); - // FIXME: https://github.com/xamarin/java.interop/issues/1192 [UnconditionalSuppressMessage ("Trimming", "IL2055", Justification = makeGenericTypeMessage)] - [UnconditionalSuppressMessage ("AOT", "IL3050", Justification = makeGenericTypeMessage)] [return: DynamicallyAccessedMembers (Constructors)] static Type MakeGenericType ( [DynamicallyAccessedMembers (Constructors)] Type type, Type [] arguments) => + // FIXME: https://github.com/xamarin/java.interop/issues/1192 + #pragma warning disable IL3050 type.MakeGenericType (arguments); + #pragma warning restore IL3050 Type[] arguments = type.GetGenericArguments (); if (arguments.Length == 0) @@ -657,11 +658,12 @@ static JniValueMarshaler GetObjectArrayMarshaler (Type elementType) { const string makeGenericMethodMessage = "This code path is not used in Android projects."; - // FIXME: https://github.com/xamarin/java.interop/issues/1192 [UnconditionalSuppressMessage ("Trimming", "IL2060", Justification = makeGenericMethodMessage)] - [UnconditionalSuppressMessage ("AOT", "IL3050", Justification = makeGenericMethodMessage)] static MethodInfo MakeGenericMethod (MethodInfo method, Type type) => + // FIXME: https://github.com/xamarin/java.interop/issues/1192 + #pragma warning disable IL3050 method.MakeGenericMethod (type); + #pragma warning restore IL3050 Func indirect = GetObjectArrayMarshalerHelper; var reifiedMethodInfo = MakeGenericMethod (