Skip to content

Opened existential cleanups #41729

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 4 commits into from
Mar 8, 2022
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
16 changes: 0 additions & 16 deletions include/swift/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3939,12 +3939,6 @@ class ConstraintSystem {
/// due to a change.
ConstraintList &getActiveConstraints() { return ActiveConstraints; }

void findConstraints(SmallVectorImpl<Constraint *> &found,
llvm::function_ref<bool(const Constraint &)> pred) {
filterConstraints(ActiveConstraints, pred, found);
filterConstraints(InactiveConstraints, pred, found);
}

/// Retrieve the representative of the equivalence class containing
/// this type variable.
TypeVariableType *getRepresentative(TypeVariableType *typeVar) const {
Expand Down Expand Up @@ -4117,16 +4111,6 @@ class ConstraintSystem {
/// into the worklist.
void addTypeVariableConstraintsToWorkList(TypeVariableType *typeVar);

static void
filterConstraints(ConstraintList &constraints,
llvm::function_ref<bool(const Constraint &)> pred,
SmallVectorImpl<Constraint *> &found) {
for (auto &constraint : constraints) {
if (pred(constraint))
found.push_back(&constraint);
}
}

public:

/// Coerce the given expression to an rvalue, if it isn't already.
Expand Down
8 changes: 3 additions & 5 deletions lib/Sema/CSGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3733,11 +3733,9 @@ static bool generateInitPatternConstraints(
return cs.generateWrappedPropertyTypeConstraints(
wrappedVar, cs.getType(target.getAsExpr()), patternType);

if (!patternType->is<OpaqueTypeArchetypeType>()) {
// Add a conversion constraint between the types.
cs.addConstraint(ConstraintKind::Conversion, cs.getType(target.getAsExpr()),
patternType, locator, /*isFavored*/true);
}
// Add a conversion constraint between the types.
cs.addConstraint(ConstraintKind::Conversion, cs.getType(target.getAsExpr()),
patternType, locator, /*isFavored*/true);

return false;
}
Expand Down
35 changes: 20 additions & 15 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1348,7 +1348,8 @@ namespace {
static Optional<
std::tuple<TypeVariableType *, Type, OpenedExistentialAdjustments>>
shouldOpenExistentialCallArgument(
ValueDecl *callee, unsigned paramIdx, Type paramTy, Type argTy) {
ValueDecl *callee, unsigned paramIdx, Type paramTy, Type argTy,
Expr *argExpr, ConstraintSystem &cs) {
if (!callee)
return None;

Expand Down Expand Up @@ -1382,6 +1383,20 @@ shouldOpenExistentialCallArgument(
if (!paramTy->hasTypeVariable())
return None;

// An argument expression that explicitly coerces to an existential
// disables the implicit opening of the existential.
if (argExpr) {
if (auto argCoercion = dyn_cast<CoerceExpr>(
argExpr->getSemanticsProvidingExpr())) {
if (auto typeRepr = argCoercion->getCastTypeRepr()) {
if (auto toType = cs.getType(typeRepr)) {
if (toType->isAnyExistentialType())
return None;
}
}
}
}

OpenedExistentialAdjustments adjustments;

// If the argument is inout, strip it off and we can add it back.
Expand Down Expand Up @@ -1670,10 +1685,10 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
auto argTy = argument.getOldType();

bool matchingAutoClosureResult = param.isAutoClosure();
auto *argExpr = getArgumentExpr(locator.getAnchor(), argIdx);
if (param.isAutoClosure() && !isSynthesizedArgument(argument)) {
auto &ctx = cs.getASTContext();
auto *fnType = paramTy->castTo<FunctionType>();
auto *argExpr = getArgumentExpr(locator.getAnchor(), argIdx);

// If this is a call to a function with a closure argument and the
// parameter is an autoclosure, let's just increment the score here
Expand Down Expand Up @@ -1715,7 +1730,7 @@ static ConstraintSystem::TypeMatchResult matchCallArguments(
// If the argument is an existential type and the parameter is generic,
// consider opening the existential type.
if (auto existentialArg = shouldOpenExistentialCallArgument(
callee, paramIdx, paramTy, argTy)) {
callee, paramIdx, paramTy, argTy, argExpr, cs)) {
// My kingdom for a decent "if let" in C++.
TypeVariableType *openedTypeVar;
Type existentialType;
Expand Down Expand Up @@ -10113,18 +10128,8 @@ ConstraintSystem::simplifyOpenedExistentialOfConstraint(
if (type2->isAnyExistentialType()) {
// We have the existential side. Produce an opened archetype and bind
// type1 to it.
bool isMetatype = false;
auto instanceTy = type2;
if (auto metaTy = type2->getAs<ExistentialMetatypeType>()) {
isMetatype = true;
instanceTy = metaTy->getExistentialInstanceType();
}
assert(instanceTy->isExistentialType());
Type openedTy =
OpenedArchetypeType::get(instanceTy->getCanonicalType(),
DC->getGenericSignatureOfContext());
if (isMetatype)
openedTy = MetatypeType::get(openedTy, getASTContext());
Type openedTy = openExistentialType(type2, getConstraintLocator(locator))
.first;
return matchTypes(type1, openedTy, ConstraintKind::Bind, subflags, locator);
}
if (!type2->isTypeVariableOrMember())
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/opened_existentials_suppression.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %target-swift-frontend -enable-experimental-opened-existential-types -typecheck -dump-ast -parse-as-library %s | %FileCheck %s

protocol P { }

func acceptsBox<T>(_ value: T) { }

// CHECK: passBox
// CHECK-NOT: open_existential_expr
func passBox(p: P) {
acceptsBox(p as P)
}