File: variadic_generic_constraints.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (78 lines) | stat: -rw-r--r-- 3,092 bytes parent folder | download | duplicates (2)
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
// RUN: %target-typecheck-verify-swift

// Test instantiation of constraint solver constraints from generic requirements
// involving type pack parameters

// Conformance requirements

protocol P {}

func takesP<each T: P>(_: repeat each T) {}  // expected-note {{where 'each T' = 'DoesNotConformToP'}}

struct ConformsToP: P {}
struct DoesNotConformToP {}

takesP()  // ok
takesP(ConformsToP(), ConformsToP(), ConformsToP())  // ok

takesP(ConformsToP(), DoesNotConformToP(), ConformsToP()) // expected-error {{global function 'takesP' requires that 'DoesNotConformToP' conform to 'P'}}

// Superclass requirements

class C {}

class SubclassOfC: C {}
class NotSubclassOfC {}

func takesC<each T: C>(_: repeat each T) {}  // expected-note {{where 'each T' = 'NotSubclassOfC'}}

takesC()  // ok
takesC(SubclassOfC(), SubclassOfC(), SubclassOfC())  // ok

takesC(SubclassOfC(), NotSubclassOfC(), SubclassOfC())  // expected-error {{global function 'takesC' requires that 'NotSubclassOfC' inherit from 'C'}}

// Layout requirements

struct S {}

func takesAnyObject<each T: AnyObject>(_: repeat each T) {}

takesAnyObject()
takesAnyObject(C(), C(), C())

// FIXME: Bad diagnostic
takesAnyObject(C(), S(), C())  // expected-error {{type of expression is ambiguous without a type annotation}}

// Same-type requirements

// expected-note@+1 {{where '(each T).Element' = 'String', '(each U).Element' = 'Int'}}
func takesParallelSequences<each T, each U>(t: repeat each T, u: repeat each U)
    where repeat each T: Sequence,
          repeat each U: Sequence,
          repeat (each T).Element == (each U).Element {}
takesParallelSequences()  // ok
takesParallelSequences(t: Array<Int>(), u: Set<Int>())  // ok
takesParallelSequences(t: Array<String>(), Set<Int>(), u: Set<String>(), Array<Int>())  // ok
takesParallelSequences(t: Array<String>(), Set<Int>(), u: Array<Int>(), Set<String>())  // expected-error {{global function 'takesParallelSequences(t:u:)' requires the types 'String' and 'Int' be equivalent}}

// Same-shape requirements

func zip<each T, each U>(t: repeat each T, u: repeat each U) -> (repeat (each T, each U)) {}
// expected-note@-1 {{'zip(t:u:)' declared here}}

let _ = zip()  // ok
let _ = zip(t: 1, u: "hi")  // ok
let _ = zip(t: 1, 2, u: "hi", "hello")  // ok
let _ = zip(t: 1, 2, 3, u: "hi", "hello", "greetings")  // ok
let _ = zip(t: 1, u: "hi", "hello", "greetings")  // expected-error {{extra arguments at positions #3, #4 in call}}
// expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'Int' and 'String, String, String' have the same shape}}

func goodCallToZip<each T, each U>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any {
  _ = zip(t: repeat each t, u: repeat each u)
}

func badCallToZip<each T, each U>(t: repeat each T, u: repeat each U) {
  _ = zip(t: repeat each t, u: repeat each u)
  // expected-error@-1 {{global function 'zip(t:u:)' requires the type packs 'each T' and 'each U' have the same shape}}
  // expected-error@-2 {{pack expansion requires that 'each U' and 'each T' have the same shape}}
}