Skip to content

Commit bc686e7

Browse files
committed
[Diagnostics] Change phrasing of the existential_requires_any warning
The previous warning "must be explicitly marked as 'any'" isn't clear if you don't already know the feature, this new phrasing should make it clearer by including the correct spelling of the type as an example. Fixes rdar://90384448
1 parent 5361369 commit bc686e7

11 files changed

+44
-42
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4737,8 +4737,8 @@ ERROR(any_not_existential,none,
47374737
"'any' has no effect on %select{concrete type|type parameter}0 %1",
47384738
(bool, Type))
47394739
ERROR(existential_requires_any,none,
4740-
"%select{protocol |}1%0 as a type must be explicitly marked as 'any'",
4741-
(Type, bool))
4740+
"use of %select{protocol |}2%0 as a type must be written %1",
4741+
(Type, Type, bool))
47424742

47434743
ERROR(nonisolated_let,none,
47444744
"'nonisolated' is meaningless on 'let' declarations because "

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4254,6 +4254,7 @@ class ExistentialTypeVisitor
42544254
Ctx.Diags.diagnose(comp->getNameLoc(),
42554255
diag::existential_requires_any,
42564256
proto->getDeclaredInterfaceType(),
4257+
proto->getExistentialType(),
42574258
/*isAlias=*/false)
42584259
.limitBehavior(DiagnosticBehavior::Warning)
42594260
.fixItReplace(replaceRepr->getSourceRange(), fix);
@@ -4273,6 +4274,7 @@ class ExistentialTypeVisitor
42734274
Ctx.Diags.diagnose(comp->getNameLoc(),
42744275
diag::existential_requires_any,
42754276
alias->getDeclaredInterfaceType(),
4277+
ExistentialType::get(alias->getDeclaredInterfaceType()),
42764278
/*isAlias=*/true)
42774279
.limitBehavior(DiagnosticBehavior::Warning)
42784280
.fixItReplace(replaceRepr->getSourceRange(), fix);

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 {{protocol 'EqualComparable' as a type must be explicitly marked as 'any'}}
37+
var eqComp : EqualComparable = t1 // expected-warning {{use of protocol 'EqualComparable' as a type must be written 'any EqualComparable'}}
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/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 {{protocol 'InnerProtocol' as a type must be explicitly marked as 'any'}}
34+
func f() { let _ = InnerProtocol.self } // expected-warning {{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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ class B : A {
167167
}
168168

169169
func testB(_ b: B) {
170-
var _: P1 = b // expected-warning {{protocol 'P1' as a type must be explicitly marked as 'any'}}
171-
var _: P4 = b // expected-warning {{protocol 'P4' as a type must be explicitly marked as 'any'}}
170+
var _: P1 = b // expected-warning {{use of protocol 'P1' as a type must be written 'any P1'}}
171+
var _: P4 = b // expected-warning {{use of protocol 'P4' as a type must be written 'any P4'}}
172172
var _: P5 = b
173173
var _: P6 = b
174-
var _: P7 = b // expected-warning {{protocol 'P7' as a type must be explicitly marked as 'any'}}
174+
var _: P7 = b // expected-warning {{use of protocol 'P7' as a type must be written 'any P7'}}
175175
var _: P8 = b
176-
var _: P9 = b // expected-warning {{protocol 'P9' as a type must be explicitly marked as 'any'}}
176+
var _: P9 = b // expected-warning {{use of protocol 'P9' as a type must be written 'any P9'}}
177177
}
178178

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

test/decl/protocol/existential_member_accesses_self_assoctype.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ do {
805805
let _: any Class<Struct<Bool>.Inner> & ConcreteAssocTypes =
806806
arg[
807807
// FIXME: Sema thinks (any ConcreteAssocTypes).self is a function ref.
808-
// expected-warning@+1 {{protocol 'ConcreteAssocTypes' as a type must be explicitly marked as 'any'}}
808+
// expected-warning@+1 {{use of protocol 'ConcreteAssocTypes' as a type must be written 'any ConcreteAssocTypes'}}
809809
subscript4: Struct<Bool>(), ConcreteAssocTypes.self, { true }
810810
]
811811
}

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 {{protocol 'P' as a type must be explicitly marked as 'any'}}
15+
func test(p: P) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P>}} {{none}}
1717
}
1818
}
1919
do {
20-
func test(p: ((P))) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
20+
func test(p: ((P))) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P>}} {{none}}
2222
}
2323
}
@@ -57,12 +57,12 @@ do {
5757
}
5858
}
5959
do {
60-
func test(p: P.Type) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
60+
func test(p: P.Type) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P>}} {{none}}
6262
}
6363
}
6464
do {
65-
func test(p: (P).Type) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
65+
func test(p: (P).Type) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P>}} {{none}}
6767
}
6868
}
@@ -78,12 +78,12 @@ do {
7878
}
7979

