@@ -2741,16 +2741,19 @@ namespace {
2741
2741
auto *locator = CS.getConstraintLocator (closure);
2742
2742
auto closureType = CS.createTypeVariable (locator, TVO_CanBindToNoEscape);
2743
2743
2744
- // Collect any references to closure parameters whose types involve type
2745
- // variables from the closure, because there will be a dependency on
2746
- // those type variables once we have generated constraints for the
2747
- // closure body.
2748
- struct CollectParameterRefs : public ASTWalker {
2744
+ // Collect any variable references whose types involve type variables,
2745
+ // because there will be a dependency on those type variables once we have
2746
+ // generated constraints for the closure body. This includes references
2747
+ // to other closure params such as in `{ x in { x }}` where the inner
2748
+ // closure is dependent on the outer closure's param type, as well as
2749
+ // cases like `for i in x where bar({ i })` where there's a dependency on
2750
+ // the type variable for the pattern `i`.
2751
+ struct CollectVarRefs : public ASTWalker {
2749
2752
ConstraintSystem &cs;
2750
- llvm::SmallVector<TypeVariableType *, 4 > paramRefs ;
2753
+ llvm::SmallVector<TypeVariableType *, 4 > varRefs ;
2751
2754
bool hasErrorExprs = false ;
2752
2755
2753
- CollectParameterRefs (ConstraintSystem &cs) : cs(cs) { }
2756
+ CollectVarRefs (ConstraintSystem &cs) : cs(cs) { }
2754
2757
2755
2758
std::pair<bool , Expr *> walkToExprPre (Expr *expr) override {
2756
2759
// If there are any error expressions in this closure
@@ -2760,21 +2763,21 @@ namespace {
2760
2763
return {false , nullptr };
2761
2764
}
2762
2765
2763
- // Retrieve type variables from references to parameter declarations .
2766
+ // Retrieve type variables from references to var decls .
2764
2767
if (auto *declRef = dyn_cast<DeclRefExpr>(expr)) {
2765
- if (auto *paramDecl = dyn_cast<ParamDecl >(declRef->getDecl ())) {
2766
- if (Type paramType = cs.getTypeIfAvailable (paramDecl )) {
2767
- paramType ->getTypeVariables (paramRefs );
2768
+ if (auto *varDecl = dyn_cast<VarDecl >(declRef->getDecl ())) {
2769
+ if (auto varType = cs.getTypeIfAvailable (varDecl )) {
2770
+ varType ->getTypeVariables (varRefs );
2768
2771
}
2769
2772
}
2770
2773
}
2771
2774
2772
2775
return { true , expr };
2773
2776
}
2774
- } collectParameterRefs (CS);
2775
- closure->walk (collectParameterRefs );
2777
+ } collectVarRefs (CS);
2778
+ closure->walk (collectVarRefs );
2776
2779
2777
- if (collectParameterRefs .hasErrorExprs )
2780
+ if (collectVarRefs .hasErrorExprs )
2778
2781
return Type ();
2779
2782
2780
2783
auto inferredType = inferClosureType (closure);
@@ -2784,7 +2787,7 @@ namespace {
2784
2787
CS.addUnsolvedConstraint (
2785
2788
Constraint::create (CS, ConstraintKind::DefaultClosureType,
2786
2789
closureType, inferredType, locator,
2787
- collectParameterRefs. paramRefs ));
2790
+ collectVarRefs. varRefs ));
2788
2791
2789
2792
CS.setClosureType (closure, inferredType);
2790
2793
return closureType;
0 commit comments