Skip to content

Commit 40b5709

Browse files
authored
Merge pull request #81022 from nickolas-pohilets/mpokhylets/fix-80992
Fixed no copying IsIsolated flag when cloning subscript params
2 parents 52d7781 + 22280de commit 40b5709

File tree

3 files changed

+34
-23
lines changed

3 files changed

+34
-23
lines changed

include/swift/AST/Decl.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6997,6 +6997,10 @@ class ParamDecl : public VarDecl {
69976997
/// Create a an identical copy of this ParamDecl.
69986998
static ParamDecl *clone(const ASTContext &Ctx, ParamDecl *PD);
69996999

7000+
static ParamDecl *cloneAccessor(const ASTContext &Ctx,
7001+
ParamDecl const *subscriptParam,
7002+
DeclContext *Parent);
7003+
70007004
static ParamDecl *
70017005
createImplicit(ASTContext &Context, SourceLoc specifierLoc,
70027006
SourceLoc argumentNameLoc, Identifier argumentName,

lib/AST/Decl.cpp

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8919,6 +8919,21 @@ ParamDecl *ParamDecl::clone(const ASTContext &Ctx, ParamDecl *PD) {
89198919
return Clone;
89208920
}
89218921

8922+
ParamDecl *ParamDecl::cloneAccessor(const ASTContext &Ctx,
8923+
ParamDecl const *subscriptParam,
8924+
DeclContext *Parent) {
8925+
auto *param = new (Ctx) ParamDecl(
8926+
subscriptParam->getSpecifierLoc(), subscriptParam->getArgumentNameLoc(),
8927+
subscriptParam->getArgumentName(), subscriptParam->getNameLoc(),
8928+
subscriptParam->getName(), /*declContext*/ Parent);
8929+
param->setOptions(subscriptParam->getOptions());
8930+
8931+
// The cloned parameter is implicit.
8932+
param->setImplicit();
8933+
8934+
return param;
8935+
}
8936+
89228937
ParamDecl *
89238938
ParamDecl::createImplicit(ASTContext &Context, SourceLoc specifierLoc,
89248939
SourceLoc argumentNameLoc, Identifier argumentName,
@@ -11138,23 +11153,7 @@ AccessorDecl *AccessorDecl::createParsed(
1113811153
paramsEnd = indices->getEndLoc();
1113911154
}
1114011155
for (auto *subscriptParam : *indices) {
11141-
// Clone the parameter.
11142-
auto *param = new (ctx) ParamDecl(
11143-
subscriptParam->getSpecifierLoc(),
11144-
subscriptParam->getArgumentNameLoc(),
11145-
subscriptParam->getArgumentName(), subscriptParam->getNameLoc(),
11146-
subscriptParam->getName(), /*declContext*/ accessor);
11147-
param->setAutoClosure(subscriptParam->isAutoClosure());
11148-
11149-
// The cloned parameter is implicit.
11150-
param->setImplicit();
11151-
11152-
if (subscriptParam->isSending())
11153-
param->setSending();
11154-
11155-
if (subscriptParam->isCallerIsolated())
11156-
param->setCallerIsolated();
11157-
11156+
auto param = ParamDecl::cloneAccessor(ctx, subscriptParam, accessor);
1115811157
newParams.push_back(param);
1115911158
}
1116011159

test/Concurrency/isolated_parameters.swift

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

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

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

@@ -591,3 +588,14 @@ public actor MyActorIsolatedParameterMerge {
591588
class ClassWithIsolatedAsyncInitializer {
592589
init(isolation: isolated (any Actor)? = #isolation) async {}
593590
}
591+
592+
// https://github.com/swiftlang/swift/issues/80992
593+
struct WritableActorKeyPath<Root: Actor, Value>: Sendable {
594+
var getter: @Sendable (isolated Root) -> Value
595+
var setter: @Sendable (isolated Root, Value) -> Void
596+
597+
subscript(_ root: isolated Root) -> Value {
598+
get { getter(root) }
599+
nonmutating set { setter(root, newValue) }
600+
}
601+
}

0 commit comments

Comments
 (0)