Skip to content

Don't open an existential argument that's completely self-conforming. #42011

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
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/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ def enable_experimental_opened_existential_types :
HelpText<"Enable experimental support for implicitly opened existentials">;

def disable_experimental_opened_existential_types :
Flag<["-"], "disble-experimental-opened-existential-types">,
Flag<["-"], "disable-experimental-opened-existential-types">,
HelpText<"Disable experimental support for implicitly opened existentials">;

def enable_deserialization_recovery :
Expand Down
5 changes: 0 additions & 5 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,6 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Opts.EnableExperimentalVariadicGenerics |=
Args.hasArg(OPT_enable_experimental_variadic_generics);

// SwiftOnoneSupport produces different symbols when opening existentials,
// so disable it.
if (FrontendOpts.ModuleName == SWIFT_ONONE_SUPPORT)
Opts.EnableOpenedExistentialTypes = false;

Opts.EnableExperimentalDistributed |=
Args.hasArg(OPT_enable_experimental_distributed);

Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,6 +1501,25 @@ shouldOpenExistentialCallArgument(
if (!argTy->isAnyExistentialType())
return None;

if (argTy->isExistentialType()) {
// If the existential argument type conforms to all of its protocol
// requirements, don't open the existential.
auto layout = argTy->getExistentialLayout();
auto module = cs.DC->getParentModule();
bool containsNonSelfConformance = false;
for (auto proto : layout.getProtocols()) {
auto protoDecl = proto->getDecl();
auto conformance = module->lookupExistentialConformance(argTy, protoDecl);
if (conformance.isInvalid()) {
containsNonSelfConformance = true;
break;
}
}

if (!containsNonSelfConformance)
return None;
}

auto param = getParameterAt(callee, paramIdx);
if (!param)
return None;
Expand Down
4 changes: 3 additions & 1 deletion test/Constraints/opened_existentials_suppression.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ func acceptsBox<T>(_ value: T) { }

// CHECK: passBox
// CHECK-NOT: open_existential_expr
func passBox(p: P) {
func passBox(p: P, obj: AnyObject, err: Error) {
acceptsBox(p as P)
acceptsBox(obj)
acceptsBox(err)
}