8080
do {
81-
func test(p: P & Q) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
81+
func test(p: P & Q) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P & Q>}} {{none}}
8383
}
8484
}
8585
do {
86-
func test(p: ((P & Q))) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
86+
func test(p: ((P & Q))) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P & Q>}} {{none}}
8888
}
8989
}
@@ -123,12 +123,12 @@ do {
123123
}
124124
}
125125
do {
126-
func test(p: (P & Q).Type) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
126+
func test(p: (P & Q).Type) { // expected-warning {{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:16--1:23=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: P & Q>}} {{none}}
128128
}
129129
}
130130
do {
131-
func test(p: ((P & Q)).Type) { // expected-warning {{protocol 'P' as a type must be explicitly marked as 'any'}}
131+
func test(p: ((P & Q)).Type) { // expected-warning {{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=<#generic parameter name#>}} {{-1:12--1:12=<<#generic parameter name#>: 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 {{protocol 'P1' as a type must be explicitly marked as 'any'}}
449+
return x is P1 // expected-warning {{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 {{protocol 'AsExistentialAssocTypeB' as a type must be explicitly marked as 'any'}}
94+
var delegate : AsExistentialAssocTypeB? { get } // expected-warning {{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 {{protocol 'AsExistentialAssocTypeAgainA' as a type must be explicitly marked as 'any'}}
106+
func aMethod(_ object : AsExistentialAssocTypeAgainA) // expected-warning {{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{{protocol 'Input' as a type must be explicitly marked as 'any'}}{{30-35=any Input}}
248-
// expected-warning@+1{{protocol 'Output' as a type must be explicitly marked as 'any'}}{{40-46=any Output}}
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}}
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{{'Constraint' (aka 'Input') as a type must be explicitly marked as 'any'}}{{29-39=any Constraint}}
256+
func testConstraintAlias(x: Constraint) {} // expected-warning{{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,25 +287,25 @@ func testAnyFixIt() {
287287
func method() -> any HasAssoc {}
288288
}
289289

290-
// expected-warning@+1 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-18=any HasAssoc}}
290+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{19-27=any HasAssoc}}
292+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-23=any HasAssoc.Type}}
294+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-25=any (HasAssoc).Type}}
296+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-27=any ((HasAssoc)).Type}}
298+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-18=(any HasAssoc)}}
301-
// expected-warning@+1 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{30-38=(any HasAssoc)}}
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)}}
302302
let _: HasAssoc.Protocol = HasAssoc.self
303-
// expected-warning@+1 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{11-19=any HasAssoc}}
303+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-18=(any HasAssoc)}}
305+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-23=(any HasAssoc.Type)}}
307+
// expected-warning@+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 {{'HasAssoc' as a type must be explicitly marked as 'any'}}{{10-18=(any HasAssoc)}}
309+
// expected-warning@+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
}

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 {{protocol 'HasSelfRequirements' as a type must be explicitly marked as 'any'}}
6+
func returnsOwnProtocol() -> HasSelfRequirements // expected-warning {{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 {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
77+
case let dbl as HasAssoc: // expected-warning {{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 {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
86+
func testHasAssoc(_ x: Any, _: HasAssoc) { // expected-warning {{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 {{protocol 'HasAs
9292
typealias Assoc = Int
9393
func foo() {}
9494

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

9999
// SR-38
100-
var b: HasAssoc // expected-warning {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
100+
var b: HasAssoc // expected-warning {{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 {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
121+
where T : HasAssoc, T.Assoc == HasAssoc // expected-warning {{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 {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
127+
protocol ProtocolWhere where T == HasAssoc { // expected-warning {{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 {{protocol 'HasAssoc' as a type must be explicitly marked as 'any'}}
134+
extension HasAssoc where Assoc == HasAssoc {} // expected-warning {{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)