Skip to content

Commit c03aff5

Browse files
AnthonyLatsishborla
authored andcommitted
Add type-checker performance tracking test for swiftlang#43369
To make this test work, fix an issue in `ConstraintSystem::salvage` where a threshold breach during solving went unnoticed due to exiting on ambiguity before reaching the `isTooComplex` check. Address this by moving the `isTooComplex` check to before we start processing solutions, and stick another one in `findBestSolution` for short-circuiting while we're here.
1 parent 9c1643d commit c03aff5

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

lib/Sema/CSRanking.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,10 @@ SolutionCompareResult ConstraintSystem::compareSolutions(
14051405
Optional<unsigned>
14061406
ConstraintSystem::findBestSolution(SmallVectorImpl<Solution> &viable,
14071407
bool minimize) {
1408+
// Don't spend time filtering solutions if we already hit a threshold.
1409+
if (isTooComplex(viable))
1410+
return None;
1411+
14081412
if (viable.empty())
14091413
return None;
14101414
if (viable.size() == 1)

lib/Sema/ConstraintSystem.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3843,6 +3843,10 @@ SolutionResult ConstraintSystem::salvage() {
38433843
// Solve the system.
38443844
solveImpl(viable);
38453845

3846+
// If we hit a threshold, we're done.
3847+
if (isTooComplex(viable))
3848+
return SolutionResult::forTooComplex(getTooComplexRange());
3849+
38463850
// Before removing any "fixed" solutions, let's check
38473851
// if ambiguity is caused by fixes and diagnose if possible.
38483852
if (diagnoseAmbiguityWithFixes(viable))
@@ -3888,9 +3892,6 @@ SolutionResult ConstraintSystem::salvage() {
38883892
// Fall through to produce diagnostics.
38893893
}
38903894

3891-
if (isTooComplex(viable))
3892-
return SolutionResult::forTooComplex(getTooComplexRange());
3893-
38943895
// Could not produce a specific diagnostic; punt to the client.
38953896
return SolutionResult::forUndiagnosedError();
38963897
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: %target-typecheck-verify-swift -solver-expression-time-threshold=5
2+
// REQUIRES: tools-release,no_asan
3+
4+
// Hits the default memory threshold
5+
// https://github.com/apple/swift/issues/43369
6+
7+
struct A {}
8+
struct B {}
9+
10+
func - (lhs: A, rhs: A) -> B {}
11+
func / (lhs: B, rhs: Double) -> B {}
12+
func - (lhs: B, rhs: B) -> B {}
13+
14+
do {
15+
let a0 = A()
16+
let a1 = A()
17+
18+
let t0 = 0
19+
let t1 = 0
20+
21+
// expected-error@+1 {{the compiler is unable to type-check this expression in reasonable time}}
22+
let _ = (a0 - a0) / (t0 - t0) - (a1 - a1) / (t1 - t1)
23+
}

0 commit comments

Comments
 (0)