diff --git a/src/coreclr/jit/importercalls.cpp b/src/coreclr/jit/importercalls.cpp index 2e18cd9015028..a9aa37830949f 100644 --- a/src/coreclr/jit/importercalls.cpp +++ b/src/coreclr/jit/importercalls.cpp @@ -2226,9 +2226,9 @@ void Compiler::impPopArgsForSwiftCall(GenTreeCall* call, CORINFO_SIG_INFO* sig, BADCODE("Duplicate SwiftSelf parameter"); } - if (argIndex != 0) + if (argIndex != (sig->numArgs - 1)) { - BADCODE("SwiftSelf must be the first argument in the signature"); + BADCODE("SwiftSelf must be the last argument in the signature"); } selfType = info.compCompHnd->getTypeInstantiationArgument(argClass, 0); diff --git a/src/mono/mono/metadata/marshal.c b/src/mono/mono/metadata/marshal.c index 0ad51eae802a9..f9aa95467519f 100644 --- a/src/mono/mono/metadata/marshal.c +++ b/src/mono/mono/metadata/marshal.c @@ -3714,9 +3714,9 @@ mono_marshal_get_native_wrapper (MonoMethod *method, gboolean check_exceptions, break; } else if (param_klass == swift_error || param_klass == swift_error_ptr) { swift_error_args++; - } else if (param_gklass && (param_gklass->container_class == swift_self_t) && i > 0) { + } else if (param_gklass && (param_gklass->container_class == swift_self_t) && (i != method->signature->param_count - 1)) { swift_error_args = swift_self_args = 0; - mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "SwiftSelf must be the first argument in the signature."); + mono_error_set_generic_error (emitted_error, "System", "InvalidProgramException", "SwiftSelf must be the last argument in the signature."); break; } else if (param_gklass && (param_gklass->container_class == swift_self_t) && m_type_is_byref (method->signature->params [i])) { swift_error_args = swift_self_args = 0; diff --git a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.cs b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.cs index 8899985b99ac1..04b9b1efb6c2c 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.cs +++ b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.cs @@ -61,11 +61,11 @@ public struct FrozenNonEnregisteredStruct } [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] - [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV3Sums5Int64VyF")] + [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV3sums5Int64VyF")] public static extern long SumFrozenEnregisteredStruct(SwiftSelf self); [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] - [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV3Sums5Int64VyF")] + [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV3sums5Int64VyF")] public static extern long SumFrozenNonEnregisteredStruct(SwiftSelf self); [Fact] @@ -81,4 +81,27 @@ public unsafe static void TestSelfIsFrozenNonEnregisteredStruct() long sum = SumFrozenNonEnregisteredStruct(new SwiftSelf(new FrozenNonEnregisteredStruct { A = 10, B = 20, C = 30, D = 40, E = 50 })); Assert.Equal(150, sum); } + + [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] + [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext24FrozenEnregisteredStructV16sumWithExtraArgs1c1dS2f_SftF")] + public static extern float SumFrozenEnregisteredStructWithExtraArgs(float c, float d, SwiftSelf self); + + [UnmanagedCallConv(CallConvs = new Type[] { typeof(CallConvSwift) })] + [DllImport(SwiftLib, EntryPoint = "$s16SwiftSelfContext27FrozenNonEnregisteredStructV16sumWithExtraArgs1f1gS2f_SftF")] + public static extern float SumFrozenNonEnregisteredStructWithExtraArgs(float f, float g, SwiftSelf self); + + [Fact] + public unsafe static void TestSelfIsFrozenEnregisteredStructWithExtraArgs() + { + float sum = SumFrozenEnregisteredStructWithExtraArgs(3f, 4f, new SwiftSelf(new FrozenEnregisteredStruct { A = 10, B = 20 })); + Assert.Equal(37f, sum); + } + + [Fact] + [SkipOnMono("https://github.com/dotnet/runtime/issues/108855")] + public unsafe static void TestSelfIsFrozenNonEnregisteredStructWithExtraArgs() + { + float sum = SumFrozenNonEnregisteredStructWithExtraArgs(3f, 4f, new SwiftSelf(new FrozenNonEnregisteredStruct { A = 10, B = 20, C = 30, D = 40, E = 50 })); + Assert.Equal(157f, sum); + } } diff --git a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.swift b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.swift index 92d26668367ca..1b01e3f06db2f 100644 --- a/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.swift +++ b/src/tests/Interop/Swift/SwiftSelfContext/SwiftSelfContext.swift @@ -26,9 +26,13 @@ public struct FrozenEnregisteredStruct let a : Int64; let b : Int64; - public func Sum() -> Int64 { + public func sum() -> Int64 { return a + b } + + public func sumWithExtraArgs(c: Float, d: Float) -> Float { + return Float(a + b) + c + d + } } @frozen @@ -39,7 +43,11 @@ public struct FrozenNonEnregisteredStruct { let d : Int64; let e : Int64; - public func Sum() -> Int64 { + public func sum() -> Int64 { return a + b + c + d + e } -} + + public func sumWithExtraArgs(f: Float, g: Float) -> Float { + return Float(a + b + c + d + e) + f + g + } +} \ No newline at end of file