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