|
| 1 | +// RUN: %target-swift-frontend \ |
| 2 | +// RUN: %s \ |
| 3 | +// RUN: -emit-sil \ |
| 4 | +// RUN: -enable-experimental-feature ValueGenerics \ |
| 5 | +// RUN: -enable-builtin-module \ |
| 6 | +// RUN: -disable-availability-checking \ |
| 7 | +// RUN: -O |
| 8 | + |
| 9 | +// REQUIRES: swift_feature_ValueGenerics |
| 10 | + |
| 11 | +// Force verification of TypeLowering's isTrivial. |
| 12 | + |
| 13 | +import Builtin |
| 14 | + |
| 15 | +@frozen |
| 16 | +public struct Vector<let Count: Int, Element: ~Copyable>: ~Copyable { |
| 17 | + private var storage: Builtin.FixedArray<Count, Element> |
| 18 | + |
| 19 | + public init(_ valueForIndex: (Int) -> Element) { |
| 20 | + storage = Builtin.emplace { rawPointer in |
| 21 | + let base = UnsafeMutablePointer<Element>(rawPointer) |
| 22 | + for i in 0..<Count { |
| 23 | + (base + i).initialize(to: valueForIndex(i)) |
| 24 | + } |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + public subscript(i: Int) -> Element { |
| 29 | + _read { |
| 30 | + assert(i >= 0 && i < Count) |
| 31 | + let rawPointer = Builtin.addressOfBorrow(self) |
| 32 | + let base = UnsafePointer<Element>(rawPointer) |
| 33 | + yield ((base + i).pointee) |
| 34 | + } |
| 35 | + |
| 36 | + _modify { |
| 37 | + assert(i >= 0 && i < Count) |
| 38 | + let rawPointer = Builtin.addressof(&self) |
| 39 | + let base = UnsafeMutablePointer<Element>(rawPointer) |
| 40 | + yield (&(base + i).pointee) |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | +extension Vector: Copyable where Element: Copyable { |
| 45 | + public init(repeating value: Element) { |
| 46 | + self.init { _ in value } |
| 47 | + } |
| 48 | +} |
| 49 | +extension Vector: BitwiseCopyable where Element: BitwiseCopyable {} |
| 50 | + |
| 51 | +func main() { |
| 52 | + var v = Vector<4, String>(repeating: "x") // OK |
| 53 | + //v[0] = 1 |
| 54 | + //v[1] = 2 |
| 55 | + //v[2] = 3 |
| 56 | + //v[3] = 4 |
| 57 | + print("break here") |
| 58 | +} |
| 59 | +main() |
0 commit comments