Skip to content

Commit 16715ad

Browse files
committed
Add a simple borrow accessor end to end test
1 parent 4f1bfb2 commit 16715ad

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

include/swift/SIL/MemAccessUtils.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,9 +1811,11 @@ Result AccessUseDefChainVisitor<Impl, Result>::visit(SILValue sourceAddr) {
18111811
return asImpl().visitUnidentified(sourceAddr);
18121812

18131813
if (isGuaranteedAddressReturn(sourceAddr)) {
1814-
return asImpl().visitAccessProjection(
1815-
cast<ApplyInst>(sourceAddr),
1816-
&cast<ApplyInst>(sourceAddr)->getSelfArgumentOperand());
1814+
auto *selfOp = &cast<ApplyInst>(sourceAddr)->getSelfArgumentOperand();
1815+
if (selfOp->get()->getType().isObject()) {
1816+
return asImpl().visitUnidentified(sourceAddr);
1817+
}
1818+
return asImpl().visitAccessProjection(cast<ApplyInst>(sourceAddr), selfOp);
18171819
}
18181820

18191821
// Don't currently allow any other calls to return an accessed address.

test/SIL/borrow_accessor_e2e.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -emit-executable -enable-experimental-feature BorrowAndMutateAccessors -o %t/a.out
3+
// RUN: %target-codesign %t/a.out
4+
// RUN: %target-run %t/a.out
5+
6+
// REQUIRES: executable_test
7+
8+
public struct Container<Element: ~Copyable >: ~Copyable {
9+
var _storage: UnsafeBufferPointer<Element>
10+
var _count: Int
11+
12+
var first: Element {
13+
@_unsafeSelfDependentResult
14+
borrow {
15+
return _storage.baseAddress.unsafelyUnwrapped.pointee
16+
}
17+
}
18+
19+
public subscript(index: Int) -> Element {
20+
@_unsafeSelfDependentResult
21+
borrow {
22+
precondition(index >= 0 && index < _count, "Index out of bounds")
23+
return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index).pointee
24+
}
25+
}
26+
}
27+
28+
extension Container: Copyable where Element: Copyable {}
29+
30+
func test() {
31+
let n = 10_000
32+
let arr = Array(0...n)
33+
let sum = arr.withUnsafeBufferPointer { ubpointer in
34+
let container = Container(_storage: ubpointer, _count: arr.count)
35+
var sum = 0
36+
for i in 0..<container._count {
37+
sum &+= container[i]
38+
}
39+
return sum
40+
}
41+
let expectedSum = n * (n + 1) / 2
42+
assert(sum == expectedSum)
43+
}
44+
45+
test()
46+

0 commit comments

Comments
 (0)