Skip to content

Commit a27110f

Browse files
committed
AST: Add a diagnostic group for any syntax diagnostics
Allows users to unconditionally escalate `ExistentialAny` warnings to errors with `-Werror <group>`.
1 parent adc2006 commit a27110f

File tree

4 files changed

+35
-15
lines changed

4 files changed

+35
-15
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ GROUP(DeprecatedDeclaration, "DeprecatedDeclaration.md")
2626
GROUP(Unsafe, "Unsafe.md")
2727
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")
2828
GROUP(DeclarationUnavailableFromAsynchronousContext, "DeclarationUnavailableFromAsynchronousContext.md")
29+
GROUP(ExistentialAny, "ExistentialAny.md")
2930

3031
#define UNDEFINE_DIAGNOSTIC_GROUPS_MACROS
3132
#include "swift/AST/DefineDiagnosticGroupsMacros.h"

include/swift/AST/DiagnosticsSema.def

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5819,11 +5819,12 @@ ERROR(any_not_existential,none,
58195819
ERROR(incorrect_optional_any,none,
58205820
"optional 'any' type must be written %0",
58215821
(Type))
5822-
ERROR(existential_requires_any,none,
5823-
"use of %select{protocol |}2%0 as a type must be written %1",
5824-
(Type, Type, bool))
5825-
ERROR(inverse_requires_any,none,
5826-
"constraint that suppresses conformance requires 'any'", ())
5822+
5823+
GROUPED_ERROR(existential_requires_any,ExistentialAny,none,
5824+
"use of %select{protocol |}2%0 as a type must be written %1",
5825+
(Type, Type, bool))
5826+
GROUPED_ERROR(inverse_requires_any,ExistentialAny,none,
5827+
"constraint that suppresses conformance requires 'any'", ())
58275828

58285829
ERROR(nonisolated_mutable_storage,none,
58295830
"'nonisolated' cannot be applied to mutable stored properties",

test/type/explicit_existential.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// RUN: %target-typecheck-verify-swift -verify-additional-prefix default-swift-mode-
22
// 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-
// RUN: %target-typecheck-verify-swift -enable-experimental-feature ExistentialAny -verify-additional-prefix explicit-any- -verify-additional-prefix default-swift-mode-
3+
// RUN: %target-typecheck-verify-swift -print-diagnostic-groups -enable-upcoming-feature ExistentialAny -verify-additional-prefix explicit-any- -verify-additional-prefix default-swift-mode-
4+
// RUN: %target-typecheck-verify-swift -print-diagnostic-groups -enable-experimental-feature ExistentialAny -verify-additional-prefix explicit-any- -verify-additional-prefix default-swift-mode-
55

66
// REQUIRES: swift_feature_ExistentialAny
77

@@ -19,9 +19,9 @@ protocol Bar {
1919

2020
class Bistro {
2121
convenience init(_: Bar){ self.init()}
22-
// expected-explicit-any-warning@-1 {{use of protocol 'Bar' as a type must be written 'any Bar'; this will be an error in a future Swift language mode}}{{23-26=any Bar}}
22+
// expected-explicit-any-warning@-1 {{use of protocol 'Bar' as a type must be written 'any Bar'; this will be an error in a future Swift language mode [ExistentialAny]}}{{23-26=any Bar}}
2323
class func returnBar() -> Bar {}
24-
// expected-explicit-any-warning@-1 {{use of protocol 'Bar' as a type must be written 'any Bar'; this will be an error in a future Swift language mode}}{{29-32=any Bar}}
24+
// expected-explicit-any-warning@-1 {{use of protocol 'Bar' as a type must be written 'any Bar'; this will be an error in a future Swift language mode [ExistentialAny]}}{{29-32=any Bar}}
2525
}
2626

2727
func useBarAsType(_ x: any Bar) {}
@@ -220,7 +220,7 @@ enum E1: RawRepresentable {
220220
typealias RawValue = P1
221221

222222
var rawValue: P1 {
223-
// expected-explicit-any-warning@-1 {{use of protocol 'P1' as a type must be written 'any P1'; this will be an error in a future Swift language mode}}{{17-19=any P1}}
223+
// expected-explicit-any-warning@-1 {{use of protocol 'P1' as a type must be written 'any P1'; this will be an error in a future Swift language mode [ExistentialAny]}}{{17-19=any P1}}
224224
return ConcreteComposition()
225225
}
226226
}
@@ -318,9 +318,9 @@ enum EE : Equatable, any Empty { // expected-error {{raw type 'any Empty' is not
318318

319319
// Protocols from a serialized module (the standard library).
320320
do {
321-
// expected-explicit-any-warning@+1 {{use of protocol 'Decodable' as a type must be written 'any Decodable'; this will be an error in a future Swift language mode}}
321+
// expected-explicit-any-warning@+1 {{use of protocol 'Decodable' as a type must be written 'any Decodable'; this will be an error in a future Swift language mode [ExistentialAny]}}
322322
let _: Decodable
323-
// expected-explicit-any-warning@+1 {{use of 'Codable' (aka 'Decodable & Encodable') as a type must be written 'any Codable' (aka 'any Decodable & Encodable'); this will be an error in a future Swift language mode}}
323+
// expected-explicit-any-warning@+1 {{use of 'Codable' (aka 'Decodable & Encodable') as a type must be written 'any Codable' (aka 'any Decodable & Encodable'); this will be an error in a future Swift language mode [ExistentialAny]}}
324324
let _: Codable
325325
}
326326

@@ -546,7 +546,7 @@ func testEnumAssociatedValue() {
546546
case c1((any HasAssoc) -> Void)
547547
// expected-error@+1 {{use of protocol 'HasAssoc' as a type must be written 'any HasAssoc'}}
548548
case c2((HasAssoc) -> Void)
549-
// expected-explicit-any-warning@+1 {{use of protocol 'P' as a type must be written 'any P'; this will be an error in a future Swift language mode}}
549+
// expected-explicit-any-warning@+1 {{use of protocol 'P' as a type must be written 'any P'; this will be an error in a future Swift language mode [ExistentialAny]}}
550550
case c3((P) -> Void)
551551
}
552552
}
@@ -572,6 +572,6 @@ func f(_ x: Objectlike) {}
572572

573573
typealias Copy = Copyable
574574
func h(_ z1: Copy,
575-
// expected-explicit-any-warning@-1 {{use of 'Copy' (aka 'Copyable') as a type must be written 'any Copy' (aka 'any Copyable'); this will be an error in a future Swift language mode}}
575+
// expected-explicit-any-warning@-1 {{use of 'Copy' (aka 'Copyable') as a type must be written 'any Copy' (aka 'any Copyable'); this will be an error in a future Swift language mode [ExistentialAny]}}
576576
_ z2: Copyable) {}
577-
// expected-explicit-any-warning@-1 {{use of protocol 'Copyable' as a type must be written 'any Copyable'; this will be an error in a future Swift language mode}}
577+
// expected-explicit-any-warning@-1 {{use of protocol 'Copyable' as a type must be written 'any Copyable'; this will be an error in a future Swift language mode [ExistentialAny]}}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# `ExistentialAny`
2+
3+
This diagnostic group includes type syntax errors and warnings that may arise on
4+
occurrences of protocol or protocol composition types without an `any` or `some`
5+
qualifier in type position (vs. constraint position).
6+
For example:
7+
8+
```swift
9+
struct AnyJoinedSequence<Element> {
10+
private let sequences: [Sequence<Element>] // error.
11+
}
12+
13+
extension AnyJoinedSequence: Sequence, IteratorProtocol {
14+
func next() -> Element? {
15+
fatalError("Not implemented")
16+
}
17+
}
18+
```

0 commit comments

Comments
 (0)