Skip to content

Commit e5f80d1

Browse files
authored
Merge pull request #72018 from DougGregor/implicit-open-existentials-swift6
Add upcoming feature `ImplicitOpenExistentials` for SE-0352
2 parents 9178c4a + 6075de1 commit e5f80d1

File tree

6 files changed

+47
-3
lines changed

6 files changed

+47
-3
lines changed

CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@
44
> This is in reverse chronological order, so newer entries are added to the top.
55
66
## Swift 6.0
7+
* [SE-0352][]:
8+
The Swift 6 language mode will open existential values with
9+
"self-conforming" types (such as `any Error` or `@objc` protocols)
10+
passed to generic functions. For example:
11+
12+
```swift
13+
func takeError<E: Error>(_ error: E) { }
14+
15+
func passError(error: any Error) {
16+
takeError(error) // Swift 5 does not open `any Error`, Swift 6 does
17+
}
18+
```
19+
20+
This behavior can be enabled prior to the Swift 6 language mode
21+
using the upcoming language feature `ImplicitOpenExistentials`.
22+
723
* [SE-0422][]:
824
Non-built-in expression macros can now be used as default arguments that
925
expand at each call site. For example, a custom `#CurrentFile` macro used as

include/swift/Basic/Features.def

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ SUPPRESSIBLE_LANGUAGE_FEATURE(Extern, 0, "@_extern")
129129
LANGUAGE_FEATURE(ExpressionMacroDefaultArguments, 422, "Expression macro as caller-side default argument")
130130
LANGUAGE_FEATURE(BuiltinStoreRaw, 0, "Builtin.storeRaw")
131131

132+
// Swift 6
132133
UPCOMING_FEATURE(ConciseMagicFile, 274, 6)
133134
UPCOMING_FEATURE(ForwardTrailingClosures, 286, 6)
134135
UPCOMING_FEATURE(StrictConcurrency, 0337, 6)
@@ -140,9 +141,11 @@ UPCOMING_FEATURE(InternalImportsByDefault, 409, 6)
140141
UPCOMING_FEATURE(IsolatedDefaultValues, 411, 6)
141142
UPCOMING_FEATURE(GlobalConcurrency, 412, 6)
142143
UPCOMING_FEATURE(FullTypedThrows, 413, 6)
143-
UPCOMING_FEATURE(ExistentialAny, 335, 7)
144144
UPCOMING_FEATURE(InferSendableFromCaptures, 418, 6)
145+
UPCOMING_FEATURE(ImplicitOpenExistentials, 352, 6)
145146

147+
// Swift 7
148+
UPCOMING_FEATURE(ExistentialAny, 335, 7)
146149

147150
EXPERIMENTAL_FEATURE(StaticAssert, false)
148151
EXPERIMENTAL_FEATURE(NamedOpaqueTypes, false)

lib/AST/FeatureSet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ UNINTERESTING_FEATURE(GlobalConcurrency)
439439
UNINTERESTING_FEATURE(FullTypedThrows)
440440
UNINTERESTING_FEATURE(ExistentialAny)
441441
UNINTERESTING_FEATURE(InferSendableFromCaptures)
442+
UNINTERESTING_FEATURE(ImplicitOpenExistentials)
442443

443444
// ----------------------------------------------------------------------------
444445
// MARK: - Experimental Features

lib/ASTGen/Sources/ASTGen/SourceManager.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class SourceManager {
3737
}
3838

3939
/// MARK: Source file management
40+
4041
extension SourceManager {
4142
/// Inserts a new source file into the source manager.
4243
///

lib/Sema/CSSimplify.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,10 +1578,13 @@ shouldOpenExistentialCallArgument(ValueDecl *callee, unsigned paramIdx,
15781578
return std::nullopt;
15791579

15801580
// If the existential argument conforms to all of protocol requirements on
1581-
// the formal parameter's type, don't open.
1581+
// the formal parameter's type, don't open unless ImplicitOpenExistentials is
1582+
// enabled.
1583+
15821584
// If all of the conformance requirements on the formal parameter's type
15831585
// are self-conforming, don't open.
1584-
{
1586+
ASTContext &ctx = argTy->getASTContext();
1587+
if (!ctx.LangOpts.hasFeature(Feature::ImplicitOpenExistentials)) {
15851588
Type existentialObjectType;
15861589
if (auto existentialMetaTy = argTy->getAs<ExistentialMetatypeType>())
15871590
existentialObjectType = existentialMetaTy->getInstanceType();
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %target-typecheck-verify-swift -enable-upcoming-feature ImplicitOpenExistentials
2+
// RUN: %target-typecheck-verify-swift -swift-version 6
3+
4+
#if _runtime(_ObjC)
5+
@objc
6+
protocol X {}
7+
8+
func foo<T: X>(_ val: T.Type) {}
9+
10+
func bar(_ val: X.Type) {
11+
// Only succeeds when we're allowed to open an @objc existential.
12+
foo(val)
13+
}
14+
#endif
15+
16+
func takeError<E: Error>(_ error: E) { }
17+
18+
func passError(error: any Error) {
19+
takeError(error) // okay
20+
}

0 commit comments

Comments
 (0)