Skip to content

Commit d8e5733

Browse files
[NFC] Merge test/type/explicit_existential & test/type/explicit_existential_swift6
1 parent 3c855da commit d8e5733

File tree

2 files changed

+54
-570
lines changed

2 files changed

+54
-570
lines changed

test/type/explicit_existential.swift

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature NoncopyableGenerics
1+
// RUN: %target-typecheck-verify-swift -verify-additional-prefix default-swift-mode-
2+
// RUN: %target-typecheck-verify-swift -swift-version 6 -verify-additional-prefix swift-6-
3+
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ExistentialAny -verify-additional-prefix explicit-any- -verify-additional-prefix default-swift-mode-
4+
25

36
protocol HasSelfRequirements {
47
func foo(_ x: Self)
@@ -11,6 +14,11 @@ protocol Bar {
1114
func bar() -> any Bar
1215
}
1316

17+
class Bistro {
18+
convenience init(_: Bar){ self.init()} // expected-explicit-any-error{{use of protocol 'Bar' as a type must be written 'any Bar'}}{{23-26=any Bar}}
19+
class func returnBar() -> Bar {} // expected-explicit-any-error {{use of protocol 'Bar' as a type must be written 'any Bar'}}{{29-32=any Bar}}
20+
}
21+
1422
func useBarAsType(_ x: any Bar) {}
1523

1624
protocol Pub : Bar { }
@@ -64,7 +72,7 @@ protocol HasAssoc {
6472

6573
do {
6674
enum MyError: Error {
67-
case bad(Any)
75+
case bad(Any) // expected-swift-6-error {{associated value 'bad' of 'Sendable'-conforming enum 'MyError' has non-sendable type 'Any'}}
6876
}
6977

7078
func checkIt(_ js: Any) throws {
@@ -88,6 +96,8 @@ func testHasAssoc(_ x: Any, _: any HasAssoc) {
8896
func foo() {}
8997

9098
func method() -> any HasAssoc {}
99+
func existentialArray() -> [any HasAssoc] {}
100+
func existentialcSequence() -> any Sequence<any HasAssoc> {}
91101
}
92102
}
93103

@@ -137,6 +147,21 @@ struct OuterGeneric<T> {
137147
func contextuallyGenericMethod() where T == any HasAssoc {}
138148
}
139149

150+
protocol Collection<T> {
151+
associatedtype T
152+
}
153+
154+
struct TestParameterizedProtocol<T> : Collection {
155+
typealias T = T
156+
157+
let x : Collection<T> // expected-error {{use of protocol 'Collection<T>' as a type must be written 'any Collection<T>'}}
158+
}
159+
160+
func acceptAny(_: Collection<Int>) {}
161+
// expected-error@-1 {{use of protocol 'Collection<Int>' as a type must be written 'any Collection<Int>'}}
162+
func returnsAny() -> Collection<Int> {}
163+
// expected-error@-1 {{use of protocol 'Collection<Int>' as a type must be written 'any Collection<Int>'}}
164+
140165
func testInvalidAny() {
141166
struct S: HasAssoc {
142167
typealias Assoc = Int
@@ -189,7 +214,7 @@ protocol RawRepresentable {
189214
enum E1: RawRepresentable {
190215
typealias RawValue = P1
191216

192-
var rawValue: P1 {
217+
var rawValue: P1 { // expected-explicit-any-error {{use of protocol 'P1' as a type must be written 'any P1'}}{{17-19=any P1}}
193218
return ConcreteComposition()
194219
}
195220
}
@@ -237,6 +262,10 @@ func hasInvalidExistential(_: any DoesNotExistIHope) {}
237262
protocol Input {
238263
associatedtype A
239264
}
265+
protocol InputB {
266+
associatedtype B
267+
}
268+
240269
protocol Output {
241270
associatedtype A
242271
}
@@ -250,7 +279,11 @@ typealias ExistentialFunction = (any Input) -> any Output
250279
func testFunctionAlias(fn: ExistentialFunction) {}
251280

252281
typealias Constraint = Input
253-
func testConstraintAlias(x: Constraint) {} // expected-error{{use of 'Constraint' (aka 'Input') as a type must be written 'any Constraint'}}{{29-39=any Constraint}}
282+
typealias ConstraintB = Input & InputB
283+
284+
//expected-error@+2{{use of 'Constraint' (aka 'Input') as a type must be written 'any Constraint' (aka 'any Input')}}
285+
//expected-error@+1 {{use of 'ConstraintB' (aka 'Input & InputB') as a type must be written 'any ConstraintB' (aka 'any Input & InputB')}}
286+
func testConstraintAlias(x: Constraint, y: ConstraintB) {}
254287

255288
typealias Existential = any Input
256289
func testExistentialAlias(x: Existential, y: any Constraint) {}
@@ -278,7 +311,9 @@ enum EE : Equatable, any Empty { // expected-error {{raw type 'any Empty' is not
278311

279312
// Protocols from a serialized module (the standard library).
280313
do {
314+
// expected-explicit-any-error@+1 {{use of protocol 'Decodable' as a type must be written 'any Decodable'}}
281315
let _: Decodable
316+
// expected-explicit-any-error@+1 {{use of 'Codable' (aka 'Decodable & Encodable') as a type must be written 'any Codable' (aka 'any Decodable & Encodable')}}
282317
let _: Codable
283318
}
284319

@@ -421,8 +456,9 @@ func testAnyFixIt() {
421456
let _: (~Copyable)?
422457
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
423458
let _: HasAssoc!
424-
// expected-error@+2 {{type '(any Copyable)?' cannot be suppressed}}
425-
// expected-warning@+1 {{using '!' is not allowed here; treating this as '?' instead}}
459+
// expected-default-swift-mode-warning@+3 {{using '!' is not allowed here; treating this as '?' instead}}
460+
// expected-swift-6-error@+2 {{using '!' is not allowed here; perhaps '?' was intended?}} {{19-20=?}}
461+
// expected-error@+1 {{type '(any Copyable)?' cannot be suppressed}}
426462
let _: ~Copyable!
427463
// expected-error@+1 {{constraint that suppresses conformance requires 'any'}}{{11-20=any ~Copyable}}
428464
let _: (~Copyable)!
@@ -503,6 +539,7 @@ func testEnumAssociatedValue() {
503539
case c1((any HasAssoc) -> Void)
504540
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
505541
case c2((HasAssoc) -> Void)
542+
// expected-explicit-any-error@+1 {{use of protocol 'P' as a type must be written 'any P'}}
506543
case c3((P) -> Void)
507544
}
508545
}
@@ -518,3 +555,14 @@ protocol PP {}
518555
struct A : PP {}
519556
let _: any PP = A() // Ok
520557
let _: any (any PP) = A() // expected-error{{redundant 'any' in type 'any (any PP)'}} {{8-12=}}
558+
559+
// coverage for rdar://123332844
560+
let x: Any.Type = AnyObject.self
561+
let y: Any.Type = Any.self
562+
563+
typealias Objectlike = AnyObject
564+
func f(_ x: Objectlike) {}
565+
566+
typealias Copy = Copyable
567+
func h(_ z1: Copy, // expected-explicit-any-error {{use of 'Copy' (aka 'Copyable') as a type must be written 'any Copy' (aka 'any Copyable')}}
568+
_ z2: Copyable) {} // expected-explicit-any-error {{use of protocol 'Copyable' as a type must be written 'any Copyable'}}

0 commit comments

Comments
 (0)