From 6e14025b9553fd2fea0a55361dec956415ff63dc Mon Sep 17 00:00:00 2001 From: Konrad `ktoso` Malawski Date: Thu, 25 May 2023 10:53:18 +0200 Subject: [PATCH] [Distributed] Explicitly ban __owned and other specifiers we dont support --- include/swift/AST/DiagnosticsSema.def | 3 +++ lib/Sema/TypeCheckDistributed.cpp | 16 +++++++++++- .../distributed_actor_ban_owned_shared.swift | 25 +++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 test/Distributed/distributed_actor_ban_owned_shared.swift diff --git a/include/swift/AST/DiagnosticsSema.def b/include/swift/AST/DiagnosticsSema.def index 165a78aaf2104..4c2a6a92a7e04 100644 --- a/include/swift/AST/DiagnosticsSema.def +++ b/include/swift/AST/DiagnosticsSema.def @@ -5085,6 +5085,9 @@ ERROR(distributed_actor_func_nonisolated, none, ERROR(distributed_actor_func_inout, none, "cannot declare 'inout' argument %0 in %1 %2", (DeclName, DescriptiveDeclKind, DeclName)) +ERROR(distributed_actor_func_unsupported_specifier, none, + "cannot declare '%0' argument %1 in %2 %3", + (StringRef, DeclName, DescriptiveDeclKind, DeclName)) ERROR(distributed_actor_func_closure, none, "%0 %1 cannot declare closure arguments, as they cannot be serialized", (DescriptiveDeclKind, DeclName)) diff --git a/lib/Sema/TypeCheckDistributed.cpp b/lib/Sema/TypeCheckDistributed.cpp index 58c82ab5f2396..9afdb38c4f184 100644 --- a/lib/Sema/TypeCheckDistributed.cpp +++ b/lib/Sema/TypeCheckDistributed.cpp @@ -535,8 +535,8 @@ bool CheckDistributedFunctionRequest::evaluate( checkDistributedSerializationRequirementIsExactlyCodable( C, serializationRequirements); - // --- Check parameters for 'Codable' conformance for (auto param : *func->getParameters()) { + // --- Check parameters for 'Codable' conformance auto paramTy = func->mapTypeIntoContext(param->getInterfaceType()); for (auto req : serializationRequirements) { @@ -555,6 +555,7 @@ bool CheckDistributedFunctionRequest::evaluate( } } + // --- Check parameters for various illegal modifiers if (param->isInOut()) { param->diagnose( diag::distributed_actor_func_inout, @@ -566,6 +567,19 @@ bool CheckDistributedFunctionRequest::evaluate( return true; } + if (param->getSpecifier() == ParamSpecifier::LegacyShared || + param->getSpecifier() == ParamSpecifier::LegacyOwned || + param->getSpecifier() == ParamSpecifier::Consuming || + param->getSpecifier() == ParamSpecifier::Borrowing) { + param->diagnose( + diag::distributed_actor_func_unsupported_specifier, + ParamDecl::getSpecifierSpelling(param->getSpecifier()), + param->getName(), + func->getDescriptiveKind(), + func->getName()); + return true; + } + if (param->isVariadic()) { param->diagnose( diag::distributed_actor_func_variadic, diff --git a/test/Distributed/distributed_actor_ban_owned_shared.swift b/test/Distributed/distributed_actor_ban_owned_shared.swift new file mode 100644 index 0000000000000..fa6eb64c1289d --- /dev/null +++ b/test/Distributed/distributed_actor_ban_owned_shared.swift @@ -0,0 +1,25 @@ +// RUN: %empty-directory(%t) +// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/Inputs/FakeDistributedActorSystems.swift +// RUN: %target-swift-frontend -typecheck -verify -disable-availability-checking -I %t 2>&1 %s +// REQUIRES: concurrency +// REQUIRES: distributed + +import Distributed +import FakeDistributedActorSystems + +typealias DefaultDistributedActorSystem = FakeActorSystem + +class Param: Codable {} + +distributed actor First { + distributed func owned(_: __owned Param) async throws {} // expected-error{{cannot declare '__owned' argument '_' in distributed instance method 'owned'}} + distributed func shared(_: __shared Param) async throws {} // expected-error{{cannot declare '__shared' argument '_' in distributed instance method 'shared'}} + distributed func consuming(_: consuming Param) async throws {} + // expected-error@-1{{Copyable types cannot be 'consuming' or 'borrowing' yet}} + // expected-error@-2{{parameter '' of type '<>' in distributed instance method does not conform to serialization requirement 'Codable'}} +} + +func test(first: First) async throws { + try await first.owned(.init()) + try await first.shared(.init()) +} \ No newline at end of file