|
| 1 | +// RUN: %empty-directory(%t) |
| 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 | +// 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 | + |
| 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}} |
| 7 | + |
| 8 | +func testInferFromOtherPos1<T>(_: T = 42, _: [T]) {} |
| 9 | +// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}} |
| 10 | + |
| 11 | +func testInferFromOtherPos2<T>(_: T = 42, _: T = 0.0) {} |
| 12 | +// expected-error@-1 2 {{cannot use default expression for inference of 'T' because it is inferrable from parameters #0, #1}} |
| 13 | + |
| 14 | +protocol P { |
| 15 | + associatedtype X |
| 16 | +} |
| 17 | + |
| 18 | +func testInferFromSameType<T, U: P>(_: T = 42, _: [U]) where T == U.X {} |
| 19 | +// expected-error@-1 {{cannot use default expression for inference of 'T' because it is inferrable through same-type requirement: T == U.X}} |
| 20 | + |
| 21 | +func test1<T>(_: T = 42) {} // Ok |
| 22 | + |
| 23 | +struct S : P { |
| 24 | + typealias X = Int |
| 25 | +} |
| 26 | + |
| 27 | +func test2<T: P>(_: T = S()) {} // Ok |
| 28 | + |
| 29 | +struct A : P { |
| 30 | + typealias X = Double |
| 31 | +} |
| 32 | + |
| 33 | +class B : P { |
| 34 | + typealias X = String |
| 35 | + |
| 36 | + init() {} |
| 37 | +} |
| 38 | + |
| 39 | +func test2<T: P & AnyObject>(_: T = B()) {} // Ok |
| 40 | + |
| 41 | +func test2NonClassDefault<T: P & AnyObject>(_: T = S()) {} |
| 42 | +// expected-error@-1 {{global function 'test2NonClassDefault' requires that 'S' be a class type}} |
| 43 | +// expected-note@-2 {{where 'T' = 'S'}} |
| 44 | + |
| 45 | +func test2NonConformingDefault<T: P>(_: T = 42.0) {} |
| 46 | +// expected-error@-1 {{global function 'test2NonConformingDefault' requires that 'Double' conform to 'P'}} |
| 47 | +// expected-note@-2 {{where 'T' = 'Double'}} |
| 48 | + |
| 49 | +func testMultiple<T, U>(a: T = 42.0, b: U = "") {} // Ok |
| 50 | + |
| 51 | +// Subscripts |
| 52 | + |
| 53 | +extension S { |
| 54 | + subscript<T: P>(a: T = S()) -> Int { |
| 55 | + get { return 42 } |
| 56 | + } |
| 57 | + |
| 58 | + subscript<T: P, U: AnyObject>(a: T = S(), b: U = B()) -> Int { |
| 59 | + get { return 42 } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +func main() { |
| 65 | + test1() // Ok |
| 66 | + |
| 67 | + test2() // Ok |
| 68 | + test2(A()) // Ok as well |
| 69 | + |
| 70 | + testMultiple() // Ok (T = Double, U = String) |
| 71 | + testMultiple(a: 0) // Ok (T = Int, U = String) |
| 72 | + testMultiple(b: S()) // Ok (T = Double, U = S) |
| 73 | + testMultiple(a: 0.0, b: "a") // Ok |
| 74 | + |
| 75 | + // From a different module |
| 76 | + with_defaults() // Ok |
| 77 | + with_defaults("") // Ok |
| 78 | + |
| 79 | + _ = S()[] // Ok |
| 80 | + _ = S()[B()] // Ok |
| 81 | +} |
0 commit comments