Skip to content

Commit abc2157

Browse files
authored
Merge pull request #59856 from amritpan/improve-disjunction-printing
[ConstraintSystem] Improve initial constraints printing in the type inference algorithm debug output
2 parents b62819e + e23576f commit abc2157

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

include/swift/Sema/Constraint.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,10 @@ class Constraint final : public llvm::ilist_node<Constraint>,
852852
/// Clone the given constraint.
853853
Constraint *clone(ConstraintSystem &cs) const;
854854

855-
void print(llvm::raw_ostream &Out, SourceManager *sm) const;
855+
/// Print constraint placed on type and constraint properties.
856+
///
857+
/// \c skipLocator skips printing of locators.
858+
void print(llvm::raw_ostream &Out, SourceManager *sm, bool skipLocator = false) const;
856859

857860
SWIFT_DEBUG_DUMPER(dump(SourceManager *SM));
858861

lib/Sema/Constraint.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
322322
llvm_unreachable("Unhandled ConstraintKind in switch.");
323323
}
324324

325-
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
325+
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm, bool skipLocator) const {
326326
// Print all type variables as $T0 instead of _ here.
327327
PrintOptions PO;
328328
PO.PrintTypesForDebugging = true;
@@ -343,16 +343,30 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
343343
Out << "]]";
344344
}
345345
Out << ":\n";
346-
347-
interleave(getNestedConstraints(),
346+
347+
// Sort constraints by favored, unmarked, disabled
348+
// for printing only.
349+
std::vector<Constraint *> sortedConstraints(getNestedConstraints().begin(),
350+
getNestedConstraints().end());
351+
llvm::sort(sortedConstraints,
352+
[](const Constraint *lhs, const Constraint *rhs) {
353+
if (lhs->isFavored() != rhs->isFavored())
354+
return lhs->isFavored();
355+
if (lhs->isDisabled() != rhs->isDisabled())
356+
return rhs->isDisabled();
357+
return false;
358+
});
359+
360+
interleave(sortedConstraints,
348361
[&](Constraint *constraint) {
349362
if (constraint->isDisabled())
350363
Out << "> [disabled] ";
351364
else if (constraint->isFavored())
352365
Out << "> [favored] ";
353366
else
354367
Out << "> ";
355-
constraint->print(Out, sm);
368+
constraint->print(Out, sm,
369+
/*skipLocator=*/constraint->getLocator() == Locator);
356370
},
357371
[&] { Out << "\n"; });
358372
return;
@@ -522,7 +536,7 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
522536
fix->print(Out);
523537
}
524538

525-
if (Locator) {
539+
if (Locator && !skipLocator) {
526540
Out << " [[";
527541
Locator->dump(sm, Out);
528542
Out << "]];";

test/Constraints/overload_filtering_objc.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ func testOptional(obj: P) {
2525

2626

2727
func test_double_cgfloat_conversion_filtering(d: Double, cgf: CGFloat) {
28-
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat {{.*}} -> implicit conversion [Double-to-CGFloat] -> apply function -> constructor member
28+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat
2929
let _: CGFloat = d
3030

31-
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double {{.*}} -> implicit conversion [CGFloat-to-Double] -> apply function -> constructor member
31+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double
3232
let _: Double = cgf
3333

3434
func test_optional_cgf(_: CGFloat??) {
@@ -37,9 +37,9 @@ func test_double_cgfloat_conversion_filtering(d: Double, cgf: CGFloat) {
3737
func test_optional_double(_: Double??) {
3838
}
3939

40-
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat {{.*}} -> implicit conversion [Double-to-CGFloat] -> apply function -> constructor member
40+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).CGFloat.init(_:)@{{.*}} : (CGFloat.Type) -> (Double) -> CGFloat
4141
test_optional_cgf(d)
4242

43-
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double {{.*}} -> implicit conversion [CGFloat-to-Double] -> apply function -> constructor member
43+
// CHECK: [favored] $T{{.*}} bound to decl CoreGraphics.(file).Double extension.init(_:)@{{.*}} : (Double.Type) -> (CGFloat) -> Double
4444
test_optional_double(cgf)
4545
}

0 commit comments

Comments
 (0)