Skip to content

Commit 75a24ca

Browse files
authored
Merge pull request #32673 from hamishknight/one-time-activation
[CS] Avoid checking RHS of one-way constraint for reactivation
2 parents 28fb66c + 23e7974 commit 75a24ca

File tree

2 files changed

+38
-16
lines changed

2 files changed

+38
-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: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
16+
// FIXME(SR-13132): This should compile.
17+
foo { // expected-error {{type of expression is ambiguous without more context}}
18+
""
19+
}
20+
21+
struct S<T> {
22+
init(_: T.Type) {}
23+
func overloaded() -> [T] { [] }
24+
func overloaded(_ x: T) -> [T] { [x] }
25+
func overloaded(_ x: T...) -> [T] { x }
26+
}
27+
28+
func bar<T>(_ x: T, _ fn: (T, T.Type) -> [T]) {}
29+
bar("") { x, ty in
30+
(Builtin.one_way(S(ty).overloaded(x)))
31+
}

0 commit comments

Comments
 (0)