Skip to content

Fixed no copying IsIsolated flag when cloning subscript params #81022

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 2 commits into from
May 14, 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
4 changes: 4 additions & 0 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6993,6 +6993,10 @@ class ParamDecl : public VarDecl {
/// Create a an identical copy of this ParamDecl.
static ParamDecl *clone(const ASTContext &Ctx, ParamDecl *PD);

static ParamDecl *cloneAccessor(const ASTContext &Ctx,
ParamDecl const *subscriptParam,
DeclContext *Parent);

static ParamDecl *
createImplicit(ASTContext &Context, SourceLoc specifierLoc,
SourceLoc argumentNameLoc, Identifier argumentName,
Expand Down
33 changes: 16 additions & 17 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8888,6 +8888,21 @@ ParamDecl *ParamDecl::clone(const ASTContext &Ctx, ParamDecl *PD) {
return Clone;
}

ParamDecl *ParamDecl::cloneAccessor(const ASTContext &Ctx,
ParamDecl const *subscriptParam,
DeclContext *Parent) {
auto *param = new (Ctx) ParamDecl(
subscriptParam->getSpecifierLoc(), subscriptParam->getArgumentNameLoc(),
subscriptParam->getArgumentName(), subscriptParam->getNameLoc(),
subscriptParam->getName(), /*declContext*/ Parent);
param->setOptions(subscriptParam->getOptions());

// The cloned parameter is implicit.
param->setImplicit();

return param;
}

ParamDecl *
ParamDecl::createImplicit(ASTContext &Context, SourceLoc specifierLoc,
SourceLoc argumentNameLoc, Identifier argumentName,
Expand Down Expand Up @@ -11107,23 +11122,7 @@ AccessorDecl *AccessorDecl::createParsed(
paramsEnd = indices->getEndLoc();
}
for (auto *subscriptParam : *indices) {
// Clone the parameter.
auto *param = new (ctx) ParamDecl(
subscriptParam->getSpecifierLoc(),
subscriptParam->getArgumentNameLoc(),
subscriptParam->getArgumentName(), subscriptParam->getNameLoc(),
subscriptParam->getName(), /*declContext*/ accessor);
param->setAutoClosure(subscriptParam->isAutoClosure());

// The cloned parameter is implicit.
param->setImplicit();

if (subscriptParam->isSending())
param->setSending();

if (subscriptParam->isCallerIsolated())
param->setCallerIsolated();

auto param = ParamDecl::cloneAccessor(ctx, subscriptParam, accessor);
newParams.push_back(param);
}

Expand Down
20 changes: 14 additions & 6 deletions test/Concurrency/isolated_parameters.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

@available(SwiftStdlib 5.1, *)
actor A {
func f() { } // expected-typechecker-note 5{{calls to instance method 'f()' from outside of its actor context are implicitly asynchronous}}
func f() { } // expected-typechecker-note 3{{calls to instance method 'f()' from outside of its actor context are implicitly asynchronous}}
}

@available(SwiftStdlib 5.1, *)
Expand Down Expand Up @@ -364,11 +364,8 @@ func isolatedClosures() {
// expected-typechecker-warning@+2 {{cannot have more than one 'isolated' parameter; this is an error in the Swift 6 language mode}}
// expected-typechecker-warning@+1 {{subscript with 'isolated' parameter cannot be 'nonisolated'; this is an error in the Swift 6 language mode}}{{3-15=}}
nonisolated subscript(_ a: isolated A, _ b: isolated A) -> Int {
// FIXME: wrong isolation. should be isolated to `a`.
#if ALLOW_TYPECHECKER_ERRORS
a.f() // expected-typechecker-error {{call to actor-isolated instance method 'f()' in a synchronous actor-isolated context}}
b.f() // expected-typechecker-error {{call to actor-isolated instance method 'f()' in a synchronous actor-isolated context}}
#endif
a.f()
b.f()
return 0
}

Expand Down Expand Up @@ -591,3 +588,14 @@ public actor MyActorIsolatedParameterMerge {
class ClassWithIsolatedAsyncInitializer {
init(isolation: isolated (any Actor)? = #isolation) async {}
}

// https://github.com/swiftlang/swift/issues/80992
struct WritableActorKeyPath<Root: Actor, Value>: Sendable {
var getter: @Sendable (isolated Root) -> Value
var setter: @Sendable (isolated Root, Value) -> Void

subscript(_ root: isolated Root) -> Value {
get { getter(root) }
nonmutating set { setter(root, newValue) }
}
}