Skip to content

Commit 146b217

Browse files
committed
AST: Add a diagnostic group for any syntax diagnostics
Allow users to escalate `any` syntax warnings to errors with `-Werror ExistentialAny`.
1 parent 466eb6d commit 146b217

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

include/swift/AST/DiagnosticGroups.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ GROUP(no_group, "")
2525
GROUP(DeprecatedDeclaration, "DeprecatedDeclaration.md")
2626
GROUP(Unsafe, "Unsafe.md")
2727
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")
28+
GROUP(ExistentialAny, "ExistentialAny.md")
2829

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

include/swift/AST/DiagnosticsSema.def

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

58265827
ERROR(nonisolated_mutable_storage,none,
58275828
"'nonisolated' cannot be applied to mutable stored properties",
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ExistentialAny -Werror ExistentialAny -swift-version 5
2+
3+
// REQUIRES: swift_feature_ExistentialAny
4+
5+
do {
6+
protocol P {}
7+
8+
let _: P
9+
// expected-error@-1{{use of protocol 'P' as a type must be written 'any P'; this will be an error in a future Swift language mode}}
10+
let _: ~Copyable
11+
// expected-error@-1 {{constraint that suppresses conformance requires 'any'; this will be an error in a future Swift language mode}}
12+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# `ExistentialAny`
2+
3+
This diagnostic group includes errors and warnings pertaining to the `any` type
4+
syntax proposed in [SE-0335].
5+
`any` syntax draws a line between constraint types and existential or boxed
6+
types.
7+
8+
For example, `any Collection` is a boxed type abstracting over a value of a
9+
dynamic type that conforms to the protocol `Collection`, whereas the
10+
`Collection` part is the conformance constraint imposed on the type of the
11+
underlying value *as well as* a constraint type.
12+
The distinction between a conformance constraint and a constraint type can be
13+
clearly seen in classic generic syntax: `<T: Collection>`.
14+
15+
Constraint types exist to express conformances and have no meaning in relation
16+
to values.
17+
18+
```swift
19+
func sillyFunction(collection: Collection) { // error
20+
// ...
21+
}
22+
```
23+
24+
[SE-0335]: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0335-existential-any.md

0 commit comments

Comments
 (0)