mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
Add a simple borrow accessor end to end test
This commit is contained in:
@@ -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.
|
||||
|
||||
47
test/SIL/borrow_accessor_e2e.swift
Normal file
47
test/SIL/borrow_accessor_e2e.swift
Normal 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()
|
||||
|
||||
Reference in New Issue
Block a user