Skip to content

Commit a11a14a

Browse files
committed
[Constraint solver] Add debug dumps + a test for common result types.
1 parent 0ea6536 commit a11a14a

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4826,6 +4826,15 @@ ConstraintSystem::simplifyApplicableFnConstraint(
48264826
if (auto typeVar = desugar2->getAs<TypeVariableType>()) {
48274827
auto choices = getUnboundBindOverloads(typeVar);
48284828
if (Type resultType = findCommonResultType(choices)) {
4829+
ASTContext &ctx = getASTContext();
4830+
if (ctx.LangOpts.DebugConstraintSolver) {
4831+
auto &log = ctx.TypeCheckerDebug->getStream();
4832+
log.indent(solverState ? solverState->depth * 2 + 2 : 0)
4833+
<< "(common result type for $T" << typeVar->getID() << " is "
4834+
<< resultType.getString()
4835+
<< ")\n";
4836+
}
4837+
48294838
addConstraint(ConstraintKind::Bind, func1->getResult(), resultType,
48304839
locator);
48314840
}

test/Constraints/common_type.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: %target-typecheck-verify-swift -debug-constraints 2>%t.err
2+
// RUN: %FileCheck %s < %t.err
3+
4+
struct X {
5+
func g(_: Int) -> Int { return 0 }
6+
func g(_: Double) -> Int { return 0 }
7+
8+
subscript(_: Int) -> String { return "" }
9+
subscript(_: Double) -> String { return "" }
10+
}
11+
12+
struct Y {
13+
func g(_: Int) -> Double { return 0 }
14+
func g(_: Double) -> Double { return 0 }
15+
16+
subscript(_: Int) -> Substring { return "" }
17+
subscript(_: Double) -> Substring { return "" }
18+
}
19+
20+
func f(_: Int) -> X { return X() }
21+
func f(_: Double) -> Y { return Y() }
22+
23+
func testCallCommonType() {
24+
// CHECK: overload set choice binding $T{{[0-9]+}} := (Int) -> X
25+
// CHECK-NEXT: (common result type for $T{{[0-9]+}} is Int)
26+
// CHECK: (overload set choice binding $T{{[0-9]+}} := (Double) -> Y)
27+
// CHECK-NEXT: (common result type for $T{{[0-9]+}} is Double)
28+
_ = f(0).g(0)
29+
}
30+
31+
func testSubscriptCommonType() {
32+
// FIXME: This will work once we have more filtering of subscripts.
33+
// CHECK: subscript_expr
34+
// CHECK: overload set choice binding $T{{[0-9]+}} := (Int) -> X
35+
// CHECK-NOT: (common result type for $T{{[0-9]+}} is String)
36+
// CHECK: (overload set choice binding $T{{[0-9]+}} := (Double) -> Y)
37+
// CHECK-NOT: (common result type for $T{{[0-9]+}} is Substring)
38+
_ = f(0)[0]
39+
}

0 commit comments

Comments
 (0)