forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
attr_required.swift
73 lines (56 loc) · 2.5 KB
/
attr_required.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
// RUN: %target-typecheck-verify-swift
// -------------------------------------------------------------------------
// Restrictions on where required can appear
// -------------------------------------------------------------------------
required class AC { } // expected-error{{'required' may only be used on 'init' declarations}}
class C {
required init(string s: String) { } // expected-note{{'required' initializer is declared in superclass here}}
required var s: String // expected-error{{'required' may only be used on 'init' declarations}}
required func f() { } // expected-error{{'required' may only be used on 'init' declarations}}
}
struct S {
required init() { } // expected-error{{'required' initializer in non-class type 'S'}}
}
enum E {
required init() { } // expected-error{{'required' initializer in non-class type 'E'}}
}
extension C {
required convenience init(string2 s: String) { self.init(string:s) } // expected-error{{'required' initializer must be declared directly in class 'C' (not in an extension)}}
}
// -------------------------------------------------------------------------
// Subclass requirements for required
// -------------------------------------------------------------------------
class C2 : C { // okay: implicitly defined
}
class C2b : C {
init() {}
} // expected-error{{'required' initializer 'init(string:)' must be provided by subclass of 'C'}}{{1-1=\n required init(string s: String) {\n fatalError("init(string:) has not been implemented")\n }\n}}
class C3 : C {
required init(string s: String) { } // expected-note{{overridden required initializer is here}}
}
class C4 : C3 {
// implicitly required
init(string s: String) { }
// expected-error @-1{{'required' modifier must be present on all overrides of a required initializer}}
// expected-note @-2{{'required' initializer is declared in superclass here}}
}
class C5 : C4 {
}
class C5b : C4 {
init() {}
// expected-error{{'required' initializer 'init(string:)' must be provided by subclass of 'C4'}}
func wibble() { }
}
class Foo {
required init() { }
// expected-note@-1{{'required' initializer is declared in superclass here}}
// expected-note@-2{{overridden required initializer is here}}
}
class Bar : Foo {
}
class Baz : Bar {
init(i: Int) { super.init() }
} // expected-error{{'required' initializer 'init()' must be provided by subclass of 'Bar'}}
class Baz2 : Bar {
init() { super.init() } // expected-error {{'required' modifier must be present on all overrides of a required initializer}}
}