-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathenum_cases.swift
109 lines (81 loc) · 3.62 KB
/
enum_cases.swift
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
// RUN: %target-typecheck-verify-swift -swift-version 3
// Tests for enum case behavior in Swift 3
enum E {
case foo(bar: String)
case bar(_: String)
case two(x: Int, y: Int)
case tuple((x: Int, y: Int))
}
enum G_E<T> {
case foo(bar: T)
case bar(_: T)
case two(x: T, y: T)
case tuple((x: T, y: T))
}
let arr: [String] = []
let _ = arr.map(E.foo) // Ok
let _ = arr.map(E.bar) // Ok
let _ = arr.map(E.two) // expected-error {{cannot invoke 'map' with an argument list of type '((Int, Int) -> E)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = arr.map(E.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> E)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = arr.map(G_E<String>.foo) // Ok
let _ = arr.map(G_E<String>.bar) // Ok
let _ = arr.map(G_E<String>.two) // expected-error {{cannot invoke 'map' with an argument list of type '((String, String) -> G_E<String>)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = arr.map(G_E<Int>.tuple) // expected-error {{cannot invoke 'map' with an argument list of type '(((x: Int, y: Int)) -> G_E<Int>)'}}
// expected-note@-1{{expected an argument list of type '((Self.Element) throws -> T)'}}
let _ = E.foo("hello") // expected-error {{missing argument label 'bar:' in call}}
let _ = E.bar("hello") // Ok
let _ = G_E<String>.foo("hello") // expected-error {{missing argument label 'bar:' in call}}
let _ = G_E<String>.bar("hello") // Ok
// Passing enum case as an argument to generic function
func bar_1<T>(_: (T) -> E) {}
func bar_2<T>(_: (T) -> G_E<T>) {}
func bar_3<T, U>(_: (T) -> G_E<U>) {}
bar_1(E.foo) // Ok
bar_1(E.bar) // Ok
bar_1(E.two) // Ok in Swift 3
bar_1(E.tuple) // Ok - it's going to be ((x: Int, y: Int))
bar_2(G_E<String>.foo) // Ok
bar_2(G_E<Int>.bar) // Ok
bar_2(G_E<Int>.two) // expected-error {{cannot convert value of type '(Int, Int) -> G_E<Int>' to expected argument type '(_) -> G_E<_>'}}
bar_2(G_E<Int>.tuple) // expected-error {{cannot convert value of type '((x: Int, y: Int)) -> G_E<Int>' to expected argument type '(_) -> G_E<_>'}}
bar_3(G_E<Int>.tuple) // Ok
// Regular enum case assigned as a value
let foo: (String) -> E = E.foo // Ok
let _ = foo("hello") // Ok
let bar: (String) -> E = E.bar // Ok
let _ = bar("hello") // Ok
let two: (Int, Int) -> E = E.two // Ok
let _ = two(0, 42) // Ok
let tuple: ((x: Int, y: Int)) -> E = E.tuple // Ok
let _ = tuple((x: 0, y: 42)) // Ok
// Generic enum case assigned as a value
let g_foo: (String) -> G_E<String> = G_E<String>.foo // Ok
let _ = g_foo("hello") // Ok
let g_bar: (String) -> G_E<String> = G_E<String>.bar // Ok
let _ = g_bar("hello") // Ok
let g_two: (Int, Int) -> G_E<Int> = G_E<Int>.two // Ok
let _ = g_two(0, 42) // Ok
let g_tuple: ((x: Int, y: Int)) -> G_E<Int> = G_E<Int>.tuple // Ok
let _ = g_tuple((x: 0, y: 42)) // Ok
// Following example doesn't work in Swift 3 mode because Foo.a and Foo.b have different argument types
// since the labels are not striped from them.
enum Foo {
case a(x: Int)
case b(y: Int)
}
func foo<T>(_: T, _: T) {}
foo(Foo.a, Foo.b) // expected-error {{cannot invoke 'foo' with an argument list of type '((Int) -> Foo, (Int) -> Foo)}}
// expected-note@-1 {{expected an argument list of type '(T, T)'}}
// rdar://problem/32551313 - Useless SE-0110 diagnostic (only in Swift 4 mode)
enum E_32551313<L, R> {
case Left(L)
case Right(R)
}
struct Foo_32551313 {
static func bar() -> E_32551313<(String, Foo_32551313?), (String, String)>? {
return E_32551313.Left("", Foo_32551313()) // Ok
}
}