1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
// RUN: %target-typecheck-verify-swift -parse-stdlib
import Swift
func someValidAddress<T>() -> UnsafeMutablePointer<T> { fatalError() }
func someValidAddress<T>() -> UnsafePointer<T> { fatalError() }
struct ValidImmutable {
var base: UnsafePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress {
return base
}
}
}
struct ValidBoth {
var base: UnsafeMutablePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress {
return UnsafePointer(base)
}
unsafeMutableAddress {
return base
}
}
}
struct OnlyMutable {
var base: UnsafeMutablePointer<Int>
subscript(index: Int) -> Int {
unsafeMutableAddress { // expected-error {{subscript with a mutable addressor must also have a getter, addressor, or 'read' accessor}}
return base
}
}
}
struct Repeated {
var base: UnsafeMutablePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress { // expected-note {{previous definition}}
return UnsafePointer(base)
}
unsafeAddress { // expected-error {{subscript already has an addressor}}
return base // expected-error {{cannot convert return expression of type 'UnsafeMutablePointer<Int>' to return type 'UnsafePointer<Int>'}}
}
}
}
struct RepeatedMutable {
var base: UnsafeMutablePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress {
return UnsafePointer(base)
}
unsafeMutableAddress { // expected-note {{previous definition}}
return base
}
unsafeMutableAddress { // expected-error {{subscript already has a mutable addressor}}
return base
}
}
}
struct AddressorAndGet {
var base: UnsafePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress { // expected-error {{subscript cannot provide both an addressor and a getter}}
return base
}
get { // expected-note {{getter defined here}}
return base.get() // expected-error {{value of type 'UnsafePointer<Int>' has no member 'get'}}
}
}
}
struct AddressorAndSet {
var base: UnsafePointer<Int>
subscript(index: Int) -> Int {
unsafeAddress {
return base
}
set {
}
}
}
struct MutableAddressorAndGet {
var base: UnsafeMutablePointer<Int>
subscript(index: Int) -> Int {
unsafeMutableAddress {
return base
}
get {
return base.pointee
}
}
}
protocol HasImmutableSubscript {
subscript(index: Int) -> Int { get }
}
protocol HasMutableSubscript {
subscript(index: Int) -> Int { get set } // expected-note {{protocol requires}}
}
struct DisobedientImmutableAddressor: HasMutableSubscript { // expected-error {{does not conform}}
subscript(index: Int) -> Int { // expected-note {{candidate is not settable}}
unsafeAddress { return someValidAddress() }
}
}
struct ObedientImmutableAddressor: HasImmutableSubscript {
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
}
}
struct ObedientMutableAddressor: HasMutableSubscript {
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
unsafeMutableAddress { return someValidAddress() }
}
}
protocol HasMutatingImmutableSubscript {
subscript(index: Int) -> Int { mutating get }
}
protocol HasMutatingMutableSubscript {
subscript(index: Int) -> Int { mutating get set } // expected-note {{protocol requires}}
}
// We allow mutating accessor requirements to be implemented by non-mutating accessors.
struct DisobedientImmutableAddressor2: HasMutatingMutableSubscript { // expected-error {{does not conform}}
subscript(index: Int) -> Int { // expected-note {{candidate is not settable}}
unsafeAddress { return someValidAddress() }
}
}
struct ObedientImmutableAddressor2: HasMutatingImmutableSubscript {
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
}
}
struct ObedientMutableAddressor2: HasMutatingMutableSubscript {
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
unsafeMutableAddress { return someValidAddress() }
}
}
// Non-mutating accessor requirements cannot be implemented by mutating accessors.
protocol HasNonMutatingMutableSubscript {
subscript(index: Int) -> Int { get nonmutating set } // expected-note {{protocol requires}}
}
struct DisobedientNonMutatingMutableAddressor: HasNonMutatingMutableSubscript { // expected-error {{does not conform}}
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
unsafeMutableAddress { return someValidAddress() } // expected-note {{candidate is marked 'mutating' but protocol does not allow it}}
}
}
struct ObedientNonMutatingMutableAddressor: HasNonMutatingMutableSubscript {
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
nonmutating unsafeMutableAddress { return someValidAddress() }
}
}
struct RedundantAddressors {
var owner : Builtin.NativeObject
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() } // expected-note {{previous definition of addressor here}}
unsafeAddress { return someValidAddress() } // expected-error {{subscript already has an addressor}}
}
}
struct RedundantMutableAddressors {
var owner : Builtin.NativeObject
subscript(index: Int) -> Int {
unsafeAddress { return someValidAddress() }
unsafeMutableAddress { return someValidAddress() } // expected-note {{previous definition of mutable addressor here}}
unsafeMutableAddress { return someValidAddress() } // expected-error {{subscript already has a mutable addressor}}
}
}
|