Skip to content

Commit e4d2d51

Browse files
committed
[Concurrency] memberAccessHasSpecialPermissionInSwift5 should treat init accessors as stored properties
Init accessor properties cannot escape self and are only allowed to initialized and access a set of properties declared in their `@storageRestrictions(...)` attribute. Resolves: #70550
1 parent 747cbfc commit e4d2d51

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,10 +1763,20 @@ static bool memberAccessHasSpecialPermissionInSwift5(
17631763

17641764
// If the context in which we consider the access matches between the
17651765
// old (escaping-use restriction) and new (flow-isolation) contexts,
1766-
// and it is a stored property, then permit it here without any warning.
1766+
// and it is a stored or init accessor property, then permit it here
1767+
// without any warning.
17671768
// Later, flow-isolation pass will check and emit a warning if needed.
1768-
if (refCxt == oldFn && isStoredProperty(member))
1769-
return true;
1769+
if (refCxt == oldFn) {
1770+
if (isStoredProperty(member))
1771+
return true;
1772+
1773+
if (auto *var = dyn_cast<VarDecl>(member)) {
1774+
// Init accessor properties are permitted to access only stored
1775+
// properties.
1776+
if (var->hasInitAccessor())
1777+
return true;
1778+
}
1779+
}
17701780

17711781
// Otherwise, it's definitely going to be illegal, so warn and permit.
17721782
auto &diags = refCxt->getASTContext().Diags;

test/Concurrency/flow_isolation.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,3 +734,28 @@ actor CheckDeinitFromActor {
734734
ns = nil // expected-warning {{cannot access property 'ns' with a non-sendable type 'NonSendableType?' from non-isolated deinit; this is an error in Swift 6}}
735735
}
736736
}
737+
738+
// https://github.com/apple/swift/issues/70550
739+
func testActorWithInitAccessorInit() {
740+
@available(SwiftStdlib 5.1, *)
741+
actor Angle {
742+
var degrees: Double
743+
var radians: Double = 0 {
744+
@storageRestrictions(initializes: degrees)
745+
init(initialValue) {
746+
degrees = initialValue * 180 / .pi
747+
}
748+
749+
get { degrees * .pi / 180 }
750+
set { degrees = newValue * 180 / .pi }
751+
}
752+
753+
init(degrees: Double) {
754+
self.degrees = degrees // Ok
755+
}
756+
757+
init(radians: Double) {
758+
self.radians = radians // Ok
759+
}
760+
}
761+
}

0 commit comments

Comments
 (0)