Skip to content

Commit 8a1107e

Browse files
committed
[Tests] InitAccessors: Test use of init accessor properties without setters
1 parent 135f2c2 commit 8a1107e

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

test/Interpreter/init_accessors.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,57 @@ test_memberwise_with_default_args()
462462
// CHECK-NEXT: test-defaulted-1: TestDefaulted(_a: 0, _b: 0)
463463
// CHECK-NEXT: test-defaulted-2: TestDefaulted(_a: 3, _b: 4)
464464
// CHECK-NEXT: test-defaulted-class: ("<<default>>", 1)
465+
466+
func test_init_accessors_without_setters() {
467+
struct TestStruct<T> {
468+
var _x: T
469+
470+
var x: T {
471+
init(initialValue) initializes(_x) {
472+
_x = initialValue
473+
}
474+
475+
get { _x }
476+
}
477+
478+
init(value: T) {
479+
x = value
480+
}
481+
}
482+
483+
let test1 = TestStruct(value: 42)
484+
print("test-without-setter1: \(test1.x)")
485+
486+
class Base<T: Collection> {
487+
private var _v: T
488+
489+
var data: T {
490+
init(initialValue) initializes(_v) {
491+
_v = initialValue
492+
}
493+
494+
get { _v }
495+
}
496+
497+
init(data: T) {
498+
self.data = data
499+
}
500+
}
501+
502+
let test2 = Base(data: [1, 2, 3])
503+
print("test-without-setter2: \(test2.data)")
504+
505+
class Sub<U> : Base<U> where U: Collection, U.Element == String {
506+
init(other: U) {
507+
super.init(data: other)
508+
}
509+
}
510+
511+
let test3 = Sub(other: ["a", "b", "c"])
512+
print("test-without-setter3: \(test3.data)")
513+
}
514+
515+
test_init_accessors_without_setters()
516+
// CHECK: test-without-setter1: 42
517+
// CHECK-NEXT: test-without-setter2: [1, 2, 3]
518+
// CHECK-NEXT: test-without-setter3: ["a", "b", "c"]

test/decl/var/init_accessors.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,3 +465,59 @@ func test_default_arguments_are_analyzed() {
465465
}
466466
}
467467
}
468+
469+
struct TestStructPropWithoutSetter {
470+
var _x: Int
471+
472+
var x: Int {
473+
init(initialValue) initializes(_x) {
474+
self._x = initialValue
475+
}
476+
477+
get { _x }
478+
}
479+
480+
init(v: Int) {
481+
x = v // Ok
482+
}
483+
}
484+
485+
extension TestStructPropWithoutSetter {
486+
init(other: Int) {
487+
x = other // Ok
488+
}
489+
490+
init(other: inout TestStructPropWithoutSetter, v: Int) {
491+
other.x = v // expected-error {{cannot assign to property: 'x' is immutable}}
492+
}
493+
}
494+
495+
do {
496+
class TestClassPropWithoutSetter {
497+
var x: Int {
498+
init {
499+
}
500+
501+
get { 0 }
502+
}
503+
}
504+
505+
class SubTestPropWithoutSetter : TestClassPropWithoutSetter {
506+
init(otherV: Int) {
507+
x = otherV // Ok
508+
}
509+
}
510+
511+
class OtherWithoutSetter<U> {
512+
var data: U {
513+
init {
514+
}
515+
516+
get { fatalError() }
517+
}
518+
519+
init(data: U) {
520+
self.data = data // Ok
521+
}
522+
}
523+
}

0 commit comments

Comments
 (0)