@@ -802,6 +802,27 @@ class DisjunctionStep final : public BindingStep<DisjunctionChoiceProducer> {
802802 }
803803};
804804
805+ // / Retrieves the DeclContext that a conjunction should be solved within.
806+ static DeclContext *getDeclContextForConjunction (ConstraintLocator *loc) {
807+ // Closures introduce a new DeclContext that needs switching into.
808+ auto anchor = loc->getAnchor ();
809+ if (loc->directlyAt <ClosureExpr>())
810+ return castToExpr<ClosureExpr>(anchor);
811+
812+ // SingleValueStmtExprs need to switch to their enclosing context. This
813+ // is unfortunately necessary since they can be present in single-expression
814+ // closures, which don't have their DeclContext established since they're
815+ // solved together with the rest of the system.
816+ if (loc->isForSingleValueStmtConjunction ())
817+ return castToExpr<SingleValueStmtExpr>(anchor)->getDeclContext ();
818+
819+ // Do the same for TapExprs.
820+ if (loc->directlyAt <TapExpr>())
821+ return castToExpr<TapExpr>(anchor)->getVar ()->getDeclContext ();
822+
823+ return nullptr ;
824+ }
825+
805826class ConjunctionStep : public BindingStep <ConjunctionElementProducer> {
806827 // / Snapshot of the constraint system before conjunction.
807828 class SolverSnapshot {
@@ -826,11 +847,9 @@ class ConjunctionStep : public BindingStep<ConjunctionElementProducer> {
826847 : CS(cs), Conjunction(conjunction),
827848 TypeVars (std::move(cs.TypeVariables)) {
828849 auto *locator = Conjunction->getLocator ();
829- // If this conjunction represents a closure, we need to
830- // switch declaration context over to it.
831- if (locator->directlyAt <ClosureExpr>()) {
832- DC.emplace (CS.DC , castToExpr<ClosureExpr>(locator->getAnchor ()));
833- }
850+ // If we need to switch into a new DeclContext for the conjunction, do so.
851+ if (auto *newDC = getDeclContextForConjunction (locator))
852+ DC.emplace (CS.DC , newDC);
834853
835854 auto &CG = CS.getConstraintGraph ();
836855 // Remove all of the current inactive constraints.
0 commit comments