Skip to content

Commit

Permalink
[QoI] Cleanup AST after trying to shrink constraint system of invalid…
Browse files Browse the repository at this point in the history
… expression

Since `ConstraintSystem::shrink` is going to attempt to type-check
sub-expressions separately it's essential to clean-up AST if constraint
generation or solving of the such expressions fails, otherwise if
such solving resulted in creation of implicit expression type variables
might leak to the outside.
  • Loading branch information
xedin committed Jan 13, 2017
1 parent 8fff45d commit 9b7d632
Show file tree
Hide file tree
Showing 12 changed files with 24 additions and 23 deletions.
13 changes: 13 additions & 0 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,7 @@ bool ConstraintSystem::Candidate::solve() {
// because it would assign types to expressions, which
// might interfere with solving higher-level expressions.
ExprCleaner cleaner(E);
CleanupIllFormedExpressionRAII cleanup(TC.Context, E);

// Allocate new constraint system for sub-expression.
ConstraintSystem cs(TC, DC, None);
Expand Down Expand Up @@ -1495,6 +1496,18 @@ bool ConstraintSystem::Candidate::solve() {

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

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

return false;
}

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

0 comments on commit 9b7d632

Please sign in to comment.