Skip to content

Commit ccbfdbe

Browse files
authored
Merge pull request #42558 from hborla/se-0309-require-any
[Sema] Require `any` for SE-0309 existential types.
2 parents 9c7e5c9 + 0e7594a commit ccbfdbe

11 files changed

+44
-48
lines changed

lib/Sema/TypeCheckType.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4306,7 +4306,6 @@ class ExistentialTypeVisitor
43064306
proto->getDeclaredInterfaceType(),
43074307
proto->getExistentialType(),
43084308
/*isAlias=*/false)
4309-
.limitBehavior(DiagnosticBehavior::Warning)
43104309
.fixItReplace(replaceRepr->getSourceRange(), fix);
43114310
}
43124311
} else if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(comp->getBoundDecl())) {
@@ -4325,7 +4324,6 @@ class ExistentialTypeVisitor
43254324
alias->getDeclaredInterfaceType(),
43264325
ExistentialType::get(alias->getDeclaredInterfaceType()),
43274326
/*isAlias=*/true)
4328-
.limitBehavior(DiagnosticBehavior::Warning)
43294327
.fixItReplace(replaceRepr->getSourceRange(), fix);
43304328
}
43314329
}

test/Generics/function_defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func min<T : MethodLessComparable>(_ x: T, y: T) -> T {
3434
//===----------------------------------------------------------------------===//
3535

3636
func existential<T : EqualComparable, U : EqualComparable>(_ t1: T, t2: T, u: U) {
37-
var eqComp : EqualComparable = t1 // expected-warning {{use of protocol 'EqualComparable' as a type must be written 'any EqualComparable'}}
37+
var eqComp : any EqualComparable = t1
3838
eqComp = u
3939
if t1.isEqual(eqComp) {} // expected-error{{cannot convert value of type 'any EqualComparable' to expected argument type 'T'}}
4040
if eqComp.isEqual(t2) {}

test/SILGen/dependent_member_lowering.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protocol P1: P0 where Foo == String {
3131
}
3232

3333
// CHECK-LABEL: sil hidden [ossa] @$s25dependent_member_lowering26existentialDependentMemberySSAA2P1_pF : $@convention(thin) (@in_guaranteed P1) -> @owned String
34-
func existentialDependentMember(_ p1: P1) -> String {
34+
func existentialDependentMember(_ p1: any P1) -> String {
3535
// CHECK: $@convention(witness_method: P0) <τ_0_0 where τ_0_0 : P0> (@in_guaranteed τ_0_0) -> @out τ_0_0.Foo
3636
return p1.foo()
3737
}

test/decl/nested/protocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protocol OuterProtocol {
3131
struct ConformsToOuterProtocol : OuterProtocol {
3232
typealias Hen = Int
3333

34-
func f() { let _ = InnerProtocol.self } // expected-warning {{use of protocol 'InnerProtocol' as a type must be written 'any InnerProtocol'}}
34+
func f() { let _ = InnerProtocol.self } // expected-error {{use of protocol 'InnerProtocol' as a type must be written 'any InnerProtocol'}}
3535
}
3636

3737
protocol Racoon {

test/decl/protocol/conforms/inherited.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,13 +177,13 @@ class B : A {
177177
}
178178

179179
func testB(_ b: B) {
180-
var _: P1 = b // expected-warning {{use of protocol 'P1' as a type must be written 'any P1'}}
181-
var _: P4 = b // expected-warning {{use of protocol 'P4' as a type must be written 'any P4'}}
182-
var _: P5 = b
183-
var _: P6 = b
184-
var _: P7 = b // expected-warning {{use of protocol 'P7' as a type must be written 'any P7'}}
185-
var _: P8 = b
186-
var _: P9 = b // expected-warning {{use of protocol 'P9' as a type must be written 'any P9'}}
180+
var _: any P1 = b
181+
var _: any P4 = b
182+
var _: any P5 = b
183+
var _: any P6 = b
184+
var _: any P7 = b
185+
var _: any P8 = b
186+
var _: any P9 = b
187187
}
188188

189189
// Class A5 conforms to P5 in an inheritable manner.

test/decl/protocol/existential_member_accesses_self_assoctype.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -802,9 +802,7 @@ do {
802802

803803
let _: any Class<Struct<Bool>.Inner> & ConcreteAssocTypes =
804804
arg[
805-
// FIXME: Sema thinks (any ConcreteAssocTypes).self is a function ref.
806-
// expected-warning@+1 {{use of protocol 'ConcreteAssocTypes' as a type must be written 'any ConcreteAssocTypes'}}
807-
subscript4: Struct<Bool>(), ConcreteAssocTypes.self, { true }
805+
subscript4: Struct<Bool>(), (any ConcreteAssocTypes).self, { true }
808806
]
809807
}
810808
}

test/decl/protocol/existential_member_accesses_self_assoctype_fixit.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ protocol P {
1212
protocol Q {}
1313

1414
do {
15-
func test(p: P) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
15+
func test(p: P) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
1616
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P'; consider using a generic constraint instead}} {{-1:16--1:17=some P}} {{none}}
1717
}
1818
}
1919
do {
20-
func test(p: ((P))) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
20+
func test(p: ((P))) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
2121
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P'; consider using a generic constraint instead}} {{-1:18--1:19=some P}} {{none}}
2222
}
2323
}
@@ -57,12 +57,12 @@ do {
5757
}
5858
}
5959
do {
60-
func test(p: P.Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
60+
func test(p: P.Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
6161
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any P.Type'; consider using a generic constraint instead}} {{-1:16--1:17=(some P)}} {{none}}
6262
}
6363
}
6464
do {
65-
func test(p: (P).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
65+
func test(p: (P).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
6666
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any (P).Type'; consider using a generic constraint instead}} {{-1:17--1:18=some P}} {{none}}
6767
}
6868
}
@@ -78,12 +78,12 @@ do {
7878
}
7979

8080
do {
81-
func test(p: P & Q) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
81+
func test(p: P & Q) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
8282
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P & Q'; consider using a generic constraint instead}} {{-1:16--1:21=some P & Q}} {{none}}
8383
}
8484
}
8585
do {
86-
func test(p: ((P & Q))) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
86+
func test(p: ((P & Q))) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
8787
p.method(false) // expected-error {{member 'method' cannot be used on value of type 'any P & Q'; consider using a generic constraint instead}} {{-1:18--1:23=some P & Q}} {{none}}
8888
}
8989
}
@@ -123,12 +123,12 @@ do {
123123
}
124124
}
125125
do {
126-
func test(p: (P & Q).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
126+
func test(p: (P & Q).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
127127
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any (P & Q).Type'; consider using a generic constraint instead}} {{-1:17--1:22=some P & Q}} {{none}}
128128
}
129129
}
130130
do {
131-
func test(p: ((P & Q)).Type) { // expected-warning {{use of protocol 'P' as a type must be written 'any P'}}
131+
func test(p: ((P & Q)).Type) { // expected-error {{use of protocol 'P' as a type must be written 'any P'}}
132132
p.staticMethod(false) // expected-error {{member 'staticMethod' cannot be used on value of type 'any ((P & Q)).Type'; consider using a generic constraint instead}} {{-1:18--1:23=some P & Q}} {{none}}
133133
}
134134
}

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func i<T : C3>(_ x : T?) -> Bool {
446446
// expected-warning@-1 {{checking a value with optional type 'T?' against type 'any P1' succeeds whenever the value is non-nil; did you mean to use '!= nil'?}}
447447
}
448448
func j(_ x : C1) -> Bool {
449-
return x is P1 // expected-warning {{use of protocol 'P1' as a type must be written 'any P1'}}
449+
return x is P1 // expected-error {{use of protocol 'P1' as a type must be written 'any P1'}}
450450
}
451451
func k(_ x : C1?) -> Bool {
452452
return x is any P1

test/decl/protocol/recursive_requirement.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protocol AsExistentialB {
9191
}
9292

9393
protocol AsExistentialAssocTypeA {
94-
var delegate : AsExistentialAssocTypeB? { get } // expected-warning {{use of protocol 'AsExistentialAssocTypeB' as a type must be written 'any AsExistentialAssocTypeB'}}
94+
var delegate : AsExistentialAssocTypeB? { get } // expected-error {{use of protocol 'AsExistentialAssocTypeB' as a type must be written 'any AsExistentialAssocTypeB'}}
9595
}
9696
protocol AsExistentialAssocTypeB {
9797
func aMethod(_ object : AsExistentialAssocTypeA)
@@ -103,7 +103,7 @@ protocol AsExistentialAssocTypeAgainA {
103103
associatedtype Bar
104104
}
105105
protocol AsExistentialAssocTypeAgainB {
106-
func aMethod(_ object : AsExistentialAssocTypeAgainA) // expected-warning {{use of protocol 'AsExistentialAssocTypeAgainA' as a type must be written 'any AsExistentialAssocTypeAgainA'}}
106+
func aMethod(_ object : AsExistentialAssocTypeAgainA) // expected-error {{use of protocol 'AsExistentialAssocTypeAgainA' as a type must be written 'any AsExistentialAssocTypeAgainA'}}
107107
}
108108

109109
// SR-547

test/type/explicit_existential.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,16 @@ protocol Output {
244244
associatedtype A
245245
}
246246

247-
// expected-warning@+2{{use of protocol 'Input' as a type must be written 'any Input'}}{{30-35=any Input}}
248-
// expected-warning@+1{{use of protocol 'Output' as a type must be written 'any Output'}}{{40-46=any Output}}
247+
// expected-error@+2{{use of protocol 'Input' as a type must be written 'any Input'}}{{30-35=any Input}}
248+
// expected-error@+1{{use of protocol 'Output' as a type must be written 'any Output'}}{{40-46=any Output}}
249249
typealias InvalidFunction = (Input) -> Output
250250
func testInvalidFunctionAlias(fn: InvalidFunction) {}
251251

252252
typealias ExistentialFunction = (any Input) -> any Output
253253
func testFunctionAlias(fn: ExistentialFunction) {}
254254

255255
typealias Constraint = Input
256-
func testConstraintAlias(x: Constraint) {} // expected-warning{{use of 'Constraint' (aka 'Input') as a type must be written 'any Constraint'}}{{29-39=any Constraint}}
256+
func testConstraintAlias(x: Constraint) {} // expected-error{{use of 'Constraint' (aka 'Input') as a type must be written 'any Constraint'}}{{29-39=any Constraint}}
257257

258258
typealias Existential = any Input
259259
func testExistentialAlias(x: Existential, y: any Constraint) {}
@@ -287,26 +287,26 @@ func testAnyFixIt() {
287287
func method() -> any HasAssoc {}
288288
}
289289

290-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=any HasAssoc}}
290+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=any HasAssoc}}
291291
let _: HasAssoc = ConformingType()
292-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{19-27=any HasAssoc}}
292+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{19-27=any HasAssoc}}
293293
let _: Optional<HasAssoc> = nil
294-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=any HasAssoc.Type}}
294+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=any HasAssoc.Type}}
295295
let _: HasAssoc.Type = ConformingType.self
296-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-25=any (HasAssoc).Type}}
296+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-25=any (HasAssoc).Type}}
297297
let _: (HasAssoc).Type = ConformingType.self
298-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-27=any ((HasAssoc)).Type}}
298+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-27=any ((HasAssoc)).Type}}
299299
let _: ((HasAssoc)).Type = ConformingType.self
300-
// expected-warning@+2 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
301-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{30-38=(any HasAssoc)}}
300+
// expected-error@+2 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
301+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{30-38=(any HasAssoc)}}
302302
let _: HasAssoc.Protocol = HasAssoc.self
303-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{11-19=any HasAssoc}}
303+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{11-19=any HasAssoc}}
304304
let _: (HasAssoc).Protocol = (any HasAssoc).self
305-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
305+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
306306
let _: HasAssoc? = ConformingType()
307-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=(any HasAssoc.Type)}}
307+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-23=(any HasAssoc.Type)}}
308308
let _: HasAssoc.Type? = ConformingType.self
309-
// expected-warning@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
309+
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}{{10-18=(any HasAssoc)}}
310310
let _: HasAssoc.Protocol? = (any HasAssoc).self
311311

