Skip to content

Commit 0c4303f

Browse files
committed
[CS] Avoid checking RHS of one-way constraint for reactivation
Previously we would only gather one-way constraints if they were found through a type variable in their right hand side. However we would incorrectly check this against the type variable that we started the search at. This meant that if the constraint was found through a fixed binding, we would never return it, and could therefore fail to re-activate it, leaving it unsolved. Fix this issue by simply removing the check for the RHS, and letting the constraint system handle it instead. Resolves rdar://64890308.
1 parent eed09ea commit 0c4303f

File tree

2 files changed

+36
-16
lines changed

2 files changed

+36
-16
lines changed

lib/Sema/ConstraintGraph.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -442,24 +442,15 @@ llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
442442
llvm::function_ref<bool(Constraint *)> acceptConstraintFn) {
443443
llvm::TinyPtrVector<Constraint *> constraints;
444444
// Whether we should consider this constraint at all.
445-
auto rep = CS.getRepresentative(typeVar);
446445
auto shouldConsiderConstraint = [&](Constraint *constraint) {
447-
// For a one-way constraint, only consider it when the type variable
448-
// is on the right-hand side of the the binding, and the left-hand side of
449-
// the binding is one of the type variables currently under consideration.
446+
// For a one-way constraint, only consider it when the left-hand side of
447+
// the binding is one of the type variables currently under consideration,
448+
// as only such constraints need solving for this component. Note that we
449+
// don't perform any other filtering, as the constraint system should be
450+
// responsible for checking any other conditions.
450451
if (constraint->isOneWayConstraint()) {
451-
auto lhsTypeVar =
452-
constraint->getFirstType()->castTo<TypeVariableType>();
453-
if (!CS.isActiveTypeVariable(lhsTypeVar))
454-
return false;
455-
456-
SmallVector<TypeVariableType *, 2> rhsTypeVars;
457-
constraint->getSecondType()->getTypeVariables(rhsTypeVars);
458-
for (auto rhsTypeVar : rhsTypeVars) {
459-
if (CS.getRepresentative(rhsTypeVar) == rep)
460-
return true;
461-
}
462-
return false;
452+
auto lhsTypeVar = constraint->getFirstType()->castTo<TypeVariableType>();
453+
return CS.isActiveTypeVariable(lhsTypeVar);
463454
}
464455

465456
return true;

test/Constraints/rdar64890308.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %target-typecheck-verify-swift -parse-stdlib
2+
3+
// rdar://64890308: Make sure we don't leave one-way constraints unsolved.
4+
5+
import Swift
6+
7+
@_functionBuilder
8+
class ArrayBuilder<Element> {
9+
static func buildBlock() -> [Element] { [] }
10+
static func buildBlock(_ elt: Element) -> [Element] { [elt] }
11+
static func buildBlock(_ elts: Element...) -> [Element] { elts }
12+
}
13+
14+
func foo<T>(@ArrayBuilder<T> fn: () -> [T]) {}
15+
foo {
16+
""
17+
}
18+
19+
struct S<T> {
20+
init(_: T.Type) {}
21+
func overloaded() -> [T] { [] }
22+
func overloaded(_ x: T) -> [T] { [x] }
23+
func overloaded(_ x: T...) -> [T] { x }
24+
}
25+
26+
func bar<T>(_ x: T, _ fn: (T, T.Type) -> [T]) {}
27+
bar("") { x, ty in
28+
(Builtin.one_way(S(ty).overloaded(x)))
29+
}

0 commit comments

Comments
 (0)