Skip to content

Commit cf1a946

Browse files
committed
Give ~Escapable _read accessors with borrow scope borrowed ownership
This applies to all _read accessors whose result depends on a borrow of self. In this case, the coroutine defines the borrow scope, and the ~Escapable property value can only be used within that scope. This makes it impossible to synthesize a getter. Returning the ~Escpable value from the getter would always escape the coroutine.
1 parent ec85589 commit cf1a946

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

lib/Sema/TypeCheckStorage.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,18 @@ OpaqueReadOwnershipRequest::evaluate(Evaluator &evaluator,
869869
return OpaqueReadOwnership::Borrowed;
870870
};
871871

872+
if (auto *accessorDecl = storage->getAccessor(AccessorKind::Read)) {
873+
auto lifetimeDependencies = accessorDecl->getLifetimeDependencies();
874+
if (lifetimeDependencies.has_value() && !lifetimeDependencies->empty()) {
875+
for (auto &lifetimeDependenceInfo : *lifetimeDependencies) {
876+
if (lifetimeDependenceInfo.hasScopeLifetimeParamIndices()) {
877+
// A scoped lifetime dependence borrows its source.
878+
return OpaqueReadOwnership::Borrowed;
879+
}
880+
}
881+
}
882+
}
883+
872884
if (storage->getAccessor(AccessorKind::Read2))
873885
return OpaqueReadOwnership::Borrowed;
874886

test/SILGen/accessor_borrow.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-swift-emit-silgen -module-name accessor_borrow -Xllvm -sil-full-demangle %s | %FileCheck %s
2+
3+
struct NE: ~Escapable {}
4+
5+
struct NEContainer: ~Copyable {
6+
// This accessor borrows self. Do not synthesize a getter.
7+
var ne_coroutine: NE {
8+
_read {
9+
NE()
10+
}
11+
}
12+
}

0 commit comments

Comments
 (0)