Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[swift interop] Cannot call instance methods with at least one parameter when using SwiftSelf<T> #107946

Closed
Tracked by #108662
jkurdek opened this issue Sep 17, 2024 · 7 comments · Fixed by #108547
Closed
Tracked by #108662
Assignees
Labels
area-System.Runtime.InteropServices in-pr There is an active PR which will close this issue when it is merged os-ios Apple iOS os-tvos Apple tvOS
Milestone

Comments

@jkurdek
Copy link
Member

jkurdek commented Sep 17, 2024

Our current interop implementation only allows SwiftSelf (representing the self parameter in Swift instance methods) to be specified as the first parameter in function signatures. This design choice leads to issues when interfacing with Swift instance methods that accept additional parameters.

Let's consider following example

@frozen public struct SampleStruct
{
    public var x1: Int
    public var x2: Int

    public func addWithExtraArgs(x3: Float, x4: Float) -> Float {
        return Float(x1 + x2) + x3 + x4
    }
}

addWithExtraArgs results in the following LLVM-IR:

define protected swiftcc float @"output.SampleStruct.addWithExtraArgs(x3: Swift.Float, x4: Swift.Float) -> Swift.Float"(float %0, float %1, i64 %2, i64 %3) ...

SampleStruct is expected to be lowered into two i64 which are passed as last parameters.
This would correspond to following csharp signature:

static extern void AddWithExtraArgs(float a, float b, SwiftSelf<SampleStruct> struct)

We should either allow SwiftSelf<T> argument to be specified as the last parameter or we should reorder the parameters in runtime.

\cc: @kotlarmilos, @jakobbotsch, @amanasifkhalid, @matouskozak

Copy link
Contributor

Tagging subscribers to 'os-ios': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

Copy link
Contributor

Tagging subscribers to 'os-tvos': @vitek-karas, @kotlarmilos, @ivanpovazan, @steveisok, @akoeplinger
See info in area-owners.md if you want to be subscribed.

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Sep 17, 2024
@agocke agocke added this to the 10.0.0 milestone Sep 18, 2024
@agocke agocke removed the untriaged New issue has not been triaged by the area owner label Sep 18, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/interop-contrib
See info in area-owners.md if you want to be subscribed.

@jakobbotsch
Copy link
Member

We should either allow SwiftSelf<T> argument to be specified as the last parameter or we should reorder the parameters in runtime.

My impression (from playing around on Godbolt, IIRC) was that the Swift compiler always put this first. Does it actually always put it last? Do you know what the rules are for how the lowering of the self arg is placed?

@jkurdek
Copy link
Member Author

jkurdek commented Oct 4, 2024

I am unable to find any piece of Apple documentation which describes this. Playing around in Godbolt: https://godbolt.org/z/1qj8vsf1h it seems that for both frozen and non-frozen structs it is passed as last argument.

Do you remember any example where it was passed as first argument?

@jakobbotsch
Copy link
Member

I am unable to find any piece of Apple documentation which describes this. Playing around in Godbolt: https://godbolt.org/z/1qj8vsf1h it seems that for both frozen and non-frozen structs it is passed as last argument.

Do you remember any example where it was passed as first argument?

I don't. Looks like we should just switch the requirement to be the last argument then.

@dotnet-policy-service dotnet-policy-service bot added the in-pr There is an active PR which will close this issue when it is merged label Oct 4, 2024
@stephen-hawley
Copy link

I am unable to find any piece of Apple documentation which describes this. Playing around in Godbolt: https://godbolt.org/z/1qj8vsf1h it seems that for both frozen and non-frozen structs it is passed as last argument.
Do you remember any example where it was passed as first argument?

I don't. Looks like we should just switch the requirement to be the last argument then.

It was might have been the case in Swift 2.2 when self was an implicit first argument like C++.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Runtime.InteropServices in-pr There is an active PR which will close this issue when it is merged os-ios Apple iOS os-tvos Apple tvOS
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

6 participants