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
|
// RUN: %target-typecheck-verify-swift
class A {
init(int i: Int) { }
init(double d: Double) { }
convenience init(float f: Float) {
self.init(double: Double(f))
}
}
// Implicit overriding of subobject initializers
class B : A {
var x = "Hello"
var (y, z) = (1, 2)
}
func testB() {
_ = B(int: 5)
_ = B(double: 2.71828)
_ = B(float: 3.14159)
}
// Okay to have nothing
class C : B {
}
func testC() {
_ = C(int: 5)
_ = C(double: 2.71828)
_ = C(float: 3.14159)
}
// Okay to add convenience initializers.
class D : C {
convenience init(string s: String) {
self.init(int: Int(s)!)
}
}
func testD() {
_ = D(int: 5)
_ = D(double: 2.71828)
_ = D(float: 3.14159)
_ = D(string: "3.14159")
}
// Adding a subobject initializer prevents inheritance of subobject
// initializers.
class NotInherited1 : D {
override init(int i: Int) {
super.init(int: i)
}
convenience init(float f: Float) {
self.init(int: Int(f))
}
}
func testNotInherited1() {
var n1 = NotInherited1(int: 5)
var n2 = NotInherited1(double: 2.71828) // expected-error{{incorrect argument label in call (have 'double:', expected 'float:')}}
}
class NotInherited1Sub : NotInherited1 {
override init(int i: Int) {
super.init(int: i)
}
}
func testNotInherited1Sub() {
var n1 = NotInherited1Sub(int: 5)
var n2 = NotInherited1Sub(float: 3.14159)
var n3 = NotInherited1Sub(double: 2.71828) // expected-error{{incorrect argument label in call (have 'double:', expected 'float:')}}
}
// Having a stored property without an initial value prevents
// inheritance of initializers.
class NotInherited2 : D { // expected-error{{class 'NotInherited2' has no initializers}}
var a: Int // expected-note{{stored property 'a' without initial value prevents synthesized initializers}}{{13-13= = 0}}
var b: Int // expected-note{{stored property 'b' without initial value prevents synthesized initializers}}{{13-13= = 0}}
, c: Int // expected-note{{stored property 'c' without initial value prevents synthesized initializers}}{{13-13= = 0}}
var (d, e): (Int, String) // expected-note{{stored properties 'd' and 'e' without initial values prevent synthesized initializers}}{{28-28= = (0, "")}}
var (f, (g, h)): (Int?, (Float, String)) // expected-note{{stored properties 'f', 'g', and 'h' without initial values prevent synthesized initializers}}{{43-43= = (nil, (0.0, ""))}}
var (w, i, (j, k)): (Int?, Float, (String, D)) // expected-note{{stored properties 'w', 'i', 'j', and others without initial values prevent synthesized initializers}}
}
func testNotInherited2() {
var n1 = NotInherited2(int: 5) // expected-error{{'NotInherited2' cannot be constructed because it has no accessible initializers}}
var n2 = NotInherited2(double: 2.72828) // expected-error{{'NotInherited2' cannot be constructed because it has no accessible initializers}}
}
// <rdar://problem/16331406> Implement inheritance of variadic designated initializers
class SuperVariadic {
init(ints: Int...) { }
init(_ : Double...) { }
init(s: String, ints: Int...) { }
init(s: String, _ : Double...) { }
}
class SubVariadic : SuperVariadic { }
// Don't crash with invalid nesting of class in generic function
func testClassInGenericFunc<T>(t: T) {
class A { init(t: T) {} } // expected-error {{type 'A' cannot be nested in generic function 'testClassInGenericFunc(t:)'}}
class B : A {} // expected-error {{type 'B' cannot be nested in generic function 'testClassInGenericFunc(t:)'}}
_ = B(t: t) // expected-error {{'B' cannot be constructed because it has no accessible initializers}}
}
// https://github.com/apple/swift/issues/47632
// Required convenience 'init' inhibits inheritance
do {
class A {
required init(a: Int) {}
}
class B : A {
required convenience init(b: Int) {
self.init(a: b)
}
}
class C : B {}
_ = C(a: 0)
_ = C(b: 0)
}
|