Skip to content

prevent uses of Escapable in general #73133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -7708,7 +7708,7 @@ NOTE(add_explicit_protocol_for_conformance,none,
"consider making %kind0 explicitly conform to the '%1' protocol",
(const ValueDecl *, StringRef))
ERROR(escapable_requires_feature_flag,none,
"type '~Escapable' requires -enable-experimental-feature NonescapableTypes",
"type 'Escapable' requires -enable-experimental-feature NonescapableTypes",
())
ERROR(non_bitwise_copyable_type_class,none,
"class cannot conform to 'BitwiseCopyable'", ())
Expand Down
3 changes: 1 addition & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,9 @@ class CheckRepressions {
auto ipk = getInvertibleProtocolKind(*kp);
if (ipk) {
// Gate the '~Escapable' type behind a specific flag for now.
// Uses of 'Escapable' itself are already diagnosed; return ErrorType.
if (*ipk == InvertibleProtocolKind::Escapable &&
!ctx.LangOpts.hasFeature(Feature::NonescapableTypes)) {
diagnoseInvalid(repr, repr.getLoc(),
diag::escapable_requires_feature_flag);
return ErrorType::get(ctx);
}

Expand Down
18 changes: 16 additions & 2 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4770,6 +4770,21 @@ TypeResolver::resolveDeclRefTypeRepr(DeclRefTypeRepr *repr,
auto *dc = getDeclContext();
auto &ctx = getASTContext();

// Gate the 'Escapable' type behind a specific flag for now.
//
// NOTE: we do not return an ErrorType, though! We're just artificially
// preventing people from referring to the type without the feature.
if (auto proto = result->getAs<ProtocolType>()) {
if (auto known = proto->getKnownProtocol()) {
if (*known == KnownProtocolKind::Escapable
&& !isSILSourceFile()
&& !ctx.LangOpts.hasFeature(Feature::NonescapableTypes)) {
diagnoseInvalid(repr, repr->getLoc(),
diag::escapable_requires_feature_flag);
}
}
}

if (ctx.LangOpts.hasFeature(Feature::ImplicitSome) &&
options.isConstraintImplicitExistential()) {
// Check whether this type is an implicit opaque result type.
Expand Down Expand Up @@ -5774,10 +5789,9 @@ NeverNullType TypeResolver::resolveInverseType(InverseTypeRepr *repr,
if (auto kind = getInvertibleProtocolKind(*kp)) {

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

Expand Down
1 change: 1 addition & 0 deletions stdlib/public/core/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func _rethrowsViaClosure(_ fn: () throws -> ()) rethrows {

@_marker public protocol Copyable {}

@_documentation(visibility: internal)
@_marker public protocol Escapable {}

#if $NoncopyableGenerics && $NonescapableTypes
Expand Down
6 changes: 5 additions & 1 deletion test/Parse/inverse_escapable_feature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@



struct S: ~Escapable {} // expected-error {{type '~Escapable' requires -enable-experimental-feature NonescapableTypes}}
struct S: ~Escapable {} // expected-error {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}

func hello(_ t: some Escapable, _ u: any Escapable) {} // expected-error 2{{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}

protocol Whatever: Escapable {} // expected-error {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
2 changes: 1 addition & 1 deletion test/Sema/invertible_no_stdlib.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func reqCopy2<T: Builtin.Copyable>(_ t: T) {} // expected-note {{generic paramet

protocol P {}

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

func main() {
Expand Down
1 change: 1 addition & 0 deletions test/decl/ext/extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ extension B4 {
extension Sendable {} // expected-error {{cannot extend protocol 'Sendable'}}
extension Copyable {} // expected-error {{cannot extend protocol 'Copyable'}}
extension Escapable {} // expected-error {{cannot extend protocol 'Escapable'}}
// expected-error@-1 {{type 'Escapable' requires -enable-experimental-feature NonescapableTypes}}
extension _BitwiseCopyable {} // expected-error {{cannot extend protocol '_BitwiseCopyable'}}

@_marker protocol MyMarkerProto {}
Expand Down