|
2 | 2 | // RUN: %target-swift-frontend-emit-module -emit-module-path %t/InferViaDefaults.swiftmodule -enable-experimental-type-inference-from-defaults -module-name InferViaDefaults %S/Inputs/type_inference_via_defaults_other_module.swift |
3 | 3 | // RUN: %target-swift-frontend -enable-experimental-type-inference-from-defaults -module-name main -typecheck -verify -I %t %s %S/Inputs/type_inference_via_defaults_other_module.swift |
4 | 4 |
|
5 | | -func testInferFromResult<T>(_: T = 42) -> T { fatalError() } |
6 | | -// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from result type}} |
| 5 | +func testInferFromResult<T>(_: T = 42) -> T { fatalError() } // Ok |
| 6 | + |
| 7 | +enum ETest<T> { |
| 8 | + case test(_: T = 42) // expected-note {{default value declared here}} |
| 9 | +} |
7 | 10 |
|
8 | 11 | func testInferFromOtherPos1<T>(_: T = 42, _: [T]) {} |
9 | 12 | // expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}} |
@@ -101,7 +104,27 @@ func testReq2<T, U>(_: (T, U) = (E(), B())) where T: GenClass<U>, U: AnyObject { |
101 | 104 | func testReq3<T: P, U>(_: [T?] = [B()], _: U) where T.X == U {} |
102 | 105 | // expected-error@-1 {{cannot use default expression for inference of '[T?]' because requirement 'U == T.X' refers to other generic parameters}} |
103 | 106 |
|
| 107 | +protocol Shape { |
| 108 | +} |
| 109 | + |
| 110 | +struct Circle : Shape { |
| 111 | +} |
| 112 | + |
| 113 | +struct Rectangle : Shape { |
| 114 | +} |
| 115 | + |
| 116 | +struct Figure<S: Shape> { |
| 117 | + init(_: S = Circle()) {} // expected-note 2 {{default value declared here}} |
| 118 | +} |
| 119 | + |
104 | 120 | func main() { |
| 121 | + _ = testInferFromResult() // Ok T == Int |
| 122 | + let _: Float = testInferFromResult() // expected-error {{cannot convert value of type 'Int' to specified type 'Float'}} |
| 123 | + |
| 124 | + _ = ETest.test() // Ok |
| 125 | + |
| 126 | + let _: ETest<String> = .test() // expected-error {{cannot convert default value of type 'String' to expected argument type 'Int' for parameter #0}} |
| 127 | + |
105 | 128 | test1() // Ok |
106 | 129 |
|
107 | 130 | test2() // Ok |
@@ -129,4 +152,18 @@ func main() { |
129 | 152 | testNested6() // Ok |
130 | 153 |
|
131 | 154 | testReq2() // Ok |
| 155 | + |
| 156 | + func takesFigure<T>(_: Figure<T>) {} |
| 157 | + func takesCircle(_: Figure<Circle>) {} |
| 158 | + func takesRectangle(_: Figure<Rectangle>) {} |
| 159 | + |
| 160 | + _ = Figure.init() // Ok S == Circle |
| 161 | + let _: Figure<Circle> = .init() // Ok (S == Circle) |
| 162 | + let _: Figure<Rectangle> = .init() |
| 163 | + // expected-error@-1 {{cannot convert default value of type 'Rectangle' to expected argument type 'Circle' for parameter #0}} |
| 164 | + |
| 165 | + takesFigure(.init()) // Ok |
| 166 | + takesCircle(.init()) // Ok |
| 167 | + takesRectangle(.init()) |
| 168 | + // expected-error@-1 {{cannot convert default value of type 'Rectangle' to expected argument type 'Circle' for parameter #0}} |
132 | 169 | } |
0 commit comments