File: inference.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 (87 lines) | stat: -rw-r--r-- 2,201 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
// RUN: %target-typecheck-verify-swift

func takeIntToInt(_ f: (Int) -> Int) { }
func takeIntIntToInt(_ f: (Int, Int) -> Int) { }

// Anonymous arguments with inference
func myMap<T, U>(_ array: [T], _ f: (T) -> U) -> [U] {}

func testMap(_ array: [Int]) {
  var farray = myMap(array, { Float($0) })
  var _ : Float = farray[0]
  let farray2 = myMap(array, { x in Float(x) })
  farray = farray2
  _ = farray
}

// Infer result type.
func testResultType() {
  takeIntToInt({x in
    return x + 1
  })

  takeIntIntToInt({x, y in
    return 2 + 3
  })
}

// Closures with unnamed parameters
func unnamed() {
  takeIntToInt({_ in return 1})
  takeIntIntToInt({_, _ in return 1})
}

// Regression tests.

var nestedClosuresWithBrokenInference = { f: Int in {} }
    // expected-error@-1 {{consecutive statements on a line must be separated by ';'}} {{44-44=;}}
    // expected-error@-2 {{expected expression}}
    // expected-error@-3 {{cannot find 'f' in scope}}

// https://github.com/apple/swift/issues/53941
do {
  func f1<R>(action: () -> R) -> Void {}

  func f1<T, R>(action: (T) -> R) -> Void {}

  func f2<T, R>(action: (T) -> R) -> Void {}

  f1(action: { return }) // Ok f1<R>(action: () -> R) was the selected overload.

  // In case that's the only possible overload, it's acceptable
  f2(action: { return }) // OK
}

// https://github.com/apple/swift/issues/51081
do {
  func f1<A,Z>(_ f: @escaping (A) -> Z) -> (A) -> Z {}

  func f1<A,B,Z>(_ f: @escaping (A, B) -> Z) -> (A, B) -> Z {}

  let aa = f1 { (a: Int) in }
  let bb = f1 { (a1: Int, a2: String) in } // expected-note {{'bb' declared here}}

  aa(1) // Ok
  bb(1, "2") // Ok
  bb(1) // expected-error {{missing argument for parameter #2 in call}}

  // Tuple
  let cc = f1 { (_: (Int)) in }

  cc((1)) // Ok
  cc(1) // Ok
}

// https://github.com/apple/swift/issues/55401
do {
  let f: @convention(c) (T) -> Void // expected-error {{cannot find type 'T' in scope}}
}

// https://github.com/apple/swift/issues/42790
do {
  func foo<T>(block: () -> ()) -> T.Type { T.self } // expected-note {{in call to function 'foo(block:)'}}

  let x = foo { // expected-error {{generic parameter 'T' could not be inferred}}
    print("")
  }
}