forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconstructor.swift
111 lines (87 loc) · 2.11 KB
/
constructor.swift
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
// RUN: %target-parse-verify-swift
// User-written default constructor
struct X {
init() {}
}
X() // expected-warning{{unused}}
// User-written memberwise constructor
struct Y {
var i : Int, f : Float
init(i : Int, f : Float) {}
}
Y(i: 1, f: 1.5) // expected-warning{{unused}}
// User-written memberwise constructor with default
struct Z {
var a : Int
var b : Int
init(a : Int, b : Int = 5) {
self.a = a
self.b = b
}
}
Z(a: 1, b: 2) // expected-warning{{unused}}
// User-written init suppresses implicit constructors.
struct A {
var i, j : Int
init(x : Int) {
i = x
j = x
}
}
A() // expected-error{{missing argument for parameter 'x'}}
A(x: 1) // expected-warning{{unused}}
A(1, 1) // expected-error{{extra argument in call}}
// No user-written constructors; implicit constructors are available.
struct B {
var i : Int = 0, j : Float = 0.0
}
extension B {
init(x : Int) {
self.i = x
self.j = 1.5
}
}
B() // expected-warning{{unused}}
B(x: 1) // expected-warning{{unused}}
B(i: 1, j: 2.5) // expected-warning{{unused}}
struct F {
var d : D
var b : B
var c : C
}
struct C {
var d : D
init(d : D) { } // suppress implicit constructors
}
struct D {
var i : Int
init(i : Int) { }
}
extension D {
init() { i = 17 }
}
F() // expected-error{{missing argument for parameter 'd'}}
D() // okay // expected-warning{{unused}}
B() // okay // expected-warning{{unused}}
C() // expected-error{{missing argument for parameter 'd'}}
struct E {
init(x : Wonka) { } // expected-error{{use of undeclared type 'Wonka'}}
}
var e : E
//----------------------------------------------------------------------------
// Argument/parameter name separation
//----------------------------------------------------------------------------
class ArgParamSep {
init(_ b: Int, _: Int, forInt int: Int, c _: Int, d: Int) { }
}
//===---
//===--- Tests for crashes.
//===---
//===--- rdar://14082378
struct NoCrash1a {
init(_: NoCrash1b) {} // expected-error {{use of undeclared type 'NoCrash1b'}}
}
var noCrash1c : NoCrash1a
class MissingDef {
init() // expected-error{{initializer requires a body}}
}