forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuperclass_constraint.swift
90 lines (68 loc) · 2.64 KB
/
superclass_constraint.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
// RUN: %target-typecheck-verify-swift
// RUN: %target-typecheck-verify-swift -typecheck -debug-generic-signatures %s > %t.dump 2>&1
// RUN: %FileCheck %s < %t.dump
class A {
func foo() { }
}
class B : A {
func bar() { }
}
class Other { }
func f1<T : A>(_: T) where T : Other {} // expected-error{{generic parameter 'T' cannot be a subclass of both 'A' and 'Other'}}
func f2<T : A>(_: T) where T : B {}
class GA<T> {}
class GB<T> : GA<T> {}
protocol P {}
func f3<T, U>(_: T, _: U) where U : GA<T> {}
func f4<T, U>(_: T, _: U) where U : GA<T> {}
func f5<T, U : GA<T>>(_: T, _: U) {}
func f6<U : GA<T>, T : P>(_: T, _: U) {}
func f7<U, T>(_: T, _: U) where U : GA<T>, T : P {}
func f8<T : GA<A>>(_: T) where T : GA<B> {} // expected-error{{generic parameter 'T' cannot be a subclass of both 'GA<A>' and 'GA<B>'}}
func f9<T : GA<A>>(_: T) where T : GB<A> {}
func f10<T : GB<A>>(_: T) where T : GA<A> {}
func f11<T : GA<T>>(_: T) { } // expected-error{{superclass constraint 'GA<T>' is recursive}}
func f12<T : GA<U>, U : GB<T>>(_: T, _: U) { } // expected-error{{superclass constraint 'GB<T>' is recursive}}
func f13<T : U, U : GA<T>>(_: T, _: U) { } // expected-error{{inheritance from non-protocol, non-class type 'U'}}
// rdar://problem/24730536
// Superclass constraints can be used to resolve nested types to concrete types.
protocol P3 {
associatedtype T
}
protocol P2 {
associatedtype T : P3
}
class C : P3 {
typealias T = Int
}
class S : P2 {
typealias T = C
}
extension P2 where Self.T : C {
// CHECK: superclass_constraint.(file).P2.concreteTypeWitnessViaSuperclass1
// CHECK: Generic signature: <Self where Self : P2, Self.T : C>
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : P2, τ_0_0.T : C>
func concreteTypeWitnessViaSuperclass1(x: Self.T.T) {}
}
// CHECK: superclassConformance1
// CHECK: Requirements:
// CHECK-NEXT: T : C [explicit @
// CHECK-NEXT: T : P3 [inherited @
// CHECK-NEXT: T[.P3].T == C.T [redundant]
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : C>
func superclassConformance1<T>(t: T) where T : C, T : P3 {}
// CHECK: superclassConformance2
// CHECK: Requirements:
// CHECK-NEXT: T : C [explicit @
// CHECK-NEXT: T : P3 [inherited @
// CHECK-NEXT: T[.P3].T == C.T [redundant]
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : C>
func superclassConformance2<T>(t: T) where T : C, T : P3 {}
protocol P4 { }
class C2 : C, P4 { }
// CHECK: superclassConformance3
// CHECK: Requirements:
// CHECK-NEXT: T : C2 [explicit @
// CHECK-NEXT: T : P4 [inherited @
// CHECK: Canonical generic signature: <τ_0_0 where τ_0_0 : C2>
func superclassConformance3<T>(t: T) where T : C, T : P4, T : C2 {}