File: superclass_constraint.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 (243 lines) | stat: -rw-r--r-- 6,204 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// RUN: %target-typecheck-verify-swift
// RUN: not %target-swift-frontend -typecheck %s -debug-generic-signatures > %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{{no type for 'T' can satisfy both 'T : Other' and 'T : A'}}

// CHECK-LABEL: .f2@
// CHECK-NEXT: Generic signature: <T where T : B>
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{{no type for 'T' can satisfy both 'T : GA<B>' and 'T : GA<A>'}}

// CHECK-LABEL: .f9@
// CHECK-NEXT: Generic signature: <T where T : GB<A>>
func f9<T : GA<A>>(_: T) where T : GB<A> {}

// CHECK-LABEL: .f10@
// CHECK-NEXT: Generic signature: <T where T : GB<A>>
func f10<T : GB<A>>(_: T) where T : GA<A> {}

func f11<T : GA<T>>(_: T) { }
func f12<T : GA<U>, U : GB<T>>(_: T, _: U) { }
func f13<T : U, U : GA<T>>(_: T, _: U) { } // expected-error{{type 'T' constrained to 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
}

// CHECK-LABEL: .superclassConformance1(t:)@
// CHECK-NEXT: Generic signature: <T where T : C>
func superclassConformance1<T>(t: T)
  where T : C,
        T : P3 {}



// CHECK-LABEL: .superclassConformance2(t:)@
// CHECK-NEXT: Generic signature: <T where T : C>
func superclassConformance2<T>(t: T)
  where T : C,
   T : P3 {}

protocol P4 { }

class C2 : C, P4 { }

// CHECK-LABEL: .superclassConformance3(t:)@
// CHECK-NEXT: Generic signature: <T where T : C2>
func superclassConformance3<T>(t: T) where T : C, T : P4, T : C2 {}

protocol P5: A { }

protocol P6: A, Other { } // expected-error {{no type for 'Self' can satisfy both 'Self : Other' and 'Self : A'}}
// expected-error@-1{{multiple inheritance from classes 'A' and 'Other'}}

func takeA(_: A) { }
func takeP5<T: P5>(_ t: T) {
	takeA(t) // okay
}

protocol P7 {
// expected-error@-1{{no type for 'Self.Assoc' can satisfy both 'Self.Assoc : Other' and 'Self.Assoc : A'}}
	associatedtype Assoc: A, Other
}

// CHECK-LABEL: .superclassConformance4@
// CHECK-NEXT: Generic signature: <T, U where T : P3, U : P3, T.[P3]T : C, T.[P3]T == U.[P3]T>
func superclassConformance4<T: P3, U: P3>(_: T, _: U)
  where T.T: C,
        U.T: C,
        T.T == U.T { }

// Lookup of superclass associated types from inheritance clause

protocol Elementary {
  associatedtype Element

  func get() -> Element
}

class Classical : Elementary {
  func get() -> Int {
    return 0
  }
}

// CHECK-LABEL: .genericFunc@
// CHECK-NEXT: Generic signature: <T, U where T : Elementary, U : Classical, T.[Elementary]Element == Int>
func genericFunc<T : Elementary, U : Classical>(_: T, _: U) where T.Element == U.Element {}

// Lookup within superclass constraints.
protocol P8 {
  associatedtype B
}

class C8 {
  struct A { }
}

// CHECK-LABEL: .superclassLookup1@
// CHECK-NEXT: Generic signature: <T where T : C8, T : P8, T.[P8]B == C8.A>
func superclassLookup1<T: C8 & P8>(_: T) where T.A == T.B { }

// CHECK-LABEL: .superclassLookup2@
// CHECK-NEXT: Generic signature: <T where T : C8, T : P8, T.[P8]B == C8.A>
func superclassLookup2<T: P8>(_: T) where T.A == T.B, T: C8 { }

// CHECK-LABEL: .superclassLookup3@
// CHECK-NEXT: Generic signature: <T where T : C8, T : P8, T.[P8]B == C8.A>
func superclassLookup3<T>(_: T) where T.A == T.B, T: C8, T: P8 { }

// https://github.com/apple/swift/issues/47741

class C9 {}

protocol P9 {}

class C10 : C9, P9 { }

protocol P10 {
  associatedtype A: C9
}

// CHECK-LABEL: .testP10@
// CHECK-NEXT: Generic signature: <T where T : P10, T.[P10]A : C10>
func testP10<T>(_: T) where T: P10, T.A: C10 { }

// Nested types of generic class-constrained type parameters.
protocol Tail {
  associatedtype E
}

protocol Rump : Tail {
  associatedtype E = Self
}

class Horse<T>: Rump { }

// CHECK-LABEL: .hasRedundantConformanceConstraint@
// CHECK-NEXT: Generic signature: <X, T where X : Horse<T>>
func hasRedundantConformanceConstraint<X : Horse<T>, T>(_: X) where X : Rump {}

// https://github.com/apple/swift/issues/48432

protocol X {
	associatedtype Y : A
}

// CHECK-LABEL: .noRedundancyWarning@
// CHECK: Generic signature: <C where C : X, C.[X]Y == B>
func noRedundancyWarning<C : X>(_ wrapper: C) where C.Y == B {}

// [qualified lookup] https://github.com/apple/swift/issues/44798

protocol Init {
  init(x: ())
}

class Base {
  required init(y: ()) {}
}

class Derived : Base {}

func g<T : Init & Derived>(_: T.Type) {
  _ = T(x: ())
  _ = T(y: ())
}

// Binding a class-constrained generic parameter to a subclass existential is
// not sound.
struct G<T : Base> {}
// expected-note@-1 2 {{requirement specified as 'T' : 'Base' [with T = any Base & P]}}

_ = G<Base & P>() // expected-error {{'G' requires that 'any Base & P' inherit from 'Base'}}

func badClassConstrainedType(_: G<Base & P>) {}
// expected-error@-1 {{'G' requires that 'any Base & P' inherit from 'Base'}}

// Reduced from CoreStore in source compat suite
public protocol Pony {}

public class Teddy: Pony {}

public struct Paddock<P: Pony> {}

public struct Barn<T: Teddy> {
  // CHECK-LABEL: Barn.foo@
  // CHECK: Generic signature: <T, S where T : Teddy>
  public func foo<S>(_: S, _: Barn<T>, _: Paddock<T>) {}
}


public class Animal { }

@available(*, unavailable, message: "Not a pony")
extension Animal: Pony { }

public struct AnimalWrapper<Friend: Animal> { }

// CHECK-LABEL: ExtensionDecl line={{.*}} base=AnimalWrapper

// FIXME: This should be <Friend where Friend : Animal, Friend : Pony>
// taking into account conformance availability.

// CHECK-NEXT: Generic signature: <Friend where Friend : Animal>
extension AnimalWrapper: Pony where Friend: Pony { }