|
3 | 3 |
|
4 | 4 | // RUN: %empty-directory(%t)
|
5 | 5 | // RUN: %target-typecheck-verify-swift
|
6 |
| -// BUN: %target-swift-frontend -typecheck -verify -module-cache-path %t/MCP -o %t/InheritedDefaults.swiftmodule -module-name InheritedDefaults %s |
7 | 6 |
|
8 | 7 | import Swift
|
9 | 8 |
|
10 | 9 | public class Bar {
|
11 |
| - public init(x: Int = 24, y: Int) |
| 10 | + // note associated with the expected error in (F) below |
| 11 | + public init(x: Int = 24, y: Int, z: Int = 42) // expected-note {{corresponding parameter declared here}} |
| 12 | + |
| 13 | + public init(a: Int, b: Int = 99) |
| 14 | + public convenience init(convInit: Int = 45) {} |
| 15 | + |
| 16 | + // note associated with the expected error in (D) below |
| 17 | + public convenience init(first: Int, second: Int = 88, third: Int, fourth: Int) // expected-note {{overridden declaration is here}} |
12 | 18 | }
|
13 | 19 |
|
14 | 20 | public class Foo: Bar {
|
15 |
| - public override init(x: Int = super, y: Int) |
16 |
| - public subscript(k: Int = super) -> Int { get } // expected-error {{default value inheritance via 'super' is only valid for initializer parameters}} |
17 |
| - public func foo(z: Int = super) // expected-error {{default value inheritance via 'super' is only valid for initializer parameters}} |
| 21 | + |
| 22 | + // A) designated overriding designated - valid |
| 23 | + public override init(x: Int = super, y: Int, z: Int = super) |
| 24 | + |
| 25 | + // B) convenience shadowing convenience |
| 26 | + public convenience init(convInit: Int = super) // expected-error {{default value inheritance via 'super' is only valid on the parameters of designated initializers}} |
| 27 | + |
| 28 | + // C) convenience overriding designated |
| 29 | + public override convenience init(a: Int, b: Int = super) // expected-error {{default value inheritance via 'super' is only valid on the parameters of designated initializers}} |
| 30 | + |
| 31 | + // D) designated shadowing convenience |
| 32 | + public init(first: Int, second: Int = super, third: Int, fourth: Int) // expected-error {{default value inheritance via 'super' can only be used when overriding a designated initializer}} |
| 33 | + |
| 34 | + // E) not in initializer |
| 35 | + public subscript(k: Int = super) -> Int { get } // expected-error {{default value inheritance via 'super' is only valid on the parameters of designated initializers}} |
| 36 | + public func foo(z: Int = super) // expected-error {{default value inheritance via 'super' is only valid on the parameters of designated initializers}} |
18 | 37 | }
|
19 | 38 |
|
20 | 39 | public class Baz: Bar {
|
21 |
| - public override init(x: Int = super, y: Int = super) // expected-error {{default value inheritance via 'super' requires that the corresponding parameter of the overridden initializer has a default value}} |
| 40 | + |
| 41 | + // F) Matching param not defaulted |
| 42 | + public override init(x: Int = super, y: Int = super, z: Int = super) // expected-error {{default value inheritance via 'super' requires that the corresponding parameter of the overridden designated initializer has a default value}} |
22 | 43 | }
|
23 | 44 |
|
24 | 45 | public class Direct: Bar {
|
25 |
| - public override init(x: Int = super, y: Int) |
26 |
| - public override init(other: Int = super, value: Int) // expected-error {{argument labels for initializer 'init(other:value:)' do not match those of overridden initializer 'init(x:y:)'}} |
27 |
| - // expected-error@-1 {{default value inheritance via 'super' can only be used on the parameters of overriding initializers}} |
| 46 | + public override init(x: Int = super, y: Int, z: Int = super) |
| 47 | + |
| 48 | + // G) Doesn't override anything |
| 49 | + public override init(other: Int = super, value: Int) // expected-error {{argument labels for initializer 'init(other:value:)' do not match those of overridden initializer 'init(a:b:)'}} |
| 50 | + // expected-error@-1 {{default value inheritance via 'super' can only be used when overriding a designated initializer}} |
28 | 51 | }
|
29 | 52 |
|
30 | 53 | public class Indirect: Direct {
|
31 |
| - public override init(x: Int = super, y: Int) |
| 54 | + |
| 55 | + // H) Chain of inherited defaults - valid all the way down |
| 56 | + public override init(x: Int = super, y: Int, z: Int = super) |
| 57 | + |
| 58 | + // I) Chain of inherited defaults - invalid further down (and diagnosed there) |
32 | 59 | public override init(other: Int = super, value: Int)
|
33 | 60 | }
|
34 | 61 |
|
35 | 62 | public enum Bob {
|
36 | 63 | case bar(p: Int)
|
37 |
| - public init(foo: String = super /*comment*/) // expected-error {{default value inheritance via 'super' can only be used on the parameters of overriding initializers}} |
| 64 | + public init(foo: String = super /*comment*/) // expected-error {{default value inheritance via 'super' can only be used when overriding a designated initializer}} |
38 | 65 | }
|
0 commit comments