Skip to content

Commit a359f77

Browse files
committed
Add type-checker performance tracking test for #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 3615aa1 commit a359f77

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
@@ -3828,6 +3828,10 @@ SolutionResult ConstraintSystem::salvage() {
38283828
// Solve the system.
38293829
solveImpl(viable);
38303830

3831+
// If we hit a threshold, we're done.
3832+
if (isTooComplex(viable))
3833+
return SolutionResult::forTooComplex(getTooComplexRange());
3834+
38313835
// Before removing any "fixed" solutions, let's check
38323836
// if ambiguity is caused by fixes and diagnose if possible.
38333837
if (diagnoseAmbiguityWithFixes(viable))
@@ -3873,9 +3877,6 @@ SolutionResult ConstraintSystem::salvage() {
38733877
// Fall through to produce diagnostics.
38743878
}
38753879

3876-
if (isTooComplex(viable))
3877-
return SolutionResult::forTooComplex(getTooComplexRange());
3878-
38793880
// Could not produce a specific diagnostic; punt to the client.
38803881
return SolutionResult::forUndiagnosedError();
38813882
}
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)