|
| 1 | +// RUN: %target-run-simple-swift %s |
| 2 | +// REQUIRES: executable_test |
| 3 | + |
| 4 | +protocol P { |
| 5 | + func f0() -> Int; |
| 6 | + func f(x:Int, y:Int) -> Int; |
| 7 | +} |
| 8 | + |
| 9 | +protocol Q { |
| 10 | + func f(x:Int, y:Int) -> Int; |
| 11 | +} |
| 12 | + |
| 13 | +struct S : P, Q, Equatable { |
| 14 | + |
| 15 | + // Test that it's possible to denote a zero-arg requirement |
| 16 | + // (This involved extended the parser for unqualified DeclNames) |
| 17 | + @_implements(P, f0()) |
| 18 | + func g0() -> Int { |
| 19 | + return 10 |
| 20 | + } |
| 21 | + |
| 22 | + // Test that it's possible to implement two different protocols with the |
| 23 | + // same-named requirements. |
| 24 | + @_implements(P, f(x:y:)) |
| 25 | + func g(x:Int, y:Int) -> Int { |
| 26 | + return 5 |
| 27 | + } |
| 28 | + |
| 29 | + @_implements(Q, f(x:y:)) |
| 30 | + func h(x:Int, y:Int) -> Int { |
| 31 | + return 6 |
| 32 | + } |
| 33 | + |
| 34 | + // Test that it's possible to denote an operator requirement |
| 35 | + // (This involved extended the parser for unqualified DeclNames) |
| 36 | + @_implements(Equatable, ==(_:_:)) |
| 37 | + public static func isEqual(_ lhs: S, _ rhs: S) -> Bool { |
| 38 | + return false |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func call_P_f_generic<T:P>(p:T, x: Int, y: Int) -> Int { |
| 43 | + return p.f(x:x, y:y) |
| 44 | +} |
| 45 | + |
| 46 | +func call_P_f_existential(p:P, x: Int, y: Int) -> Int { |
| 47 | + return p.f(x:x, y:y) |
| 48 | +} |
| 49 | + |
| 50 | +func call_Q_f_generic<T:Q>(q:T, x: Int, y: Int) -> Int { |
| 51 | + return q.f(x:x, y:y) |
| 52 | +} |
| 53 | + |
| 54 | +func call_Q_f_existential(q:Q, x: Int, y: Int) -> Int { |
| 55 | + return q.f(x:x, y:y) |
| 56 | +} |
| 57 | + |
| 58 | +let s = S() |
| 59 | +assert(call_P_f_generic(p:s, x:1, y:2) == 5) |
| 60 | +assert(call_P_f_existential(p:s, x:1, y:2) == 5) |
| 61 | +assert(call_Q_f_generic(q:s, x:1, y:2) == 6) |
| 62 | +assert(call_Q_f_existential(q:s, x:1, y:2) == 6) |
| 63 | +assert(!(s == s)) |
| 64 | + |
| 65 | +// Note: at the moment directly calling the member 'f' on the concrete type 'S' |
| 66 | +// doesn't work, because it's considered ambiguous between the 'g' and 'h' |
| 67 | +// members (each of which provide an 'f' via the 'P' and 'Q' protocol |
| 68 | +// conformances), and adding a non-@_implements member 'f' to S makes it win |
| 69 | +// over _both_ the @_implements members. Unclear if this is correct; I think so? |
| 70 | + |
| 71 | +// print(s.f(x:1, y:2)) |
| 72 | + |
0 commit comments