forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathunbound.swift
108 lines (83 loc) · 3.29 KB
/
unbound.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
// RUN: %target-parse-verify-swift
// Verify the use of unbound generic types. They are permitted in
// certain places where type inference can fill in the generic
// arguments, and banned everywhere else.
// --------------------------------------------------
// Places where generic arguments are always required
// --------------------------------------------------
struct Foo<T> { // expected-note{{generic type 'Foo' declared here}} expected-note{{generic type 'Foo' declared here}}
struct Wibble { } // expected-error{{nested in generic type}}
}
class Dict<K, V> { } // expected-note{{generic type 'Dict' declared here}} expected-note{{generic type 'Dict' declared here}} expected-note{{generic type 'Dict' declared here}}
// Cannot alias a generic type without arguments.
typealias Bar = Foo // expected-error{{reference to generic type 'Foo' requires arguments in <...>}}
// Cannot refer to a member of a generic type without arguments.
typealias FW = Foo.Wibble // expected-error{{reference to generic type 'Foo' requires arguments in <...>}}
// Cannot inherit from a generic type without arguments.
class MyDict : Dict { } // expected-error{{reference to generic type 'Dict' requires arguments in <...>}}
// Cannot create variables of a generic type without arguments.
// FIXME: <rdar://problem/14238814> would allow it for local variables
// only
var x : Dict // expected-error{{reference to generic type 'Dict' requires arguments in <...>}}
// Cannot create parameters of generic type without arguments.
func f(x: Dict) {} // expected-error{{reference to generic type 'Dict' requires arguments in <...>}}
// ---------------------------------------------
// Unbound name references within a generic type
// ---------------------------------------------
struct GS<T> {
func f() -> GS {
let gs = GS()
return gs
}
struct Nested { // expected-error{{nested in generic type}}
func ff() -> GS {
let gs = GS()
return gs
}
}
struct NestedGeneric<U> { // expected-note{{generic type 'NestedGeneric' declared here}} // expected-error{{generic type 'NestedGeneric' nested in type}}
func fff() -> (GS, NestedGeneric) {
let gs = GS()
let ns = NestedGeneric()
return (gs, ns)
}
}
// FIXME: We're losing some sugar here by performing the substitution.
func ng() -> NestedGeneric { } // expected-error{{reference to generic type 'GS<T>.NestedGeneric' requires arguments in <...>}}
}
extension GS {
func g() -> GS {
let gs = GS()
return gs
}
func h() {
_ = GS() as GS<Int> // expected-error{{cannot convert value of type 'GS<T>' to type 'GS<Int>' in coercion}}
}
}
class GC<T, U> {
init() {}
func f() -> GC {
let gc = GC()
return gc
}
}
extension GC {
func g() -> GC {
let gc = GC()
return gc
}
}
class SomeClassWithInvalidMethod {
func method<T>() { // expected-error {{generic parameter 'T' is not used in function signature}}
self.method()
}
}
// <rdar://problem/20792596> QoI: Cannot invoke with argument list (T), expected an argument list of (T)
protocol r20792596P {}
// expected-note @+1 {{in call to function 'foor20792596'}}
func foor20792596<T: r20792596P>(x: T) -> T {
return x
}
func callfoor20792596<T>(x: T) -> T {
return foor20792596(x) // expected-error {{generic parameter 'T' could not be inferred}}
}