Skip to content

Commit 3222053

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 fe2408c commit 3222053

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

include/swift/AST/DiagnosticGroups.def

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ GROUP(Unsafe, "Unsafe.md")
2727
GROUP(UnknownWarningGroup, "UnknownWarningGroup.md")
2828
GROUP(PreconcurrencyImport, "PreconcurrencyImport.md")
2929
GROUP(StrictLanguageFeatures, "StrictLanguageFeatures.md")
30+
GROUP(ExistentialAny, "ExistentialAny.md")
3031

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

include/swift/AST/DiagnosticsSema.def

+6-5
Original file line numberDiff line numberDiff line change
@@ -5839,11 +5839,12 @@ ERROR(any_not_existential,none,
58395839
ERROR(incorrect_optional_any,none,
58405840
"optional 'any' type must be written %0",
58415841
(Type))
5842-
ERROR(existential_requires_any,none,
5843-
"use of %select{protocol |}2%0 as a type must be written %1",
5844-
(Type, Type, bool))
5845-
ERROR(inverse_requires_any,none,
5846-
"constraint that suppresses conformance requires 'any'", ())
5842+
5843+
GROUPED_ERROR(existential_requires_any,ExistentialAny,none,
5844+
"use of %select{protocol |}2%0 as a type must be written %1",
5845+
(Type, Type, bool))
5846+
GROUPED_ERROR(inverse_requires_any,ExistentialAny,none,
5847+
"constraint that suppresses conformance requires 'any'", ())
58475848

58485849
ERROR(nonisolated_mutable_storage,none,
58495850
"'nonisolated' cannot be applied to mutable stored properties",
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+
}
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)