|
| 1 | +// RUN: %target-run-simple-swift | %FileCheck %s |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +protocol Protocol {} |
| 5 | +extension Protocol { |
| 6 | + func foo() { |
| 7 | + print("I survived in \(Self.self).\(#function)") |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +struct Foo<T: Equatable> { |
| 12 | + struct Inner1 where T: Protocol { |
| 13 | + let bar: T |
| 14 | + } |
| 15 | + |
| 16 | + struct Inner2 where T == String { |
| 17 | + func getString() -> T { |
| 18 | + return "I survived in \(#function), T := \(T.self)" |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + struct Inner3<U: Hashable> { |
| 23 | + func isLessThan(lhs: T, rhs: T) -> U where T: Comparable, U == Bool { |
| 24 | + return lhs < rhs |
| 25 | + } |
| 26 | + |
| 27 | + init() { |
| 28 | + print("This is the unconstrained \(#function)") |
| 29 | + } |
| 30 | + init() where U == T { |
| 31 | + print("I survived in \(#function), T := \(T.self), U := \(U.self)") |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +struct ProtocolAdopter: Protocol, Equatable {} |
| 37 | + |
| 38 | +// CHECK: I survived in ProtocolAdopter.foo() |
| 39 | +Foo<ProtocolAdopter>.Inner1(bar: ProtocolAdopter()).bar.foo() |
| 40 | + |
| 41 | +// CHECK: I survived in getString(), T := String |
| 42 | +print(Foo<String>.Inner2().getString()) |
| 43 | + |
| 44 | +// CHECK: This is the unconstrained init() |
| 45 | +// CHECK: false |
| 46 | +print(Foo<Int>.Inner3<Bool>().isLessThan(lhs: .zero, rhs: .zero)) |
| 47 | + |
| 48 | +// CHECK: I survived in init(), T := Bool, U := Bool |
| 49 | +_ = Foo<Bool>.Inner3<Bool>() |
| 50 | + |
| 51 | +protocol RefinedProtocol: Protocol { |
| 52 | + associatedtype Assoc = Self |
| 53 | + associatedtype Bssoc: RefinedProtocol |
| 54 | + |
| 55 | + func overload() |
| 56 | +} |
| 57 | + |
| 58 | +extension RefinedProtocol { |
| 59 | + func callOverload() { |
| 60 | + overload() |
| 61 | + print("Assoc := \(Assoc.self), Bssoc := \(Bssoc.self)") |
| 62 | + } |
| 63 | + |
| 64 | + func overload() where Assoc == Bssoc { |
| 65 | + print("I survived in \(Self.self).\(#function) (1)") |
| 66 | + } |
| 67 | + func overload() where Assoc == Self { |
| 68 | + print("I survived in \(Self.self).\(#function) (2)") |
| 69 | + } |
| 70 | + func overload() where Assoc: Sequence, Bssoc == Assoc.Element { |
| 71 | + print("I survived in \(Self.self).\(#function) (3)") |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +struct RefinedProtocolAdopter1: RefinedProtocol { |
| 76 | + typealias Assoc = RefinedProtocolAdopter2 |
| 77 | + typealias Bssoc = RefinedProtocolAdopter2 |
| 78 | +} |
| 79 | +struct RefinedProtocolAdopter2: RefinedProtocol { |
| 80 | + typealias Bssoc = RefinedProtocolAdopter1 |
| 81 | +} |
| 82 | +struct RefinedProtocolAdopter3: RefinedProtocol { |
| 83 | + typealias Assoc = Array<Bssoc> |
| 84 | + typealias Bssoc = RefinedProtocolAdopter3 |
| 85 | +} |
| 86 | + |
| 87 | +@inline(never) |
| 88 | +func callThroughToOverload<T: RefinedProtocol>(arg: T) { |
| 89 | + arg.callOverload() |
| 90 | +} |
| 91 | + |
| 92 | +// CHECK: I survived in RefinedProtocolAdopter1.overload() (1) |
| 93 | +// CHECK: Assoc := RefinedProtocolAdopter2, Bssoc := RefinedProtocolAdopter2 |
| 94 | +callThroughToOverload(arg: RefinedProtocolAdopter1()) |
| 95 | +// CHECK: I survived in RefinedProtocolAdopter2.overload() (2) |
| 96 | +// CHECK: Assoc := RefinedProtocolAdopter2, Bssoc := RefinedProtocolAdopter1 |
| 97 | +callThroughToOverload(arg: RefinedProtocolAdopter2()) |
| 98 | +// CHECK: I survived in RefinedProtocolAdopter3.overload() (3) |
| 99 | +// CHECK: Assoc := Array<RefinedProtocolAdopter3>, Bssoc := RefinedProtocolAdopter3 |
| 100 | +callThroughToOverload(arg: RefinedProtocolAdopter3()) |
| 101 | + |
0 commit comments