Skip to content

[QoI] Cleanup AST after trying to shrink constraint system of invalid expression #6801

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
Jan 14, 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
104 changes: 64 additions & 40 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,53 +1449,77 @@ ConstraintSystem::solveSingle(FreeTypeVariableBinding allowFreeTypeVariables) {
}

bool ConstraintSystem::Candidate::solve() {
// Cleanup after constraint system generation/solving,
// because it would assign types to expressions, which
// might interfere with solving higher-level expressions.
ExprCleaner cleaner(E);
// For cleanup to work correctly e.g. `CleanupIllFormedExpressionRAII`
// `E` has to be 'reference to pointer' but, in this situation, it can't
// because we don't want to accidently modify AST while trying to reduce
// type domains of sub-expressions. Because constraint generation _might_
// produce completely different expression let's use a locally scoped
// 'reference to pointer' variable instead of `E` which enables both
// proper constraint generation/solving and cleanup.
auto *&expr = E;
{
// Cleanup after constraint system generation/solving,
// because it would assign types to expressions, which
// might interfere with solving higher-level expressions.
ExprCleaner cleaner(expr);
CleanupIllFormedExpressionRAII cleanup(TC.Context, expr);

// Allocate new constraint system for sub-expression.
ConstraintSystem cs(TC, DC, None);

// Generate constraints for the new system.
if (auto generatedExpr = cs.generateConstraints(expr)) {
expr = generatedExpr;
} else {
// Failure to generate constraint system for sub-expression
// means we can't continue solving sub-expressions.
return true;
}

// Allocate new constraint system for sub-expression.
ConstraintSystem cs(TC, DC, None);
// If there is contextual type present, add an explicit "conversion"
// constraint to the system.
if (!CT.isNull()) {
auto constraintKind = ConstraintKind::Conversion;
if (CTP == CTP_CallArgument)
constraintKind = ConstraintKind::ArgumentConversion;

// Generate constraints for the new system.
if (auto generatedExpr = cs.generateConstraints(E)) {
E = generatedExpr;
} else {
// Failure to generate constraint system for sub-expression means we can't
// continue solving sub-expressions.
return true;
}

// If there is contextual type present, add an explicit "conversion"
// constraint to the system.
if (!CT.isNull()) {
auto constraintKind = ConstraintKind::Conversion;
if (CTP == CTP_CallArgument)
constraintKind = ConstraintKind::ArgumentConversion;
cs.addConstraint(constraintKind, cs.getType(expr), CT,
cs.getConstraintLocator(expr), /*isFavored=*/true);
}

cs.addConstraint(constraintKind, cs.getType(E), CT,
cs.getConstraintLocator(E), /*isFavored=*/true);
}
// Try to solve the system and record all available solutions.
llvm::SmallVector<Solution, 2> solutions;
{
SolverState state(cs);

// Try to solve the system and record all available solutions.
llvm::SmallVector<Solution, 2> solutions;
{
SolverState state(cs);
// Use solveRec() instead of solve() in here, because solve()
// would try to deduce the best solution, which we don't
// really want. Instead, we want the reduced set of domain choices.
cs.solveRec(solutions, FreeTypeVariableBinding::Allow);
}

// Use solveRec() instead of solve() in here, because solve()
// would try to deduce the best solution, which we don't
// really want. Instead, we want the reduced set of domain choices.
cs.solveRec(solutions, FreeTypeVariableBinding::Allow);
}
// No solutions for the sub-expression means that either main expression
// needs salvaging or it's inconsistent (read: doesn't have solutions).
if (solutions.empty())
return true;

// No solutions for the sub-expression means that either main expression
// needs salvaging or it's inconsistent (read: doesn't have solutions).
if (solutions.empty())
return true;
// Record found solutions as suggestions.
this->applySolutions(solutions);

// Solution application is going to modify types of sub-expresssions,
// so we need to avoid clean-up afterwords, but let's double-check
// if we have any implicit expressions with type variables and
// nullify their types.
cleanup.disable();
expr->forEachChildExpr([&](Expr *childExpr) -> Expr * {
Type type = childExpr->getType();
if (childExpr->isImplicit() && type && type->hasTypeVariable())
childExpr->setType(Type());
return childExpr;
});

// Record found solutions as suggestions.
this->applySolutions(solutions);
return false;
return false;
}
}

void ConstraintSystem::Candidate::applySolutions(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
[{_=#keyPath(t>w
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
f#keyPath(n&_==a>c{{{{{{{{{{{{{{{{{_=b:{{{{c{{{{{{d
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
[{{{{{{{{{{{{{{0=#keyPath(n&_=d
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// REQUIRES: asserts
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir

{func a>
print(a==#keyPath(a{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{{
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
func a|Set(#keyPath(t>a>a{
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
print([{#keyPath(a}(t>A
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
protocol a func a|Set(#keyPath(t>a{
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// REQUIRES: asserts
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
{let β=b&[{{{#keyPath(n&[{{{{{{{{{{{{{{{{{{{{{{{{{{a{{{{s
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
Array(_==#keyPath(t>Void!
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
(.n).h.n&[(#keyPath(t>A
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors

// REQUIRES: deterministic-behavior
// RUN: not --crash %target-swift-frontend %s -emit-ir
// RUN: not %target-swift-frontend %s -emit-ir
#keyPath(a
print(Int
print(_=#keyPath(a
Expand Down