diff --git a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs index 07ae6ea0420607..9bd809a37fea4b 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs @@ -731,12 +731,12 @@ public static object BindToMoniker(string monikerName) [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern", Justification = "The calling method is annotated with RequiresUnreferencedCode")] - [GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)] + [GeneratedDllImport(Interop.Libraries.Ole32)] private static partial int MkParseDisplayName(IntPtr pbc, [MarshalAs(UnmanagedType.LPWStr)] string szUserName, out uint pchEaten, out IntPtr ppmk); [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2050:UnrecognizedReflectionPattern", Justification = "The calling method is annotated with RequiresUnreferencedCode")] - [GeneratedDllImport(Interop.Libraries.Ole32, PreserveSig = false)] + [GeneratedDllImport(Interop.Libraries.Ole32)] private static partial int BindMoniker(IntPtr pmk, uint grfOpt, ref Guid iidResult, out IntPtr ppvResult); [SupportedOSPlatform("windows")] diff --git a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetUserDefaultLCID.cs b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetUserDefaultLCID.cs index cf41573e2e7922..3b7a9b7cbc38d8 100644 --- a/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetUserDefaultLCID.cs +++ b/src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GetUserDefaultLCID.cs @@ -7,7 +7,7 @@ internal static partial class Interop { internal static partial class Kernel32 { - [GeneratedDllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode, PreserveSig = true)] + [GeneratedDllImport(Interop.Libraries.Kernel32, CharSet = CharSet.Unicode)] internal static partial int GetUserDefaultLCID(); } } diff --git a/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedDllImportAttribute.cs b/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedDllImportAttribute.cs index e64fae1c7ad805..19aeff926a7335 100644 --- a/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedDllImportAttribute.cs +++ b/src/libraries/Common/src/System/Runtime/InteropServices/GeneratedDllImportAttribute.cs @@ -22,14 +22,13 @@ sealed class GeneratedDllImportAttribute : Attribute public CharSet CharSet { get; set; } public string? EntryPoint { get; set; } public bool ExactSpelling { get; set; } - public bool PreserveSig { get; set; } public bool SetLastError { get; set; } public GeneratedDllImportAttribute(string dllName) { - this.Value = dllName; + LibraryName = dllName; } - public string Value { get; private set; } + public string LibraryName { get; private set; } } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportGenerator.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportGenerator.cs index 35b40c4196703e..7d03fd93b12bee 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportGenerator.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportGenerator.cs @@ -21,9 +21,6 @@ namespace Microsoft.Interop [Generator] public sealed class DllImportGenerator : IIncrementalGenerator { - private const string GeneratedDllImport = nameof(GeneratedDllImport); - private const string GeneratedDllImportAttribute = nameof(GeneratedDllImportAttribute); - internal sealed record IncrementalStubGenerationContext( StubEnvironment Environment, DllImportStubContext StubContext, @@ -351,7 +348,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu CharSet charSet = CharSet.Ansi; string? entryPoint = null; bool exactSpelling = false; // VB has different and unusual default behavior here. - bool preserveSig = true; bool setLastError = false; // All other data on attribute is defined as NamedArguments. @@ -360,7 +356,7 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu switch (namedArg.Key) { default: - Debug.Fail($"An unknown member was found on {GeneratedDllImport}"); + Debug.Fail($"An unknown member was found on {attrData.AttributeClass}"); continue; case nameof(GeneratedDllImportData.CharSet): userDefinedValues |= DllImportMember.CharSet; @@ -374,10 +370,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu userDefinedValues |= DllImportMember.ExactSpelling; exactSpelling = (bool)namedArg.Value.Value!; break; - case nameof(GeneratedDllImportData.PreserveSig): - userDefinedValues |= DllImportMember.PreserveSig; - preserveSig = (bool)namedArg.Value.Value!; - break; case nameof(GeneratedDllImportData.SetLastError): userDefinedValues |= DllImportMember.SetLastError; setLastError = (bool)namedArg.Value.Value!; @@ -391,7 +383,6 @@ private static GeneratedDllImportData ProcessGeneratedDllImportAttribute(Attribu CharSet = charSet, EntryPoint = entryPoint, ExactSpelling = exactSpelling, - PreserveSig = preserveSig, SetLastError = setLastError, }; } @@ -584,12 +575,6 @@ private static AttributeSyntax CreateDllImportAttributeForTarget(GeneratedDllImp ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.ExactSpelling); newAttributeArgs.Add(AttributeArgument(name, null, value)); } - if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.PreserveSig)) - { - NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.PreserveSig)); - ExpressionSyntax value = CreateBoolExpressionSyntax(targetDllImportData.PreserveSig); - newAttributeArgs.Add(AttributeArgument(name, null, value)); - } if (targetDllImportData.IsUserDefined.HasFlag(DllImportMember.SetLastError)) { NameEqualsSyntax name = NameEquals(nameof(DllImportAttribute.SetLastError)); @@ -629,9 +614,6 @@ static ExpressionSyntax CreateEnumExpressionSyntax(T value) where T : Enum private static GeneratedDllImportData GetTargetDllImportDataFromStubData(GeneratedDllImportData dllImportData, string originalMethodName, bool forwardAll) { DllImportMember membersToForward = DllImportMember.All - // https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.dllimportattribute.preservesig - // If PreserveSig=false (default is true), the P/Invoke stub checks/converts a returned HRESULT to an exception. - & ~DllImportMember.PreserveSig // https://docs.microsoft.com/dotnet/api/system.runtime.interopservices.dllimportattribute.setlasterror // If SetLastError=true (default is false), the P/Invoke stub gets/caches the last error after invoking the native function. & ~DllImportMember.SetLastError; @@ -646,7 +628,6 @@ private static GeneratedDllImportData GetTargetDllImportDataFromStubData(Generat EntryPoint = dllImportData.EntryPoint, ExactSpelling = dllImportData.ExactSpelling, SetLastError = dllImportData.SetLastError, - PreserveSig = dllImportData.PreserveSig, IsUserDefined = dllImportData.IsUserDefined & membersToForward }; diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs index 69da1c04c06af5..1f6e7f16e09a9f 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/DllImportStubContext.cs @@ -218,35 +218,8 @@ private static (ImmutableArray, IMarshallingGeneratorFactory) IMarshallingGeneratorFactory elementFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled)); // We don't need to include the later generator factories for collection elements - // as the later generator factories only apply to parameters or to the synthetic return value for PreserveSig support. + // as the later generator factories only apply to parameters. generatorFactory = new AttributedMarshallingModelGeneratorFactory(generatorFactory, elementFactory, new AttributedMarshallingModelOptions(options, runtimeMarshallingDisabled)); - if (!dllImportData.PreserveSig) - { - // Create type info for native out param - if (!method.ReturnsVoid) - { - // Transform the managed return type info into an out parameter and add it as the last param - TypePositionInfo nativeOutInfo = retTypeInfo with - { - InstanceIdentifier = PInvokeStubCodeGenerator.ReturnIdentifier, - RefKind = RefKind.Out, - RefKindSyntax = SyntaxKind.OutKeyword, - ManagedIndex = TypePositionInfo.ReturnIndex, - NativeIndex = typeInfos.Count - }; - typeInfos.Add(nativeOutInfo); - } - - // Use a marshalling generator that supports the HRESULT return->exception marshalling. - generatorFactory = new NoPreserveSigMarshallingGeneratorFactory(generatorFactory); - - // Create type info for native HRESULT return - retTypeInfo = new TypePositionInfo(SpecialTypeInfo.Int32, NoMarshallingInfo.Instance); - retTypeInfo = retTypeInfo with - { - NativeIndex = TypePositionInfo.ReturnIndex - }; - } generatorFactory = new ByValueContentsMarshalKindValidator(generatorFactory); } diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/GeneratedDllImportData.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/GeneratedDllImportData.cs index 07d0ddeb1ab74b..bf1d5edc940630 100644 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/GeneratedDllImportData.cs +++ b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/GeneratedDllImportData.cs @@ -18,8 +18,7 @@ public enum DllImportMember CharSet = 1 << 0, EntryPoint = 1 << 1, ExactSpelling = 1 << 2, - PreserveSig = 1 << 3, - SetLastError = 1 << 4, + SetLastError = 1 << 3, All = ~None } @@ -39,7 +38,6 @@ public sealed record GeneratedDllImportData(string ModuleName) public CharSet CharSet { get; init; } public string? EntryPoint { get; init; } public bool ExactSpelling { get; init; } - public bool PreserveSig { get; init; } public bool SetLastError { get; init; } } } diff --git a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/NoPreserveSigMarshallingGeneratorFactory.cs b/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/NoPreserveSigMarshallingGeneratorFactory.cs deleted file mode 100644 index d06e7db251cfd6..00000000000000 --- a/src/libraries/System.Runtime.InteropServices/gen/DllImportGenerator/NoPreserveSigMarshallingGeneratorFactory.cs +++ /dev/null @@ -1,33 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.Diagnostics; -using System; -using System.Collections.Generic; -using System.Text; - -namespace Microsoft.Interop -{ - internal class NoPreserveSigMarshallingGeneratorFactory : IMarshallingGeneratorFactory - { - private static readonly HResultExceptionMarshaller s_hResultException = new HResultExceptionMarshaller(); - private readonly IMarshallingGeneratorFactory _inner; - - public NoPreserveSigMarshallingGeneratorFactory(IMarshallingGeneratorFactory inner) - { - _inner = inner; - } - - public IMarshallingGenerator Create(TypePositionInfo info, StubCodeContext context) - { - if (info.IsNativeReturnPosition && !info.IsManagedReturnPosition) - { - // Use marshaller for native HRESULT return / exception throwing - System.Diagnostics.Debug.Assert(info.ManagedType.Equals(SpecialTypeInfo.Int32)); - return s_hResultException; - } - return _inner.Create(info, context); - } - } -} diff --git a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/HResultExceptionMarshaller.cs b/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/HResultExceptionMarshaller.cs deleted file mode 100644 index cb32ee3b33cc70..00000000000000 --- a/src/libraries/System.Runtime.InteropServices/gen/Microsoft.Interop.SourceGeneration/Marshalling/HResultExceptionMarshaller.cs +++ /dev/null @@ -1,53 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Diagnostics; -using System.Collections.Generic; - -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; -using Microsoft.CodeAnalysis.CSharp.Syntax; -using static Microsoft.CodeAnalysis.CSharp.SyntaxFactory; - -namespace Microsoft.Interop -{ - public sealed class HResultExceptionMarshaller : IMarshallingGenerator - { - private static readonly TypeSyntax s_nativeType = PredefinedType(Token(SyntaxKind.IntKeyword)); - - public bool IsSupported(TargetFramework target, Version version) => true; - - public TypeSyntax AsNativeType(TypePositionInfo info) - { - Debug.Assert(info.ManagedType is SpecialTypeInfo(_, _, SpecialType.System_Int32)); - return s_nativeType; - } - - // Should only be used for return value - public ParameterSyntax AsParameter(TypePositionInfo info) => throw new InvalidOperationException(); - public ArgumentSyntax AsArgument(TypePositionInfo info, StubCodeContext context) => throw new InvalidOperationException(); - - public IEnumerable Generate(TypePositionInfo info, StubCodeContext context) - { - if (context.CurrentStage != StubCodeContext.Stage.Unmarshal) - yield break; - - // Marshal.ThrowExceptionForHR() - string identifier = context.GetIdentifiers(info).managed; - yield return ExpressionStatement( - InvocationExpression( - MemberAccessExpression( - SyntaxKind.SimpleMemberAccessExpression, - MarshallerHelpers.InteropServicesMarshalType, - IdentifierName(nameof(System.Runtime.InteropServices.Marshal.ThrowExceptionForHR))), - ArgumentList(SingletonSeparatedList( - Argument(IdentifierName(identifier)))))); - } - - public bool UsesNativeIdentifier(TypePositionInfo info, StubCodeContext context) => false; - - public bool SupportsByValueMarshalKind(ByValueContentsMarshalKind marshalKind, StubCodeContext context) => false; - } - -} diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/PreserveSigTests.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/PreserveSigTests.cs deleted file mode 100644 index ee1f351e66f9fc..00000000000000 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.Tests/PreserveSigTests.cs +++ /dev/null @@ -1,267 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System; -using System.Runtime.InteropServices; - -using Xunit; - -namespace DllImportGenerator.IntegrationTests -{ - partial class NativeExportsNE - { - public partial class PreserveSig - { - public partial class False - { - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_return", PreserveSig = false)] - public static partial void NoReturnValue(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int", PreserveSig = false)] - public static partial void Int_Out(int i, out int ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int", PreserveSig = false)] - public static partial int Int_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int", PreserveSig = false)] - public static partial void Bool_Out(int i, [MarshalAs(UnmanagedType.U4)] out bool ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int", PreserveSig = false)] - [return: MarshalAs(UnmanagedType.U4)] - public static partial bool Bool_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort", PreserveSig = false)] - public static partial void Char_Out(int i, [MarshalAs(UnmanagedType.U2)] out char ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort", PreserveSig = false)] - [return: MarshalAs(UnmanagedType.U2)] - public static partial char Char_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort_string", PreserveSig = false)] - public static partial void String_Out(int i, [MarshalAs(UnmanagedType.LPWStr)] out string ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort_string", PreserveSig = false)] - [return: MarshalAs(UnmanagedType.LPWStr)] - public static partial string String_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int_array", PreserveSig = false)] - public static partial void IntArray_Out(int i, [MarshalAs(UnmanagedType.LPArray, SizeConst = sizeof(int))] out int[] ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int_array", PreserveSig = false)] - [return: MarshalAs(UnmanagedType.LPArray, SizeConst = sizeof(int))] - public static partial int[] IntArray_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort_string_array", PreserveSig = false)] - public static partial void StringArray_Out(int i, [MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeConst = sizeof(int))] out string[] ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_ushort_string_array", PreserveSig = false)] - [return: MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.LPWStr, SizeConst = sizeof(int))] - public static partial string[] StringArray_AsReturn(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_handle", PreserveSig = false)] - public static partial void SafeHandle_Out(int hr, out DummySafeHandle ret); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_handle", PreserveSig = false)] - public static partial DummySafeHandle SafeHandle_AsReturn(int hr); - - } - - public class DummySafeHandle : Microsoft.Win32.SafeHandles.SafeHandleMinusOneIsInvalid - { - private DummySafeHandle() : base(ownsHandle: true) { } - protected override bool ReleaseHandle() => true; - } - - public partial class True - { - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_return", PreserveSig = true)] - public static partial int NoReturnValue(int i); - - [GeneratedDllImport(NativeExportsNE_Binary, EntryPoint = "hresult_out_int", PreserveSig = true)] - public static partial int Int_Out(int i, out int ret); - } - } - } - - public class PreserveSigTests - { - private const int E_INVALIDARG = unchecked((int)0x80070057); - private const int COR_E_NOTSUPPORTED = unchecked((int)0x80131515); - private const int S_OK = 0; - private const int S_FALSE = 1; - - [Theory] - [InlineData(E_INVALIDARG)] - [InlineData(COR_E_NOTSUPPORTED)] - [InlineData(-1)] - public void PreserveSigFalse_Error(int input) - { - Exception exception = Marshal.GetExceptionForHR(input); - Assert.NotNull(exception); - - int expectedHR = input; - var exceptionType = exception.GetType(); - Assert.Equal(expectedHR, exception.HResult); - Exception ex; - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.NoReturnValue(input)); - Assert.Equal(expectedHR, ex.HResult); - - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Int_Out(input, out int ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Int_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Bool_Out(input, out bool ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Bool_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Char_Out(input, out char ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.Char_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.String_Out(input, out string ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.String_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.IntArray_Out(input, out int[] ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.IntArray_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.StringArray_Out(input, out string[] ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.StringArray_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - { - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.SafeHandle_Out(input, out NativeExportsNE.PreserveSig.DummySafeHandle ret)); - Assert.Equal(expectedHR, ex.HResult); - - ex = Assert.Throws(exceptionType, () => NativeExportsNE.PreserveSig.False.SafeHandle_AsReturn(input)); - Assert.Equal(expectedHR, ex.HResult); - } - } - - [Theory] - [InlineData(S_OK)] - [InlineData(S_FALSE)] - [InlineData(10)] - public void PreserveSigFalse_Success(int input) - { - Assert.True(input >= 0); - - NativeExportsNE.PreserveSig.False.NoReturnValue(input); - - { - int expected = input; - - int ret; - NativeExportsNE.PreserveSig.False.Int_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.Int_AsReturn(input); - Assert.Equal(expected, ret); - } - { - bool expected = input != 0; - - bool ret; - NativeExportsNE.PreserveSig.False.Bool_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.Bool_AsReturn(input); - Assert.Equal(expected, ret); - } - { - char expected = (char)input; - - char ret; - NativeExportsNE.PreserveSig.False.Char_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.Char_AsReturn(input); - Assert.Equal(expected, ret); - } - { - string expected = input.ToString(); - - string ret; - NativeExportsNE.PreserveSig.False.String_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.String_AsReturn(input); - Assert.Equal(expected, ret); - } - { - int[] expected = new int[sizeof(int)]; - Array.Fill(expected, input); - - int[] ret; - NativeExportsNE.PreserveSig.False.IntArray_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.IntArray_AsReturn(input); - Assert.Equal(expected, ret); - } - { - string[] expected = new string[sizeof(int)]; - Array.Fill(expected, input.ToString()); - - string[] ret; - NativeExportsNE.PreserveSig.False.StringArray_Out(input, out ret); - Assert.Equal(expected, ret); - - ret = NativeExportsNE.PreserveSig.False.StringArray_AsReturn(input); - Assert.Equal(expected, ret); - } - { - nint expected = input; - - NativeExportsNE.PreserveSig.DummySafeHandle ret; - NativeExportsNE.PreserveSig.False.SafeHandle_Out(input, out ret); - Assert.Equal(expected, (nint)ret.DangerousGetHandle()); - ret.Dispose(); - - ret = NativeExportsNE.PreserveSig.False.SafeHandle_AsReturn(input); - Assert.Equal(expected, (nint)ret.DangerousGetHandle()); - ret.Dispose(); - } - } - - [Theory] - [InlineData(S_OK)] - [InlineData(S_FALSE)] - [InlineData(E_INVALIDARG)] - [InlineData(COR_E_NOTSUPPORTED)] - [InlineData(-1)] - public void PreserveSigTrue(int input) - { - int expected = input; - int hr; - - hr = NativeExportsNE.PreserveSig.True.NoReturnValue(input); - Assert.Equal(expected, hr); - - int ret; - hr = NativeExportsNE.PreserveSig.True.Int_Out(input, out ret); - Assert.Equal(expected, hr); - Assert.Equal(expected, ret); - } - } -} diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CodeSnippets.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CodeSnippets.cs index 6f13bf6f10aada..535000d886f872 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CodeSnippets.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CodeSnippets.cs @@ -194,7 +194,6 @@ partial class Test CharSet = CharSet.Unicode, EntryPoint = ""UserDefinedEntryPoint"", ExactSpelling = true, - PreserveSig = false, SetLastError = true)] public static partial void Method(); } @@ -217,7 +216,6 @@ partial class Test CharSet = (CharSet)2, EntryPoint = EntryPointName, ExactSpelling = 0 != 1, - PreserveSig = IsTrue, SetLastError = IsFalse)] public static partial void Method1(); @@ -225,7 +223,6 @@ partial class Test CharSet = (CharSet)Two, EntryPoint = EntryPointName, ExactSpelling = One != Two, - PreserveSig = !IsFalse, SetLastError = !IsTrue)] public static partial void Method2(); } @@ -491,29 +488,15 @@ public static partial MyEnum Method( /// /// Declaration with PreserveSig = false. /// - public static string PreserveSigFalse(string typeName) => @$" + public static string SetLastErrorTrue(string typeName) => @$" using System.Runtime.InteropServices; partial class Test {{ - [GeneratedDllImport(""DoesNotExist"", PreserveSig = false)] - public static partial {typeName} Method1(); - - [GeneratedDllImport(""DoesNotExist"", PreserveSig = false)] - public static partial {typeName} Method2({typeName} p); + [GeneratedDllImport(""DoesNotExist"", SetLastError = true)] + public static partial {typeName} Method({typeName} p); }}"; - public static string PreserveSigFalse() => PreserveSigFalse(typeof(T).ToString()); - - /// - /// Declaration with PreserveSig = false and void return. - /// - public static readonly string PreserveSigFalseVoidReturn = @$" -using System.Runtime.InteropServices; -partial class Test -{{ - [GeneratedDllImport(""DoesNotExist"", PreserveSig = false)] - public static partial void Method(); -}}"; + public static string SetLastErrorTrue() => SetLastErrorTrue(typeof(T).ToString()); public static string DelegateParametersAndModifiers = BasicParametersAndModifiers("MyDelegate") + @" delegate int MyDelegate(int a);"; @@ -584,22 +567,6 @@ public static partial void Method( }}"; public static string MarshalAsArrayParameterWithNestedMarshalInfo(UnmanagedType nestedMarshalType, string preDeclaration = "") => MarshalAsArrayParameterWithNestedMarshalInfo(typeof(T).ToString(), nestedMarshalType, preDeclaration); - - public static string ArrayPreserveSigFalse(string elementType, string preDeclaration = "") => $@" -using System.Runtime.InteropServices; -{preDeclaration} -partial class Test -{{ - [GeneratedDllImport(""DoesNotExist"", PreserveSig = false)] - [return:MarshalAs(UnmanagedType.LPArray, SizeConst=10)] - public static partial {elementType}[] Method1(); - - [GeneratedDllImport(""DoesNotExist"", PreserveSig = false)] - [return:MarshalAs(UnmanagedType.LPArray, SizeParamIndex=0)] - public static partial {elementType}[] Method2(int i); -}}"; - - public static string ArrayPreserveSigFalse(string preDeclaration = "") => ArrayPreserveSigFalse(typeof(T).ToString(), preDeclaration); /// /// Declaration with parameters with MarshalAs. @@ -1133,7 +1100,7 @@ partial class Test out int pOutSize ); }}"; - + public static string CustomCollectionWithMarshaller(bool enableDefaultMarshalling) { string nativeMarshallingAttribute = enableDefaultMarshalling ? "[NativeMarshalling(typeof(Marshaller<>))]" : string.Empty; diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CompileFails.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CompileFails.cs index 48b35d68f66c66..1d64e741882c95 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CompileFails.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/CompileFails.cs @@ -24,8 +24,6 @@ public static IEnumerable CodeSnippetsToCompile() yield return new object[] { CodeSnippets.BasicParametersAndModifiers(), 5, 0 }; yield return new object[] { CodeSnippets.BasicParametersAndModifiers(), 5, 0 }; yield return new object[] { CodeSnippets.BasicParametersAndModifiers(), 5, 0 }; - yield return new object[] { CodeSnippets.PreserveSigFalse(), 3, 0 }; - yield return new object[] { CodeSnippets.PreserveSigFalse(), 3, 0 }; // Unsupported CharSet yield return new object[] { CodeSnippets.BasicParametersAndModifiersWithCharSet(CharSet.Auto), 5, 0 }; diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs index ca59b242e36c8d..edde539be5c36b 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/Compiles.cs @@ -145,36 +145,6 @@ public static IEnumerable CodeSnippetsToCompile() yield return new[] { CodeSnippets.SafeHandleWithCustomDefaultConstructorAccessibility(privateCtor: false) }; yield return new[] { CodeSnippets.SafeHandleWithCustomDefaultConstructorAccessibility(privateCtor: true) }; - // PreserveSig - yield return new[] { CodeSnippets.PreserveSigFalseVoidReturn }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; - yield return new[] { CodeSnippets.PreserveSigFalse("Microsoft.Win32.SafeHandles.SafeFileHandle") }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - yield return new[] { CodeSnippets.ArrayPreserveSigFalse(CodeSnippets.DisableRuntimeMarshalling) }; - // Custom type marshalling yield return new[] { CodeSnippets.CustomStructMarshallingParametersAndModifiers }; yield return new[] { CodeSnippets.CustomStructMarshallingStackallocParametersAndModifiersNoRef }; @@ -404,7 +374,7 @@ public static IEnumerable SnippetsWithBlittableTypesButNonBlittableDat { yield return new[] { CodeSnippets.AllGeneratedDllImportNamedArguments }; yield return new[] { CodeSnippets.BasicParametersAndModifiers() }; - yield return new[] { CodeSnippets.PreserveSigFalse() }; + yield return new[] { CodeSnippets.SetLastErrorTrue() }; } [ConditionalTheory] diff --git a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/ConvertToGeneratedDllImportAnalyzerTests.cs b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/ConvertToGeneratedDllImportAnalyzerTests.cs index 9eb49b66edddd9..cffb7c46bb2684 100644 --- a/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/ConvertToGeneratedDllImportAnalyzerTests.cs +++ b/src/libraries/System.Runtime.InteropServices/tests/DllImportGenerator.UnitTests/ConvertToGeneratedDllImportAnalyzerTests.cs @@ -6,7 +6,6 @@ using System.Runtime.InteropServices; using System.Threading.Tasks; -using Microsoft.CodeAnalysis; using Xunit; using static Microsoft.Interop.Analyzers.ConvertToGeneratedDllImportAnalyzer; @@ -98,30 +97,6 @@ await VerifyCS.VerifyAnalyzerAsync( .WithArguments("Method_Ref")); } - [ConditionalFact] - public async Task PreserveSigFalse_ReportsDiagnostic() - { - string source = @$" -using System.Runtime.InteropServices; -partial class Test -{{ - [DllImport(""DoesNotExist"", PreserveSig = false)] - public static extern void {{|#0:Method1|}}(); - - [DllImport(""DoesNotExist"", PreserveSig = true)] - public static extern void {{|#1:Method2|}}(); -}} -"; - await VerifyCS.VerifyAnalyzerAsync( - source, - VerifyCS.Diagnostic(ConvertToGeneratedDllImport) - .WithLocation(0) - .WithArguments("Method1"), - VerifyCS.Diagnostic(ConvertToGeneratedDllImport) - .WithLocation(1) - .WithArguments("Method2")); - } - [ConditionalFact] public async Task SetLastErrorTrue_ReportsDiagnostic() {