Skip to content

[ConstraintSystem] Improve initial constraints printing in the type inference algorithm debug output #59856

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion include/swift/Sema/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,10 @@ class Constraint final : public llvm::ilist_node<Constraint>,
/// Clone the given constraint.
Constraint *clone(ConstraintSystem &cs) const;

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

SWIFT_DEBUG_DUMPER(dump(SourceManager *SM));

Expand Down
24 changes: 19 additions & 5 deletions lib/Sema/Constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ Constraint *Constraint::clone(ConstraintSystem &cs) const {
llvm_unreachable("Unhandled ConstraintKind in switch.");
}

void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm, bool skipLocator) const {
// Print all type variables as $T0 instead of _ here.
PrintOptions PO;
PO.PrintTypesForDebugging = true;
Expand All @@ -343,16 +343,30 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
Out << "]]";
}
Out << ":\n";

interleave(getNestedConstraints(),

// Sort constraints by favored, unmarked, disabled
// for printing only.
std::vector<Constraint *> sortedConstraints(getNestedConstraints().begin(),
getNestedConstraints().end());
llvm::sort(sortedConstraints,
[](const Constraint *lhs, const Constraint *rhs) {
if (lhs->isFavored() != rhs->isFavored())
return lhs->isFavored();
if (lhs->isDisabled() != rhs->isDisabled())
return rhs->isDisabled();
return false;
});

interleave(sortedConstraints,
[&](Constraint *constraint) {
if (constraint->isDisabled())
Out << "> [disabled] ";
else if (constraint->isFavored())
Out << "> [favored] ";
else
Out << "> ";
constraint->print(Out, sm);
constraint->print(Out, sm,
/*skipLocator=*/constraint->getLocator() == Locator);
},
[&] { Out << "\n"; });
return;
Expand Down Expand Up @@ -522,7 +536,7 @@ void Constraint::print(llvm::raw_ostream &Out, SourceManager *sm) const {
fix->print(Out);
}

if (Locator) {
if (Locator && !skipLocator) {
Out << " [[";
Locator->dump(sm, Out);
Out << "]];";
Expand Down
8 changes: 4 additions & 4 deletions test/Constraints/overload_filtering_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ func testOptional(obj: P) {


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

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

func test_optional_cgf(_: CGFloat??) {
Expand All @@ -37,9 +37,9 @@ func test_double_cgfloat_conversion_filtering(d: Double, cgf: CGFloat) {
func test_optional_double(_: Double??) {
}

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

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