File: variadic_generic_functions.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 (107 lines) | stat: -rw-r--r-- 5,202 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
// RUN: %target-typecheck-verify-swift

func debugPrint<each T>(_ items: repeat each T)
  where repeat each T: CustomDebugStringConvertible
{
  /*for (item: T) in items {
    stdout.write(item.debugDescription)
  }*/
}

func max<each T>(_ values: repeat each T) -> (repeat each T)?
  where repeat each T: Comparable
{
  return nil
}

func min<each T: Comparable>(_ values: repeat each T) -> (repeat each T)? {
  return nil
}

func invalidPacks() {
  func monovariadic1() -> (each String) {} // expected-error {{'each' cannot be applied to non-pack type 'String'}}{{28-32=}}
  func monovariadic2<T>() -> (repeat T) {} // expected-error {{pack expansion 'T' must contain at least one pack reference}}
  func monovariadic3<T, U>() -> (T, repeat U) {} // expected-error {{pack expansion 'U' must contain at least one pack reference}}
}

func call() {
  func multipleParameters<each T>(xs: repeat each T, ys: repeat each T) -> (repeat each T) {
    return (repeat each xs)
  }
  multipleParameters()

  let x: String = multipleParameters(xs: "", ys: "")
  let (one, two) = multipleParameters(xs: "", 5.0, ys: "", 5.0)
  multipleParameters(xs: "", 5.0, ys: 5.0, "") // expected-error {{conflicting arguments to generic parameter 'each T' ('Pack{Double, String}' vs. 'Pack{String, String}' vs. 'Pack{String, Double}' vs. 'Pack{Double, Double}')}}

  func multipleSequences<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) {
    return (repeat each ys)
    // expected-error@-1 {{pack expansion requires that 'each U' and 'each T' have the same shape}}
    // expected-error@-2 {{cannot convert return expression of type '(repeat each U)' to return type '(repeat each T)'}}
  }

  func multipleSequencesWithSameShape<each T, each U>(xs: repeat each T, ys: repeat each U) -> (repeat each T) where (repeat (each T, each U)): Any {
    return (repeat each ys)
    // expected-error@-1 {{cannot convert return expression of type '(repeat each U)' to return type '(repeat each T)'}}
  }

  multipleSequences()
  _ = multipleSequences(xs: "", ys: "")
  _ = multipleSequences(xs: "", 5.0, ys: 5.0, "")
}

func contextualTyping() {
  func firsts<each T>(_ seqs: repeat [each T]) -> (repeat (each T)?) {
    fatalError()
  }

  let (_, _): (Int?, String?) = firsts([42], [""]) // OK
  let (_, _): (String?, String?) = firsts([42], [""]) // expected-error {{cannot convert value of type '((Int)?, (String)?)' to specified type '(String?, String?)'}}
  let (_, _): ([Int], String?) = firsts([42], [""]) // expected-error {{cannot convert value of type '((Int)?, (String)?)' to specified type '([Int], String?)'}}
  let (_, _, _): (String?, String?, Int) = firsts([42], [""]) // expected-error {{'((Int)?, (String)?)' is not convertible to '(String?, String?, Int)', tuples have a different number of elements}}

  func dependent<each T>(_ seqs: repeat Array<each T>) -> (repeat Array<each T>.Element?) {
    fatalError()
  }

  let (_, _): (Int?, String?) = dependent([42], [""]) // OK
  let (_, _): (String?, String?) = dependent([42], [""]) // expected-error {{cannot convert value of type '(Int?, String?)' to specified type '(String?, String?)'}}
  let (_, _): ([Int], String?) = dependent([42], [""]) // expected-error {{cannot convert value of type '(Int?, String?)' to specified type '([Int], String?)'}}
  let (_, _, _): (String?, String?, Int) = dependent([42], [""]) // expected-error {{'(Int?, String?)' is not convertible to '(String?, String?, Int)', tuples have a different number of elements}}
}

// rdar://106737972 - crash-on-invalid with default argument
do {
  func foo<each T>(_: repeat each T = bar().element) {} // expected-note {{in call to function 'foo'}}
  // expected-error@-1 {{variadic parameter cannot have a default value}}
  // expected-error@-2 {{value pack expansion can only appear inside a function argument list, tuple element, or as the expression of a for-in loop}}
  // expected-error@-3 {{generic parameter 'each T' could not be inferred}}

  func bar<each T>() -> (repeat each T) {}
}


// apple/swift#69432 - Passing nil to a parameter pack fails to produce diagnostic for expression
do {
  func foo<each T>(_ value: repeat each T) {} // expected-note {{in inferring pack element #0 of 'value'}}
  // expected-note@-1 {{in inferring pack element #0 of 'value'}}
  // expected-note@-2 {{in inferring pack element #1 of 'value'}}

  foo(nil) // expected-error {{'nil' requires a contextual type}}
  foo(nil, 1) // expected-error {{'nil' requires a contextual type}}
  foo(2, nil) // expected-error {{'nil' requires a contextual type}}

  func bar<each T, U, each W>(_ t: repeat each T, u: U, w: repeat each W) {} // expected-note {{in inferring pack element #2 of 'w'}}
  // expected-note@-1 {{in inferring pack element #3 of 't'}}

  bar(1, 2, 3, nil, "Hello", u: 3, w: 4, 8, nil) // expected-error {{'nil' requires a contextual type}}
  // expected-error@-1 {{'nil' requires a contextual type}}


  func fooWithOverload(_ value: Int) {}
  func fooWithOverload<each T>(_ value: repeat each T) {}
  // expected-note@-1 {{in inferring pack element #4 of 'value'}}

  fooWithOverload(0, 1, 2, 3, nil) // expected-error {{'nil' requires a contextual type}}

}