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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
|
// RUN: %target-swift-frontend -enable-copy-propagation=requested-passes-only -emit-sil -primary-file %s -o /dev/null -verify
struct Test1 {
var x: Int // expected-note {{variable defined here}}
var y: Int // expected-note {{variable defined here}}
var full: (Int, Int)
var test1: (Int, Int) {
@storageRestrictions(initializes: y, full, accesses: x)
init(initialValue) {
self.full = (self.x, self.y) // expected-error {{variable 'y' used before being initialized}}
}
get { full }
set { self.full = newValue }
}
var pointY: Int {
@storageRestrictions(initializes: y)
init(initialValue) {
self.y = initialValue // Ok
}
get { y }
set {}
}
var errorPoint1: (Int, Int) {
@storageRestrictions(initializes: x, y)
init(initialValue) {
// expected-error@-1 {{property 'x' not initialized by init accessor}}
// expected-error@-2 {{property 'y' not initialized by init accessor}}
}
get { (x, y) }
set { }
}
var errorPoint2: (Int, Int) {
@storageRestrictions(initializes: x, y)
init(initialValue) {
// expected-error@-1 {{property 'y' not initialized by init accessor}}
self.x = initialValue.0
}
get { (x, y) }
set { }
}
var errorPoint3: (Int, Int) {
@storageRestrictions(initializes: x, y)
init(initialValue) {
self.y = initialValue.1
print(y) // Ok
print(x) // expected-error {{variable 'x' used before being initialized}}
}
get { (x, y) }
set { }
}
init(x: Int, y: Int) {
self.x = x
self.y = y
self.full = (x, y)
}
}
struct TestPartial {
var x: Int
var y: Int
var point: (Int, Int) {
@storageRestrictions(initializes: x, y)
init(initialValue) {
self.x = initialValue.0
self.y = initialValue.1
}
get { (x, y) }
set { }
}
init(x: Int, y: Int) {
self.x = x
self.point = (x, y) // Ok (x is going to get `destroy_addr`)
}
init(_ x: Int, _ y: Int) {
self.x = x
self.y = y
self.point = (x, y) // Ok (calls a setter)
}
}
struct TestDoubleInit1 {
let x: Int // expected-note {{change 'let' to 'var' to make it mutable}}
var invalidPointX: Int {
@storageRestrictions(initializes: x)
init(initialValue) {
self.x = initialValue
self.x = 42 // expected-error {{immutable value 'x' may only be initialized once}}
}
get { x }
set { }
}
}
struct TestDoubleInit2 {
let x: Int // expected-note {{change 'let' to 'var' to make it mutable}}
var pointX: Int {
@storageRestrictions(initializes: x)
init(initialValue) {
self.x = initialValue
}
get { x }
set { }
}
init(x: Int) {
self.pointX = x
self.x = 0 // expected-error {{immutable value 'self.x' may only be initialized once}}
}
}
struct TestAccessBeforeInit {
var _x: Int
var x: Int {
@storageRestrictions(initializes: _x, accesses: y)
init(initialValue) {
_x = initialValue
}
get { _x }
set {}
}
var y: Int
init(x: Int, y: Int) {
self.x = x // expected-error {{variable 'self.y' used before being initialized}}
self.y = y
}
}
class TestInitWithGuard {
var _a: Int
var _b: Int
var pair1: (Int, Int) {
@storageRestrictions(initializes: _a, _b)
init(initialValue) { // expected-error {{property '_b' not initialized by init accessor}}
_a = initialValue.0
if _a > 0 {
return
}
_b = initialValue.1
}
get { (_a, _b) }
set { }
}
var pair2: (Int, Int) {
@storageRestrictions(initializes: _a, _b)
init(initialValue) { // Ok
_a = initialValue.0
if _a > 0 {
_b = 0
return
}
_b = initialValue.1
}
get { (_a, _b) }
set { }
}
init(a: Int, b: Int) {
self.pair2 = (a, b)
}
}
do {
struct TestPropertyInitWithUnreachableBlocks {
var _a: Int
var a: Int? {
@storageRestrictions(initializes: _a)
init { _a = newValue ?? 0 } // Ok
get { 42 }
}
}
}
do {
class Base<T: Collection> {
private var _v: T
var data: T {
@storageRestrictions(initializes: _v)
init(initialValue) {
_v = initialValue
}
get { _v }
}
init(data: T) {
self.data = data
}
init(reinit: T) {
self.data = reinit
self.data = reinit // expected-error {{immutable value 'data' may only be initialized once}}
}
}
class Sub<U> : Base<U> where U: Collection, U.Element == String {
init(other: U) {
super.init(data: other)
}
init(error: U) {
super.init(data: error)
data = error // expected-error {{immutable value 'data' may only be initialized once}}
}
}
// Make sure that re-initialization is not allowed when there is no setter.
struct TestPartialWithoutSetter {
var _a: Int
var a: Int {
@storageRestrictions(initializes: _a)
init(initialValue) {
self._a = initialValue
}
get { _a }
}
var b: Int
init(v: Int) {
self.a = v
self.a = v // expected-error {{immutable value 'a' may only be initialized once}}
self.b = v
}
}
}
do {
class Entity {
var _age: Int
var age: Int = 0 {
@storageRestrictions(initializes: _age)
init { _age = newValue }
get { _age }
set { _age = newValue }
}
}
class Person : Entity {
init(age: Int) {
self.age = age // expected-error {{'self' used in property access 'age' before 'super.init' call}}
}
init(otherAge: Int) {
super.init()
self.age = otherAge // Ok
}
}
}
// Test that init accessor without "initializes" is required
do {
struct Test {
var a: Int {
init {}
get { 42 }
set {}
}
var b: Int { // expected-note 2 {{'self' not initialized}}
init {}
get { 0 }
set { }
}
init(a: Int) {
self.a = a
} // expected-error {{return from initializer without initializing all stored properties}}
init(a: Int, b: Int) {
if a == 0 {
self.a = a
self.b = b
} else {
self.a = 0
}
} // expected-error {{return from initializer without initializing all stored properties}}
init() { // Ok
self.a = 0
self.b = 0
}
}
struct TestWithStored {
var _value: Int = 0
var a: Int {
@storageRestrictions(accesses: _value)
init {}
get { _value }
set { _value = newValue }
}
}
_ = TestWithStored(a: 42) // Ok
_ = TestWithStored(_value: 1, a: 42) // Ok
class TestWithStoredExplicit {
var _value: Int = 0
var _other: String = ""
var a: Int { // expected-note 2 {{'self' not initialized}}
@storageRestrictions(accesses: _value)
init {}
get { _value }
set { _value = newValue }
}
init(data: Int) {
self._value = data
} // expected-error {{return from initializer without initializing all stored properties}}
init() {} // expected-error {{return from initializer without initializing all stored properties}}
init(a: Int) {
self.a = a // Ok
}
}
}
|