312312
// expected-error@+1 {{optional 'any' type must be written '(any HasAssoc)?'}}{{10-23=(any HasAssoc)?}}

test/type/protocol_types.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
protocol HasSelfRequirements {
44
func foo(_ x: Self)
55

6-
func returnsOwnProtocol() -> HasSelfRequirements // expected-warning {{use of protocol 'HasSelfRequirements' as a type must be written 'any HasSelfRequirements'}}
6+
func returnsOwnProtocol() -> HasSelfRequirements // expected-error {{use of protocol 'HasSelfRequirements' as a type must be written 'any HasSelfRequirements'}}
77
}
88
protocol Bar {
99
// init() methods should not prevent use as an existential.
@@ -74,7 +74,7 @@ do {
7474

7575
func checkIt(_ js: Any) throws {
7676
switch js {
77-
case let dbl as HasAssoc: // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
77+
case let dbl as HasAssoc: // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
7878
throw MyError.bad(dbl)
7979

8080
default:
@@ -83,7 +83,7 @@ do {
8383
}
8484
}
8585

86-
func testHasAssoc(_ x: Any, _: HasAssoc) { // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
86+
func testHasAssoc(_ x: Any, _: HasAssoc) { // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
8787
if let p = x as? any HasAssoc {
8888
p.foo() // don't crash here.
8989
}
@@ -92,12 +92,12 @@ func testHasAssoc(_ x: Any, _: HasAssoc) { // expected-warning {{use of protocol
9292
typealias Assoc = Int
9393
func foo() {}
9494

95-
func method() -> HasAssoc {} // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
95+
func method() -> HasAssoc {} // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
9696
}
9797
}
9898

9999
// SR-38
100-
var b: HasAssoc // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
100+
var b: HasAssoc // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
101101

102102
// Further generic constraint error testing - typealias used inside statements
103103
protocol P {}
@@ -118,20 +118,20 @@ typealias X = Struct1<Pub & Bar>
118118
_ = Struct1<Pub & Bar>.self
119119

120120
typealias AliasWhere<T> = T
121-
where T : HasAssoc, T.Assoc == HasAssoc // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
121+
where T : HasAssoc, T.Assoc == HasAssoc // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
122122

123123
struct StructWhere<T>
124124
where T : HasAssoc,
125125
T.Assoc == any HasAssoc {}
126126

127-
protocol ProtocolWhere where T == HasAssoc { // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
127+
protocol ProtocolWhere where T == HasAssoc { // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
128128
associatedtype T
129129

130130
associatedtype U : HasAssoc
131131
where U.Assoc == any HasAssoc
132132
}
133133

134-
extension HasAssoc where Assoc == HasAssoc {} // expected-warning {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
134+
extension HasAssoc where Assoc == HasAssoc {} // expected-error {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
135135

136136
func FunctionWhere<T>(_: T)
137137
where T : HasAssoc,

0 commit comments

Comments
 (0)