Add a simple borrow accessor end to end test

This commit is contained in:
Meghana Gupta
2025-09-30 12:21:41 -07:00
parent e4d1123d10
commit c86f2c0dbc
2 changed files with 52 additions and 3 deletions

View File

@@ -1811,9 +1811,11 @@ Result AccessUseDefChainVisitor<Impl, Result>::visit(SILValue sourceAddr) {
return asImpl().visitUnidentified(sourceAddr);
if (isGuaranteedAddressReturn(sourceAddr)) {
return asImpl().visitAccessProjection(
cast<ApplyInst>(sourceAddr),
&cast<ApplyInst>(sourceAddr)->getSelfArgumentOperand());
auto *selfOp = &cast<ApplyInst>(sourceAddr)->getSelfArgumentOperand();
if (selfOp->get()->getType().isObject()) {
return asImpl().visitUnidentified(sourceAddr);
}
return asImpl().visitAccessProjection(cast<ApplyInst>(sourceAddr), selfOp);
}
// Don't currently allow any other calls to return an accessed address.

View File

@@ -0,0 +1,47 @@
// RUN: %empty-directory(%t)
// RUN: %target-build-swift %s -emit-executable -enable-experimental-feature BorrowAndMutateAccessors -o %t/a.out
// RUN: %target-codesign %t/a.out
// RUN: %target-run %t/a.out
// REQUIRES: swift_feature_BorrowAndMutateAccessors
// REQUIRES: executable_test
public struct Container<Element: ~Copyable >: ~Copyable {
var _storage: UnsafeBufferPointer<Element>
var _count: Int
var first: Element {
@_unsafeSelfDependentResult
borrow {
return _storage.baseAddress.unsafelyUnwrapped.pointee
}
}
public subscript(index: Int) -> Element {
@_unsafeSelfDependentResult
borrow {
precondition(index >= 0 && index < _count, "Index out of bounds")
return _storage.baseAddress.unsafelyUnwrapped.advanced(by: index).pointee
}
}
}
extension Container: Copyable where Element: Copyable {}
func test() {
let n = 10_000
let arr = Array(0...n)
let sum = arr.withUnsafeBufferPointer { ubpointer in
let container = Container(_storage: ubpointer, _count: arr.count)
var sum = 0
for i in 0..<container._count {
sum &+= container[i]
}
return sum
}
let expectedSum = n * (n + 1) / 2
assert(sum == expectedSum)
}
test()