Skip to content

[CSSolver] Solve multi-statement closures in source order #62527

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 1 commit into from
Dec 13, 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
28 changes: 26 additions & 2 deletions lib/Sema/CSSolver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2350,15 +2350,39 @@ Constraint *ConstraintSystem::selectDisjunction() {
}

Constraint *ConstraintSystem::selectConjunction() {
SmallVector<Constraint *, 4> conjunctions;
for (auto &constraint : InactiveConstraints) {
if (constraint.isDisabled())
continue;

if (constraint.getKind() == ConstraintKind::Conjunction)
return &constraint;
conjunctions.push_back(&constraint);
}

return nullptr;
if (conjunctions.empty())
return nullptr;

auto &SM = getASTContext().SourceMgr;

// All of the multi-statement closures should be solved in order of their
// apperance in the source.
llvm::sort(
conjunctions, [&](Constraint *conjunctionA, Constraint *conjunctionB) {
auto *locA = conjunctionA->getLocator();
auto *locB = conjunctionB->getLocator();

if (!(locA && locB))
return false;

auto *closureA = getAsExpr<ClosureExpr>(locA->getAnchor());
auto *closureB = getAsExpr<ClosureExpr>(locB->getAnchor());

return closureA && closureB
? SM.isBeforeInBuffer(closureA->getLoc(), closureB->getLoc())
: false;
});

return conjunctions.front();
}

bool DisjunctionChoice::attempt(ConstraintSystem &cs) const {
Expand Down
26 changes: 25 additions & 1 deletion test/expr/closure/multi_statement.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

// RUN: %target-typecheck-verify-swift -swift-version 5 -enable-experimental-static-assert

func isInt<T>(_ value: T) -> Bool {
Expand Down Expand Up @@ -626,3 +625,28 @@ do {
}
}
}

// Test to make sure that type-checker doesn't attempt the closure passed to
// `.filter` before the one from `.init`, otherwise it would mean that generic
// parameter of `.filter` wouldn't be inferred since it depends on result of
// `.init` closure.
func test_that_closures_are_attempted_in_order() {
struct Test<T> {
init(_: ([Int]) -> T) {}
init(_: String) {}
init(_: Int, _: String = "") {}

func filter(_: (T) -> Bool) {}
}

Test {
_ = 42
return $0.map { Optional(Float($0)) }
}
.filter {
if $0.isEmpty { // Ok
return true
}
return false
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func foo(arr: [E], other: P) -> Bool {
return arr.compactMap { i in
var flag = false
return try? i.getB(&flag)
}.compactMap { u -> P? in // expected-error {{unable to infer type of a closure parameter 'u' in the current context}}
}.compactMap { u -> P? in // Ok
guard let a = try? u.foo() else { return nil }
return a.value!
}.contains {
Expand Down