Skip to content

[IRGen] Add indirect typed error slot when async function has indirec… #78698

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

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/IRGen/GenCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3027,11 +3027,7 @@ class AsyncCallEmission final : public CallEmission {

if (nativeSchema.requiresIndirect() ||
errorSchema.shouldReturnTypedErrorIndirectly() ||
(errorSchema.empty() &&
fnConv.hasIndirectSILResults())) { // direct empty typed errors are
// passed
// indirectly for compatibility with generic
// functions.
fnConv.hasIndirectSILResults()) {
// Return the error indirectly.
auto buf = IGF.getCalleeTypedErrorResultSlot(silErrorTy);
Args[--LastArgWritten] = buf.getAddress();
Expand Down
15 changes: 15 additions & 0 deletions test/IRGen/typed_throws.swift
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,18 @@ enum LargeError: Error {
func callClosureAsyncIndirectError(f: () async throws(LargeError) -> Int) async throws(LargeError) -> Int {
return try await f()
}

protocol AsyncGenProto<A> {
associatedtype A
func fn(arg: Int) async throws(SmallError) -> A
}

// CHECK: define internal swifttailcc void @"$s12typed_throws23callAsyncIndirectResult1p1xxAA0D8GenProto_px1ARts_XP_SitYaAA10SmallErrorVYKlFTY0_"(ptr swiftasync %0)
// CHECK: musttail call swifttailcc void {{%.*}}(ptr noalias {{%.*}}, ptr swiftasync {{%.*}}, i64 {{%.*}}, ptr noalias swiftself {{%.*}}, ptr %swifterror, ptr {{%.*}}, ptr {{%.*}})
// CHECK: ret void
// CHECK: }
@inline(never)
func callAsyncIndirectResult<A>(p: any AsyncGenProto<A>, x: Int) async throws(SmallError) -> A {
return try await p.fn(arg: x)
}

25 changes: 25 additions & 0 deletions test/Interpreter/typed_throws_abi.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,10 +285,35 @@ func checkAsync() async {
await invoke { try await impl.nonMatching_f1(false) }
}

enum MyError: Error {
case x
case y
}

protocol AsyncGenProto<A> {
associatedtype A
func fn(arg: Int) async throws(MyError) -> A
}

@inline(never)
func callAsyncIndirectResult<A>(p: any AsyncGenProto<A>, x: Int) async throws(MyError) -> A {
return try await p.fn(arg: x)
}


struct AsyncGenProtoImpl: AsyncGenProto {
func fn(arg: Int) async throws(MyError) -> Int {
print("Arg is \(arg)")
return arg
}
}

@main
public struct Main {
public static func main() async {
await checkSync()
await checkAsync()
// CHECK: Arg is 10
print(try! await callAsyncIndirectResult(p: AsyncGenProtoImpl(), x: 10))
}
}