Skip to content

Commit a6e5e19

Browse files
committed
Sema: Minor adjustment to BindingSet dumping
Shorten some of the keys and format them consistently as sentence fragments rather than identifiers.
1 parent 55189ba commit a6e5e19

File tree

5 files changed

+92
-10
lines changed

5 files changed

+92
-10
lines changed

docs/DebuggingTheCompiler.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,13 @@ constraints and present the final type checked solution, e.g.:
173173
174174
Score: <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>
175175
Type Variables:
176-
($T0 [attributes: [literal: integer]] [with possible bindings: (default type of literal) Int]) @ locator@0x13e009800 [[email protected]:3:1]
176+
($T0 [attributes: [literal: integer]] [potential bindings: (default type of literal) Int]) @ locator@0x13e009800 [[email protected]:3:1]
177177
178178
Inactive Constraints:
179179
$T0 literal conforms to ExpressibleByIntegerLiteral @ locator@0x13e009800 [[email protected]:3:1]
180180
181181
(Potential Binding(s):
182-
($T0 [attributes: [literal: integer]] [with possible bindings: (default type of literal) Int])
182+
($T0 [attributes: [literal: integer]] [potential bindings: (default type of literal) Int])
183183
(attempting type variable $T0 := Int
184184
(considering: $T0 literal conforms to ExpressibleByIntegerLiteral @ locator@0x13e009800 [[email protected]:3:1]
185185
(simplification result:

lib/Sema/CSBindings.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2196,16 +2196,27 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
21962196
out << "] ";
21972197

21982198
if (involvesTypeVariables()) {
2199-
out << "[involves_type_vars: ";
2200-
interleave(AdjacentVars,
2201-
[&](const auto *typeVar) { out << typeVar->getString(PO); },
2202-
[&out]() { out << ", "; });
2199+
out << "[adjacent to: ";
2200+
if (AdjacentVars.empty()) {
2201+
out << "<none>";
2202+
} else {
2203+
SmallVector<TypeVariableType *> adjacentVars(AdjacentVars.begin(),
2204+
AdjacentVars.end());
2205+
llvm::sort(adjacentVars,
2206+
[](const TypeVariableType *lhs, const TypeVariableType *rhs) {
2207+
return lhs->getID() < rhs->getID();
2208+
});
2209+
interleave(
2210+
adjacentVars,
2211+
[&](const auto *typeVar) { out << typeVar->getString(PO); },
2212+
[&out]() { out << ", "; });
2213+
}
22032214
out << "] ";
22042215
}
22052216

22062217
auto numDefaultable = getNumViableDefaultableBindings();
22072218
if (numDefaultable > 0)
2208-
out << "[#defaultable_bindings: " << numDefaultable << "] ";
2219+
out << "[defaultable bindings: " << numDefaultable << "] ";
22092220

22102221
struct PrintableBinding {
22112222
private:
@@ -2251,7 +2262,7 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22512262
}
22522263
};
22532264

2254-
out << "[with possible bindings: ";
2265+
out << "[potential bindings: ";
22552266
SmallVector<PrintableBinding, 2> potentialBindings;
22562267
for (const auto &binding : Bindings) {
22572268
switch (binding.Kind) {
@@ -2275,7 +2286,7 @@ void BindingSet::dump(llvm::raw_ostream &out, unsigned indent) const {
22752286
}
22762287
}
22772288
if (potentialBindings.empty()) {
2278-
out << "<empty>";
2289+
out << "<none>";
22792290
} else {
22802291
interleave(
22812292
potentialBindings,

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void TypeVariableType::Implementation::print(llvm::raw_ostream &OS) {
7575
if (isPackExpansion())
7676
bindingOptions.push_back(TypeVariableOptions::TVO_PackExpansion);
7777
if (!bindingOptions.empty()) {
78-
OS << " [allows bindings to: ";
78+
OS << " [can bind to: ";
7979
interleave(bindingOptions, OS,
8080
[&](TypeVariableOptions option) {
8181
(OS << getTypeVariableOptions(option));},

unittests/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ add_swift_unittest(swiftSemaTests
44
BindingInferenceTests.cpp
55
ConstraintGenerationTests.cpp
66
ConstraintSimplificationTests.cpp
7+
ConstraintSystemDumpTests.cpp
78
UnresolvedMemberLookupTests.cpp
89
PlaceholderTypeInferenceTests.cpp
910
SolutionFilteringTests.cpp
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===--- ConstraintSystemDumpTests.cpp ------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "SemaFixture.h"
14+
#include "swift/Sema/ConstraintSystem.h"
15+
16+
using namespace swift;
17+
using namespace swift::unittest;
18+
19+
TEST_F(SemaTest, DumpConstraintSystemBasic) {
20+
ConstraintSystemOptions options;
21+
ConstraintSystem cs(DC, options);
22+
23+
auto *emptyLoc = cs.getConstraintLocator({});
24+
25+
auto *t0 = cs.createTypeVariable(emptyLoc, TVO_CanBindToLValue);
26+
auto *t1 = cs.createTypeVariable(emptyLoc, 0);
27+
auto *t2 = cs.createTypeVariable(
28+
cs.getConstraintLocator(emptyLoc, ConstraintLocator::GenericArgument),
29+
TVO_CanBindToHole | TVO_CanBindToPack);
30+
31+
cs.addUnsolvedConstraint(Constraint::create(
32+
cs, ConstraintKind::Bind, t2,
33+
TupleType::get({Type(t0), Type(t1)}, Context), emptyLoc));
34+
35+
std::string expectedOutput =
36+
R"(Score: <default 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0>
37+
Type Variables:
38+
$T0 [can bind to: lvalue] [adjacent to: $T1, $T2] [potential bindings: <none>] @ locator@ []
39+
$T1 [adjacent to: $T0, $T2] [potential bindings: <none>] @ locator@ []
40+
$T2 [can bind to: hole, pack] [potential bindings: <none>] @ locator@ [ → generic argument #0]
41+
Inactive Constraints:
42+
$T2 bind ($T0, $T1) @ locator@ []
43+
)";
44+
45+
std::string actualOutput;
46+
{
47+
llvm::raw_string_ostream os(actualOutput);
48+
cs.print(os);
49+
50+
// Remove locator addresses.
51+
std::string adjustedOutput;
52+
const auto size = actualOutput.size();
53+
size_t pos = 0;
54+
while (pos < size) {
55+
auto addr_pos = actualOutput.find("0x", pos);
56+
if (addr_pos == std::string::npos) {
57+
adjustedOutput += actualOutput.substr(pos, std::string::npos);
58+
break;
59+
} else {
60+
adjustedOutput += actualOutput.substr(pos, addr_pos - pos);
61+
}
62+
63+
pos = actualOutput.find(' ', addr_pos);
64+
}
65+
66+
actualOutput = adjustedOutput;
67+
}
68+
69+
EXPECT_EQ(expectedOutput, actualOutput);
70+
}

0 commit comments

Comments
 (0)