Skip to content

[Distributed] ban typed throws in distributed funcs #79043

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 30, 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
3 changes: 3 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5629,6 +5629,9 @@ ERROR(distributed_actor_func_unsupported_specifier, none,
ERROR(distributed_actor_func_variadic, none,
"cannot declare variadic argument %0 in %kind1",
(DeclName, const ValueDecl *))
ERROR(distributed_actor_func_typed_throws, none,
"cannot declare distributed function with typed throws",
())
NOTE(actor_mutable_state,none,
"mutation of this %0 is only permitted within the actor",
(DescriptiveDeclKind))
Expand Down
13 changes: 13 additions & 0 deletions lib/Sema/TypeCheckDistributed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,19 @@ bool CheckDistributedFunctionRequest::evaluate(
return true;
}

// TODO: rdar://136467591 Currently typed throws were not implemented for distributed methods
if (func->hasThrows()) {
if (auto thrownError = func->getEffectiveThrownErrorType()) {
// Basically we only support throwing `any Error` out of a distributed
// function because then the effective error thrown by thunk calls naturally
// is correct and the same `any Error`
if (thrownError.has_value() &&
!(*thrownError)->isEqual(C.getErrorExistentialType())) {
func->diagnose(diag::distributed_actor_func_typed_throws);
}
}
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// RUN: %target-swift-frontend -typecheck -verify -target %target-swift-5.7-abi-triple -I %t 2>&1 %s

// UNSUPPORTED: back_deploy_concurrency
// REQUIRES: concurrency
// REQUIRES: distributed

import Distributed

typealias DefaultDistributedActorSystem = LocalTestingDistributedActorSystem

distributed actor Foo {
distributed func alwaysThrows() throws(FooError) { // expected-error{{cannot declare distributed function with typed throws}}
throw FooError()
}
}

struct FooError: Codable, Error { }
struct RemoteInvocationError: Codable, Error { }

func test(foo: Foo) async throws {
do {
try await foo.alwaysThrows() // actually, this is throws(Error) because network errors etc
fatalError("Should not reach here")
// FIXME: the following warning is showing why we had to ban typed throws;
// the error type must instead be (FooError | Error (from the distributed thunk))
// rdar://136467591
} catch let error as RemoteInvocationError { // expected-warning{{cast from 'FooError' to unrelated type 'RemoteInvocationError' always fails}}
print("error = \(error)")
}
}