Skip to content

Commit 31aafc7

Browse files
authored
Merge pull request #41131 from hborla/5.6-disable-se-0309
[5.6][SE-0309] Disable SE-0309 in Swift 5.6.
2 parents e744f41 + 0d2d249 commit 31aafc7

18 files changed

+71
-42
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4647,6 +4647,9 @@ ERROR(unchecked_not_inheritance_clause,none,
46474647
ERROR(unchecked_not_existential,none,
46484648
"'unchecked' attribute cannot apply to non-protocol type %0", (Type))
46494649

4650+
ERROR(unsupported_existential_type,none,
4651+
"protocol %0 can only be used as a generic constraint because it has "
4652+
"Self or associated type requirements", (Identifier))
46504653
ERROR(any_not_existential,none,
46514654
"'any' has no effect on %select{concrete type|type parameter}0 %1",
46524655
(bool, Type))

include/swift/Basic/LangOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ namespace swift {
317317
/// keyword.
318318
bool EnableExplicitExistentialTypes = true;
319319

320+
/// Enable experimental support for universal existential types
321+
/// as proposed in SE-0309.
322+
bool EnableExperimentalUniversalExistentials = false;
323+
320324
/// Enable experimental flow-sensitive concurrent captures.
321325
bool EnableExperimentalFlowSensitiveConcurrentCaptures = false;
322326

include/swift/Option/FrontendOptions.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,10 @@ def enable_explicit_existential_types :
486486
Flag<["-"], "enable-explicit-existential-types">,
487487
HelpText<"Enable experimental support for explicit existential types">;
488488

489+
def enable_experimental_universal_existentials :
490+
Flag<["-"], "enable-experimental-universal-existentials">,
491+
HelpText<"Enable experimental support for universal existential types">;
492+
489493
def enable_deserialization_recovery :
490494
Flag<["-"], "enable-deserialization-recovery">,
491495
HelpText<"Attempt to recover from missing xrefs (etc) in swiftmodules">;

lib/Frontend/CompilerInvocation.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
441441
Opts.EnableExplicitExistentialTypes |=
442442
Args.hasArg(OPT_enable_explicit_existential_types);
443443

444+
Opts.EnableExperimentalUniversalExistentials |=
445+
Args.hasArg(OPT_enable_experimental_universal_existentials);
446+
444447
Opts.EnableExperimentalDistributed |=
445448
Args.hasArg(OPT_enable_experimental_distributed);
446449

lib/Sema/TypeCheckType.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,7 +3966,8 @@ class ExistentialTypeVisitor
39663966
return false;
39673967

39683968
// Arbitrary protocol constraints are okay for 'any' types.
3969-
if (isa<ExistentialTypeRepr>(T))
3969+
if (Ctx.LangOpts.EnableExperimentalUniversalExistentials &&
3970+
isa<ExistentialTypeRepr>(T))
39703971
return false;
39713972

39723973
visit(T);
@@ -3991,16 +3992,23 @@ class ExistentialTypeVisitor
39913992
}
39923993

39933994
void visitIdentTypeRepr(IdentTypeRepr *T) {
3994-
if (T->isInvalid() || !Ctx.LangOpts.EnableExplicitExistentialTypes)
3995+
if (T->isInvalid())
39953996
return;
39963997

39973998
auto comp = T->getComponentRange().back();
39983999
if (auto *proto = dyn_cast_or_null<ProtocolDecl>(comp->getBoundDecl())) {
39994000
if (proto->existentialRequiresAny()) {
4000-
Ctx.Diags.diagnose(comp->getNameLoc(),
4001-
diag::existential_requires_any,
4002-
proto->getName())
4003-
.limitBehavior(DiagnosticBehavior::Warning);
4001+
if (!Ctx.LangOpts.EnableExperimentalUniversalExistentials) {
4002+
Ctx.Diags.diagnose(comp->getNameLoc(),
4003+
diag::unsupported_existential_type,
4004+
proto->getName());
4005+
T->setInvalid();
4006+
} else if (Ctx.LangOpts.EnableExplicitExistentialTypes) {
4007+
Ctx.Diags.diagnose(comp->getNameLoc(),
4008+
diag::existential_requires_any,
4009+
proto->getName())
4010+
.limitBehavior(DiagnosticBehavior::Warning);
4011+
}
40044012
}
40054013
} else if (auto *alias = dyn_cast_or_null<TypeAliasDecl>(comp->getBoundDecl())) {
40064014
auto type = Type(alias->getDeclaredInterfaceType()->getDesugaredType());
@@ -4014,10 +4022,17 @@ class ExistentialTypeVisitor
40144022
if (!protoDecl->existentialRequiresAny())
40154023
continue;
40164024

4017-
Ctx.Diags.diagnose(comp->getNameLoc(),
4018-
diag::existential_requires_any,
4019-
protoDecl->getName())
4020-
.limitBehavior(DiagnosticBehavior::Warning);
4025+
if (!Ctx.LangOpts.EnableExperimentalUniversalExistentials) {
4026+
Ctx.Diags.diagnose(comp->getNameLoc(),
4027+
diag::unsupported_existential_type,
4028+
protoDecl->getName());
4029+
T->setInvalid();
4030+
} else if (Ctx.LangOpts.EnableExplicitExistentialTypes) {
4031+
Ctx.Diags.diagnose(comp->getNameLoc(),
4032+
diag::existential_requires_any,
4033+
protoDecl->getName())
4034+
.limitBehavior(DiagnosticBehavior::Warning);
4035+
}
40214036
}
40224037
}
40234038
return false;

test/Generics/function_defs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-universal-existentials
22

33
//===----------------------------------------------------------------------===//
44
// Type-check function definitions

test/decl/nested/protocol.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -parse-as-library
1+
// RUN: %target-typecheck-verify-swift -parse-as-library -enable-experimental-universal-existentials
22

33
// Protocols cannot be nested inside other types, and types cannot
44
// be nested inside protocols

test/decl/protocol/conforms/inherited.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-universal-existentials
22

33
// Inheritable: method with 'Self' in contravariant position.
44
protocol P1 {

test/decl/protocol/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -enable-objc-interop
1+
// RUN: %target-typecheck-verify-swift -enable-objc-interop -enable-experimental-universal-existentials
22
protocol EmptyProtocol { }
33

44
protocol DefinitionsInProtocols {

test/decl/protocol/protocols_with_self_or_assoc_reqs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-typecheck-verify-swift -disable-availability-checking
1+
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-universal-existentials
22

33
//===----------------------------------------------------------------------===//
44
// Use of protocols with Self or associated type requirements

0 commit comments

Comments
 (0)