|
| 1 | +// RUN: %target-run-simple-swift(-enable-experimental-feature VariadicGenerics) |
| 2 | + |
| 3 | +// REQUIRES: executable_test |
| 4 | + |
| 5 | +// Because of -enable-experimental-feature VariadicGenerics |
| 6 | +// REQUIRES: asserts |
| 7 | + |
| 8 | +// UNSUPPORTED: use_os_stdlib |
| 9 | +// UNSUPPORTED: back_deployment_runtime |
| 10 | + |
| 11 | +import StdlibUnittest |
| 12 | + |
| 13 | +var tuples = TestSuite("VariadicGenericConformances") |
| 14 | + |
| 15 | +protocol TypeMaker { |
| 16 | + func makeType() -> Any.Type |
| 17 | +} |
| 18 | + |
| 19 | +struct TupleMaker<each T> : TypeMaker { |
| 20 | + func makeType() -> Any.Type { |
| 21 | + return (repeat (each T)).self |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +func makeTypeIndirectly<T: TypeMaker>(_ t: T) -> Any.Type { |
| 26 | + return t.makeType() |
| 27 | +} |
| 28 | + |
| 29 | +struct ElementTupleMaker<each T: Sequence> : TypeMaker { |
| 30 | + func makeType() -> Any.Type { |
| 31 | + return (repeat (each T).Element).self |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +tuples.test("makeTuple1") { |
| 36 | + expectEqual("()", _typeName(makeTypeIndirectly(TupleMaker< >()))) |
| 37 | + |
| 38 | + // FIXME: This should unwrap the one-element tuple! |
| 39 | + // expectEqual("(Swift.Int)", _typeName(makeTypeIndirectly(TupleMaker<Int>()))) |
| 40 | + |
| 41 | + expectEqual("(Swift.Int, Swift.Bool)", _typeName(makeTypeIndirectly(TupleMaker<Int, Bool>()))) |
| 42 | +} |
| 43 | + |
| 44 | +tuples.test("makeTuple2") { |
| 45 | + expectEqual("()", _typeName(makeTypeIndirectly(ElementTupleMaker< >()))) |
| 46 | + |
| 47 | + // FIXME: This should unwrap the one-element tuple! |
| 48 | + // expectEqual("(Swift.Int)", _typeName(makeTypeIndirectly(ElementTupleMaker<Array<Int>>()))) |
| 49 | + |
| 50 | + expectEqual("(Swift.Int, Swift.Bool)", _typeName(makeTypeIndirectly(ElementTupleMaker<Array<Int>, Set<Bool>>()))) |
| 51 | +} |
| 52 | + |
| 53 | +protocol Q { |
| 54 | + associatedtype A |
| 55 | + |
| 56 | + func makeA() -> A |
| 57 | +} |
| 58 | + |
| 59 | +extension Q { |
| 60 | + func makeA() -> [Self] { return [self] } |
| 61 | +} |
| 62 | + |
| 63 | +extension String: Q {} |
| 64 | +extension Int: Q {} |
| 65 | +extension Bool: Q {} |
| 66 | + |
| 67 | +protocol HasPackRequirements { |
| 68 | + func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat each T.A) |
| 69 | + func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat each T.A) |
| 70 | +} |
| 71 | + |
| 72 | +extension HasPackRequirements { |
| 73 | + func doStuff1<each T: Q>(_ value: repeat each T) -> (repeat each T.A) { |
| 74 | + return (repeat (each value).makeA()) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +struct ConformsPackRequirements: HasPackRequirements { |
| 79 | + func doStuff2<each T: Q>(_ value: repeat each T) -> (repeat each T.A) { |
| 80 | + return (repeat (each value).makeA()) |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func testPackRequirements1<T: HasPackRequirements, each U: Q>(_ t: T, _ u: repeat each U) |
| 85 | + -> (repeat each U.A) { |
| 86 | + return t.doStuff1(repeat each u) |
| 87 | +} |
| 88 | + |
| 89 | +func testPackRequirements2<T: HasPackRequirements, each U: Q>(_ t: T, _ u: repeat each U) |
| 90 | + -> (repeat each U.A) { |
| 91 | + return t.doStuff2(repeat each u) |
| 92 | +} |
| 93 | + |
| 94 | +tuples.test("packRequirements") { |
| 95 | + expectEqual(([1], ["hi"], [false]), testPackRequirements1(ConformsPackRequirements(), 1, "hi", false)) |
| 96 | + expectEqual(([1], ["hi"], [false]), testPackRequirements2(ConformsPackRequirements(), 1, "hi", false)) |
| 97 | +} |
| 98 | + |
| 99 | +runAllTests() |
0 commit comments