File: tuple-conformances.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (113 lines) | stat: -rw-r--r-- 3,694 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// RUN: %target-typecheck-verify-swift -enable-experimental-feature TupleConformances

// Because of -enable-experimental-feature TupleConformances
// REQUIRES: asserts

extension () {
  // expected-error@-1 {{tuple extension must be written as extension of '(repeat each Element)'}}
  // expected-error@-2 {{tuple extension must declare conformance to exactly one protocol}}

  struct Nested {}
  // expected-error@-1 {{type 'Nested' cannot be nested in tuple extension}}
}

typealias BadTuple<each Horse> = (repeat each Horse, Int)
extension BadTuple {}
// expected-error@-1 {{tuple extension must be written as extension of '(repeat each Horse)'}}
// expected-error@-2 {{tuple extension must declare conformance to exactly one protocol}}

typealias Tuple<each Element> = (repeat each Element)

protocol Q {}

class C {}

extension Tuple: Q where repeat each Element: Collection, repeat (each Element).Element == (each Element).Index, repeat (each Element).Indices: AnyObject, repeat (each Element).SubSequence: C {}
// expected-error@-1 {{tuple extension must require that 'each Element' conforms to 'Q'}}
// expected-error@-2 {{tuple extension cannot require that 'each Element' conforms to 'Collection'}}
// expected-error@-3 {{tuple extension cannot require that '(each Element).Element' is the same type as '(each Element).Index'}}
// expected-error@-4 {{tuple extension cannot require that '(each Element).Indices' conforms to 'AnyObject'}}
// expected-error@-5 {{tuple extension cannot require that '(each Element).SubSequence' subclasses 'C'}}

protocol Base1 {}
protocol Derived1: Base1 {}

extension Tuple: Derived1 where repeat each Element: Derived1 {}
// expected-error@-1 {{conditional conformance of type '(repeat each Element)' to protocol 'Derived1' does not imply conformance to inherited protocol 'Base1'}}
// expected-note@-2 {{did you mean to explicitly state the conformance like 'extension Tuple: Base1 where ...'?}}
// expected-error@-3 {{tuple extension must declare conformance to exactly one protocol}}

protocol Base2 {}
protocol Derived2: Base2 {}

extension Tuple: Derived2 {}
// expected-error@-1 {{tuple extension must declare conformance to exactly one protocol}} // FIXME: crappy error

////

protocol P2 {}

typealias FancyTuple1<each Cat: P2> = (repeat each Cat)
extension FancyTuple1: P2 {}

protocol P3 {}

typealias FancyTuple2<each Cat: C> = (repeat each Cat)
extension FancyTuple2: P3 {}
// expected-error@-1 {{tuple extension cannot require that 'each Cat' subclasses 'C'}}
// expected-error@-2 {{tuple extension must require that 'each Cat' conforms to 'P3'}}

////

protocol P {
  associatedtype A
  associatedtype B

  func f()
}

extension Tuple: P where repeat each Element: P {
  typealias A = (repeat (each Element).A)
  typealias B = (repeat (each Element).B)
  func f() {}
}

extension Int: P {
  typealias A = Int
  typealias B = String
  func f() {}
}

func returnsPA<T: P>(_: T) -> T.A.Type {}
func returnsPB<T: P>(_: T) -> T.B.Type {}

func same<T>(_: T, _: T) {}

func useConformance() {
  same(returnsPA((1, 2, 3)), (Int, Int, Int).self)
  same(returnsPB((1, 2, 3)), (String, String, String).self)

  (1, 2, 3).f()
}

////

extension Tuple: Equatable where repeat each Element: Equatable {
  // FIXME: Hack
  @_disfavoredOverload
  public static func ==(lhs: Self, rhs: Self) -> Bool {
    var result = true
    func update<E: Equatable>(lhs: E, rhs: E) {
      result = result && (lhs == rhs)
    }

    repeat update(lhs: each lhs, rhs: each rhs)
    return result
  }
}

extension Tuple: Hashable where repeat each Element: Hashable {
  public func hash(into hasher: inout Hasher) {
    repeat (each self).hash(into: &hasher)
  }
}