Skip to content

[Constraint solver] Fix an issue with rewriting OpenExistentialExpr. #10058

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
Jun 2, 2017
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
10 changes: 10 additions & 0 deletions lib/AST/ASTVerifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ class Verifier : public ASTWalker {
if (!shouldVerify(cast<Expr>(expr)))
return false;

// In rare instances we clear the opaque value because we no
// longer have a subexpression that references it.
if (!expr->getOpaqueValue())
return true;

assert(!OpaqueValues.count(expr->getOpaqueValue()));
OpaqueValues[expr->getOpaqueValue()] = 0;
assert(OpenedExistentialArchetypes.count(expr->getOpenedArchetype())==0);
Expand All @@ -705,6 +710,11 @@ class Verifier : public ASTWalker {
}

void cleanup(OpenExistentialExpr *expr) {
// In rare instances we clear the opaque value because we no
// longer have a subexpression that references it.
if (!expr->getOpaqueValue())
return;

assert(OpaqueValues.count(expr->getOpaqueValue()));
OpaqueValues.erase(expr->getOpaqueValue());
assert(OpenedExistentialArchetypes.count(expr->getOpenedArchetype())==1);
Expand Down
26 changes: 16 additions & 10 deletions lib/AST/Expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,19 +316,25 @@ void Expr::propagateLValueAccessKind(AccessKind accessKind,

void visitOpenExistentialExpr(OpenExistentialExpr *E,
AccessKind accessKind) {
bool opaqueValueHadAK = E->getOpaqueValue()->hasLValueAccessKind();
AccessKind oldOpaqueValueAK =
(opaqueValueHadAK ? E->getOpaqueValue()->getLValueAccessKind()
: AccessKind::Read);
AccessKind oldOpaqueValueAK;
bool opaqueValueHadAK;
if (E->getOpaqueValue()) {
opaqueValueHadAK = E->getOpaqueValue()->hasLValueAccessKind();
oldOpaqueValueAK =
(opaqueValueHadAK ? E->getOpaqueValue()->getLValueAccessKind()
: AccessKind::Read);
}

visit(E->getSubExpr(), accessKind);

// Propagate the new access kind from the OVE to the original existential
// if we just set or changed it on the OVE.
if (E->getOpaqueValue()->hasLValueAccessKind()) {
auto newOpaqueValueAK = E->getOpaqueValue()->getLValueAccessKind();
if (!opaqueValueHadAK || newOpaqueValueAK != oldOpaqueValueAK)
visit(E->getExistentialValue(), newOpaqueValueAK);
if (E->getOpaqueValue()) {
// Propagate the new access kind from the OVE to the original
// existential if we just set or changed it on the OVE.
if (E->getOpaqueValue()->hasLValueAccessKind()) {
auto newOpaqueValueAK = E->getOpaqueValue()->getLValueAccessKind();
if (!opaqueValueHadAK || newOpaqueValueAK != oldOpaqueValueAK)
visit(E->getExistentialValue(), newOpaqueValueAK);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,12 @@ namespace {
if (result == nullptr) {
result = new (tc.Context) ErrorExpr(range);
cs.setType(result, erasedTy);
// The opaque value is no longer reachable in an AST walk as
// a result of the result above being replaced with an
// ErrorExpr, but there is code expecting to have a type set
// on it. Since we no longer have a reachable reference,
// we'll null this out.
record.OpaqueValue = nullptr;
}
}

Expand All @@ -768,7 +774,7 @@ namespace {
// means this is our only chance to propagate the l-value access kind
// down to the original existential value. Otherwise, propagateLVAK
// will handle this.
if (record.OpaqueValue->hasLValueAccessKind())
if (record.OpaqueValue && record.OpaqueValue->hasLValueAccessKind())
cs.propagateLValueAccessKind(record.ExistentialValue,
record.OpaqueValue->getLValueAccessKind());

Expand Down