From b8c12bd766d414d368b29753b854982e6a422496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Mon, 8 Aug 2022 16:27:12 +0900 Subject: [PATCH 1/2] Delete some .NET Nativisms * Replace MissingInteropDataException with NotSupportedException * Replace MissingRuntimeArtifactException with MissingMetadataException (it was already used as a MME in some spots anyway). I'll try to get rid of MME later. * Change the messages to refer to static analysis as the fix, not RD.XML. Open question: do we want an aka.ms link for these? We don't currently have a doc on "Making apps ready for NativeAOT", but we'll need one. --- .../CompilerHelpers/RuntimeInteropData.cs | 14 +++++----- .../src/System.Private.CoreLib.csproj | 1 - .../MissingInteropDataException.cs | 20 -------------- .../MissingMetadataExceptionCreator.cs | 27 ++++--------------- .../ReflectionDomainSetupImplementation.cs | 2 +- .../DelegateMethodInfoRetriever.cs | 10 +++---- .../src/Resources/Strings.resx | 4 +-- ...System.Private.Reflection.Execution.csproj | 1 - .../MissingRuntimeArtifactException.cs | 25 ----------------- .../src/Resources/Strings.resx | 4 +-- 10 files changed, 21 insertions(+), 87 deletions(-) delete mode 100644 src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/MissingInteropDataException.cs delete mode 100644 src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System/Reflection/MissingRuntimeArtifactException.cs diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/RuntimeInteropData.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/RuntimeInteropData.cs index 53b6dd53df1d78..a4e0d74957ad67 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/RuntimeInteropData.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/Internal/Runtime/CompilerHelpers/RuntimeInteropData.cs @@ -26,7 +26,7 @@ public static uint GetStructFieldOffset(RuntimeTypeHandle structureTypeHandle, s throw new ArgumentException(SR.Format(SR.Argument_OffsetOfFieldNotFound, RuntimeAugments.GetLastResortString(structureTypeHandle)), nameof(fieldName)); } - throw new MissingInteropDataException(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)); + throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle))); } public static int GetStructUnsafeStructSize(RuntimeTypeHandle structureTypeHandle) @@ -44,7 +44,7 @@ public static int GetStructUnsafeStructSize(RuntimeTypeHandle structureTypeHandl return structureTypeHandle.GetValueTypeSize(); } - throw new MissingInteropDataException(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)); + throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle))); } public static IntPtr GetStructUnmarshalStub(RuntimeTypeHandle structureTypeHandle) @@ -54,7 +54,7 @@ public static IntPtr GetStructUnmarshalStub(RuntimeTypeHandle structureTypeHandl return stub; } - throw new MissingInteropDataException(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)); + throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle))); } public static IntPtr GetStructMarshalStub(RuntimeTypeHandle structureTypeHandle) @@ -64,7 +64,7 @@ public static IntPtr GetStructMarshalStub(RuntimeTypeHandle structureTypeHandle) return stub; } - throw new MissingInteropDataException(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)); + throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle))); } public static IntPtr GetDestroyStructureStub(RuntimeTypeHandle structureTypeHandle, out bool hasInvalidLayout) @@ -74,7 +74,7 @@ public static IntPtr GetDestroyStructureStub(RuntimeTypeHandle structureTypeHand return stub; } - throw new MissingInteropDataException(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle)); + throw new NotSupportedException(SR.Format(SR.StructMarshalling_MissingInteropData, Type.GetTypeFromHandle(structureTypeHandle))); } @@ -82,7 +82,7 @@ public static IntPtr GetForwardDelegateCreationStub(RuntimeTypeHandle delegateTy { GetMarshallersForDelegate(delegateTypeHandle, out _, out _, out IntPtr delegateCreationStub); if (delegateCreationStub == IntPtr.Zero) - throw new MissingInteropDataException(SR.DelegateMarshalling_MissingInteropData, Type.GetTypeFromHandle(delegateTypeHandle)); + throw new NotSupportedException(SR.Format(SR.DelegateMarshalling_MissingInteropData, Type.GetTypeFromHandle(delegateTypeHandle))); return delegateCreationStub; } @@ -91,7 +91,7 @@ public static IntPtr GetDelegateMarshallingStub(RuntimeTypeHandle delegateTypeHa GetMarshallersForDelegate(delegateTypeHandle, out IntPtr openStub, out IntPtr closedStub, out _); IntPtr pStub = openStaticDelegate ? openStub : closedStub; if (pStub == IntPtr.Zero) - throw new MissingInteropDataException(SR.DelegateMarshalling_MissingInteropData, Type.GetTypeFromHandle(delegateTypeHandle)); + throw new NotSupportedException(SR.Format(SR.DelegateMarshalling_MissingInteropData, Type.GetTypeFromHandle(delegateTypeHandle))); return pStub; } diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj index 9075bb0cc5de08..3a113147f4eac1 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System.Private.CoreLib.csproj @@ -211,7 +211,6 @@ - diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/MissingInteropDataException.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/MissingInteropDataException.cs deleted file mode 100644 index 437c9e41bf6acf..00000000000000 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/InteropServices/MissingInteropDataException.cs +++ /dev/null @@ -1,20 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - - -namespace System.Runtime.InteropServices -{ - /// - /// Thrown when a manual marshalling method is called, but the type was not found - /// by static analysis or in the rd.xml file. - /// - class MissingInteropDataException : Exception - { - public Type MissingType { get; private set; } - public MissingInteropDataException(string resourceFormat, Type pertainantType): - base(SR.Format(resourceFormat, pertainantType.ToString())) - { - MissingType = pertainantType; - } - } -} diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/PayForPlayExperience/MissingMetadataExceptionCreator.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/PayForPlayExperience/MissingMetadataExceptionCreator.cs index 46b245f9db7438..ebe6da8349665a 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/PayForPlayExperience/MissingMetadataExceptionCreator.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/PayForPlayExperience/MissingMetadataExceptionCreator.cs @@ -32,11 +32,11 @@ internal static MissingMetadataException Create(TypeInfo? pertainant, string nes string usefulPertainant = ComputeUsefulPertainantIfPossible(pertainant); if (usefulPertainant == null) - return new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, pertainant.ToString())); + return new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, pertainant.ToString())); else { usefulPertainant = usefulPertainant + "." + DiagnosticMappingTables.ConvertBackTickNameToNameWithReducerInputFormat(nestedTypeName, null); - return new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_EdbNeeded, usefulPertainant)); + return new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_EdbNeeded, usefulPertainant)); } } @@ -55,7 +55,7 @@ private static MissingMetadataException CreateFromString(string? pertainant) if (pertainant == null) return new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, "")); else - return new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_EdbNeeded, pertainant)); + return new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_EdbNeeded, pertainant)); } internal static MissingMetadataException CreateMissingArrayTypeException(Type elementType, bool isMultiDim, int rank) @@ -78,9 +78,9 @@ internal static MissingMetadataException CreateFromMetadataObject(string resourc string usefulPertainant = ComputeUsefulPertainantIfPossible(pertainant); if (usefulPertainant == null) - return new MissingMetadataException(Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, pertainant.ToString())); + return new MissingMetadataException(SR.Format(SR.Reflection_InsufficientMetadata_NoHelpAvailable, pertainant.ToString())); else - return new MissingMetadataException(Format(resourceId, usefulPertainant)); + return new MissingMetadataException(SR.Format(resourceId, usefulPertainant)); } public static string ComputeUsefulPertainantIfPossible(object pertainant) @@ -299,22 +299,5 @@ private static string CreateConstructedGenericTypeStringIfAvailable(Type generic return genericTypeName.ToString(); } - // - // This is a workaround to prevent crucial information being lost when compiling console apps using the retail ILC. - // This combination turns rich error messages from the framework into resource keys without the substitution strings. - // We'll detect this case here and append the substitution string manually. - // - private static string Format(string resourceMessage, object parameter) - { - if (resourceMessage.Contains("{0}")) - return SR.Format(resourceMessage, parameter); - - // If the rich exception message was eaten by the IL2IL transform, make sure the resulting message - // has a link pointing the user towards the .NET Native debugging guide. These get normally appended - // to the restricted message by the transform, but the pattern here is not recognized by the rewriter. - // At this point we know the message doesn't come from resources (because message == resource key), so - // we can't do much to make this localizable. - return resourceMessage + ": " + parameter + ". For more information, visit http://go.microsoft.com/fwlink/?LinkId=623485"; - } } } diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs index 439be772211663..143dbf0b2011e7 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Execution/ReflectionDomainSetupImplementation.cs @@ -52,7 +52,7 @@ public sealed override Exception CreateNonInvokabilityException(MemberInfo perta } string pertainantString = MissingMetadataExceptionCreator.ComputeUsefulPertainantIfPossible(pertainant); - return new MissingRuntimeArtifactException(SR.Format(resourceName, pertainantString ?? "?")); + return new MissingMetadataException(SR.Format(resourceName, pertainantString ?? "?")); } public sealed override Exception CreateMissingArrayTypeException(Type elementType, bool isMultiDim, int rank) diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Extensions/NonPortable/DelegateMethodInfoRetriever.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Extensions/NonPortable/DelegateMethodInfoRetriever.cs index d8b2b88fb39ed7..68cbc9f19918bd 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Extensions/NonPortable/DelegateMethodInfoRetriever.cs +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Internal/Reflection/Extensions/NonPortable/DelegateMethodInfoRetriever.cs @@ -63,10 +63,8 @@ public static MethodInfo GetDelegateMethodInfo(Delegate del) callTryGetMethod = false; methodHandle = QMethodDefinition.FromObjectAndInt(resolver->Reader, resolver->Handle); - RuntimeTypeHandle declaringTypeHandleIgnored; - MethodNameAndSignature nameAndSignatureIgnored; - if (!TypeLoaderEnvironment.Instance.TryGetRuntimeMethodHandleComponents(resolver->GVMMethodHandle, out declaringTypeHandleIgnored, out nameAndSignatureIgnored, out genericMethodTypeArgumentHandles)) - throw new MissingRuntimeArtifactException(SR.DelegateGetMethodInfo_NoInstantiation); + if (!TypeLoaderEnvironment.Instance.TryGetRuntimeMethodHandleComponents(resolver->GVMMethodHandle, out _, out _, out genericMethodTypeArgumentHandles)) + throw new MissingMetadataException(SR.DelegateGetMethodInfo_NoInstantiation); } } } @@ -79,9 +77,9 @@ public static MethodInfo GetDelegateMethodInfo(Delegate del) string methodDisplayString = RuntimeAugments.TryGetMethodDisplayStringFromIp(ip); if (methodDisplayString == null) - throw new MissingRuntimeArtifactException(SR.DelegateGetMethodInfo_NoDynamic); + throw new MissingMetadataException(SR.DelegateGetMethodInfo_NoDynamic); else - throw new MissingRuntimeArtifactException(SR.Format(SR.DelegateGetMethodInfo_NoDynamic_WithDisplayString, methodDisplayString)); + throw new MissingMetadataException(SR.Format(SR.DelegateGetMethodInfo_NoDynamic_WithDisplayString, methodDisplayString)); } } MethodBase methodBase = ReflectionCoreExecution.ExecutionDomain.GetMethod(typeOfFirstParameterIfInstanceDelegate, methodHandle, genericMethodTypeArgumentHandles); diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx index dfb2f92a4b27a2..2466dbd5beb013 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx @@ -121,7 +121,7 @@ This operation cannot be carried out because metadata for the following object was removed for performance reasons:\n\n {0}\n\nNo further information is available. Rebuild in debug mode for better information.\n\n - '{0}' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859 + '{0}' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. Cannot bind to the target method because its signature is not compatible with that of the delegate type. @@ -151,7 +151,7 @@ This object cannot be invoked because no code was generated for it: '{0}'. - MakeGenericMethod() cannot create this generic method instantiation because no code was generated for it: '{0}'. + '{0}' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. Dynamic invocation of delegate constructors is not supported on this runtime. diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj index d2c2c954dcf0a2..81302d18af9deb 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System.Private.Reflection.Execution.csproj @@ -51,7 +51,6 @@ - diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System/Reflection/MissingRuntimeArtifactException.cs b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System/Reflection/MissingRuntimeArtifactException.cs deleted file mode 100644 index 3690212c33b3db..00000000000000 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/System/Reflection/MissingRuntimeArtifactException.cs +++ /dev/null @@ -1,25 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -/*============================================================ -** - Type: MissingRuntimeArtifactException -** -==============================================================*/ - -using global::System; - -namespace System.Reflection -{ - internal sealed class MissingRuntimeArtifactException : MemberAccessException - { - public MissingRuntimeArtifactException() - { - } - - public MissingRuntimeArtifactException(string message) - : base(message) - { - } - } -} diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index 702b23a4b608dd..6271c24b86216c 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3957,10 +3957,10 @@ Must not be greater than the length of the buffer. - {0} is missing structure marshalling data. To enable structure marshalling data, add a MarshalStructure directive to the application rd.xml file. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=393965 + '{0}' is missing structure marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. - {0} is missing delegate marshalling data. To enable delegate marshalling data, add a MarshalDelegate directive to the application rd.xml file. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=393965 + '{0}' is missing delegate marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. Type names passed to Assembly.GetType() must not specify an assembly. From a9b9eac8c548a040dfe29c0fee0f898cd7d5f817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Strehovsk=C3=BD?= Date: Tue, 9 Aug 2022 14:15:07 +0900 Subject: [PATCH 2/2] Add aka.ms --- .../src/Resources/Strings.resx | 4 ++-- .../System.Private.CoreLib/src/Resources/Strings.resx | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx index 2466dbd5beb013..e76aecc48eaad7 100644 --- a/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx +++ b/src/coreclr/nativeaot/System.Private.Reflection.Execution/src/Resources/Strings.resx @@ -121,7 +121,7 @@ This operation cannot be carried out because metadata for the following object was removed for performance reasons:\n\n {0}\n\nNo further information is available. Rebuild in debug mode for better information.\n\n - '{0}' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. + '{0}' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility Cannot bind to the target method because its signature is not compatible with that of the delegate type. @@ -151,7 +151,7 @@ This object cannot be invoked because no code was generated for it: '{0}'. - '{0}' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. + '{0}' is missing native code. MethodInfo.MakeGenericMethod() is not compatible with AOT compilation. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility Dynamic invocation of delegate constructors is not supported on this runtime. diff --git a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx index 6271c24b86216c..22490fd75b2a94 100644 --- a/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx +++ b/src/libraries/System.Private.CoreLib/src/Resources/Strings.resx @@ -3957,10 +3957,10 @@ Must not be greater than the length of the buffer. - '{0}' is missing structure marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. + '{0}' is missing structure marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility - '{0}' is missing delegate marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. + '{0}' is missing delegate marshalling data. This can happen for code that is not compatible with AOT. Inspect and fix AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility Type names passed to Assembly.GetType() must not specify an assembly.