Skip to content

Commit b6ffffb

Browse files
authored
Merge pull request #77707 from atrick/read_borrow
Give ~Escapable _read accessors with borrow scope borrowed ownership
2 parents 2337d92 + 987a6c9 commit b6ffffb

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-swift-emit-silgen -module-name accessor_borrow \
2+
// RUN: -enable-experimental-feature LifetimeDependence \
3+
// RUN: %s | %FileCheck %s
4+
5+
// REQUIRES: swift_feature_LifetimeDependence
6+
7+
struct NE: ~Escapable {}
8+
9+
struct NEContainer: ~Copyable {
10+
// This accessor borrows self. Do not synthesize a getter.
11+
//
12+
// Check NEContainer.ne_coroutine.read
13+
// CHECK-LABEL: sil hidden [ossa] @$s15accessor_borrow11NEContainerV12ne_coroutineAA2NEVvr : $@yield_once @convention(method) (@guaranteed NEContainer) -> @lifetime(borrow 0) @yields @guaranteed NE {
14+
//
15+
// Do not synthesize NEContainer.ne_coroutine.getter
16+
// CHECK-NOT: $s15accessor_borrow11NEContainerV12ne_coroutineAA2NEVvg
17+
var ne_coroutine: NE {
18+
_read {
19+
yield NE()
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)