Skip to content

Commit f0a69a3

Browse files
committed
prevent uses of Escapable in general
This protocol appears in the stdlib as scaffolding for the `NonescapableTypes` feature, which is still experimental and not gone through evolution as an approved addition to the stdlib. Rather than delete it from the stdlib, because it needs to still remain to support that feature work, gate references to it behind a feature flag. Additionally, prevent documentation from seeing this declaration. rdar://126705184
1 parent c90c26e commit f0a69a3

File tree

7 files changed

+26
-7
lines changed

7 files changed

+26
-7
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7708,7 +7708,7 @@ NOTE(add_explicit_protocol_for_conformance,none,
77087708
"consider making %kind0 explicitly conform to the '%1' protocol",
77097709
(const ValueDecl *, StringRef))
77107710
ERROR(escapable_requires_feature_flag,none,
7711-
"type '~Escapable' requires -enable-experimental-feature NonescapableTypes",
7711+
"type 'Escapable' requires -enable-experimental-feature NonescapableTypes",
77127712
())
77137713
ERROR(non_bitwise_copyable_type_class,none,
77147714
"class cannot conform to 'BitwiseCopyable'", ())

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,9 @@ class CheckRepressions {
124124
auto ipk = getInvertibleProtocolKind(*kp);
125125
if (ipk) {
126126
// Gate the '~Escapable' type behind a specific flag for now.
127+
// Uses of 'Escapable' itself are already diagnosed; return ErrorType.
127128
if (*ipk == InvertibleProtocolKind::Escapable &&
128129
!ctx.LangOpts.hasFeature(Feature::NonescapableTypes)) {
129-
diagnoseInvalid(repr, repr.getLoc(),
130-
diag::escapable_requires_feature_flag);
131130
return ErrorType::get(ctx);
132131
}
133132

lib/Sema/TypeCheckType.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4770,6 +4770,21 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
47704770
auto *dc = getDeclContext();
47714771
auto &ctx = getASTContext();
47724772

4773+
// Gate the 'Escapable' type behind a specific flag for now.
4774+
//
4775+
// NOTE: we do not return an ErrorType, though! We're just artificially
4776+
// preventing people from referring to the type without the feature.
4777+
if (auto proto = result->getAs<ProtocolType>()) {
4778+
if (auto known = proto->getKnownProtocol()) {
4779+
if (*known == KnownProtocolKind::Escapable
4780+
&& !isSILSourceFile()
4781+
&& !ctx.LangOpts.hasFeature(Feature::NonescapableTypes)) {
4782+
diagnoseInvalid(repr, repr->getLoc(),
4783+
diag::escapable_requires_feature_flag);
4784+
}
4785+
}
4786+
}
4787+
47734788
if (ctx.LangOpts.hasFeature(Feature::ImplicitSome) &&
47744789
options.isConstraintImplicitExistential()) {
47754790
// Check whether this type is an implicit opaque result type.
@@ -5774,10 +5789,9 @@ NeverNullType TypeResolver::resolveInverseType(InverseTypeRepr *repr,
57745789
if (auto kind = getInvertibleProtocolKind(*kp)) {
57755790

57765791
// Gate the '~Escapable' type behind a specific flag for now.
5792+
// Uses of 'Escapable' itself are already diagnosed; return ErrorType.
57775793
if (*kind == InvertibleProtocolKind::Escapable &&
57785794
!getASTContext().LangOpts.hasFeature(Feature::NonescapableTypes)) {
5779-
diagnoseInvalid(repr, repr->getLoc(),
5780-
diag::escapable_requires_feature_flag);
57815795
return wrapInExistential(ErrorType::get(getASTContext()));
57825796
}
57835797

stdlib/public/core/Misc.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {
171171

172172
@_marker public protocol Copyable {}
173173

174+
@_documentation(visibility: internal)
174175
@_marker public protocol Escapable {}
175176

176177
#if $NoncopyableGenerics && $NonescapableTypes

test/Parse/inverse_escapable_feature.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22

33

44

5-
struct S: ~Escapable {} // expected-error {{type '~Escapable' requires -enable-experimental-feature NonescapableTypes}}
5+
struct S: ~Escapable {} // expected-error {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
6+
7+
func hello(_ t: some Escapable, _ u: any Escapable) {} // expected-error 2{{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
8+
9+
protocol Whatever: Escapable {} // expected-error {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}

test/Sema/invertible_no_stdlib.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func reqCopy2<T: Builtin.Copyable>(_ t: T) {} // expected-note {{generic paramet
1414

1515
protocol P {}
1616

17-
struct DataType: P, Builtin.Escapable {}
17+
struct DataType: P, Builtin.Escapable {} // expected-error {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
1818
struct DataTypeNC: ~Builtin.Copyable {}
1919

2020
func main() {

test/decl/ext/extensions.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,7 @@ extension B4 {
398398
extension Sendable {} // expected-error {{cannot extend protocol 'Sendable'}}
399399
extension Copyable {} // expected-error {{cannot extend protocol 'Copyable'}}
400400
extension Escapable {} // expected-error {{cannot extend protocol 'Escapable'}}
401+
// expected-error@-1 {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
401402
extension _BitwiseCopyable {} // expected-error {{cannot extend protocol '_BitwiseCopyable'}}
402403

403404
@_marker protocol MyMarkerProto {}

0 commit comments

Comments
 (0)