Skip to content

[5.3] [CS] Avoid checking RHS of one-way constraint for reactivation #32693

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
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
23 changes: 7 additions & 16 deletions lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -442,24 +442,15 @@ llvm::TinyPtrVector<Constraint *> ConstraintGraph::gatherConstraints(
llvm::function_ref<bool(Constraint *)> acceptConstraintFn) {
llvm::TinyPtrVector<Constraint *> constraints;
// Whether we should consider this constraint at all.
auto rep = CS.getRepresentative(typeVar);
auto shouldConsiderConstraint = [&](Constraint *constraint) {
// For a one-way constraint, only consider it when the type variable
// is on the right-hand side of the the binding, and the left-hand side of
// the binding is one of the type variables currently under consideration.
// For a one-way constraint, only consider it when the left-hand side of
// the binding is one of the type variables currently under consideration,
// as only such constraints need solving for this component. Note that we
// don't perform any other filtering, as the constraint system should be
// responsible for checking any other conditions.
if (constraint->isOneWayConstraint()) {
auto lhsTypeVar =
constraint->getFirstType()->castTo<TypeVariableType>();
if (!CS.isActiveTypeVariable(lhsTypeVar))
return false;

SmallVector<TypeVariableType *, 2> rhsTypeVars;
constraint->getSecondType()->getTypeVariables(rhsTypeVars);
for (auto rhsTypeVar : rhsTypeVars) {
if (CS.getRepresentative(rhsTypeVar) == rep)
return true;
}
return false;
auto lhsTypeVar = constraint->getFirstType()->castTo<TypeVariableType>();
return CS.isActiveTypeVariable(lhsTypeVar);
}

return true;
Expand Down
29 changes: 29 additions & 0 deletions test/Constraints/rdar64890308.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-typecheck-verify-swift -parse-stdlib

// rdar://64890308: Make sure we don't leave one-way constraints unsolved.

import Swift

@_functionBuilder
class ArrayBuilder<Element> {
static func buildBlock() -> [Element] { [] }
static func buildBlock(_ elt: Element) -> [Element] { [elt] }
static func buildBlock(_ elts: Element...) -> [Element] { elts }
}

func foo<T>(@ArrayBuilder<T> fn: () -> [T]) {}
foo {
""
}

struct S<T> {
init(_: T.Type) {}
func overloaded() -> [T] { [] }
func overloaded(_ x: T) -> [T] { [x] }
func overloaded(_ x: T...) -> [T] { x }
}

func bar<T>(_ x: T, _ fn: (T, T.Type) -> [T]) {}
bar("") { x, ty in
(Builtin.one_way(S(ty).overloaded(x)))
}