@@ -846,63 +846,42 @@ namespace {
846
846
};
847
847
} // end anonymous namespace
848
848
849
- namespace {
850
-
851
- // Collect any variable references whose types involve type variables,
852
- // because there will be a dependency on those type variables once we have
853
- // generated constraints for the closure/tap body. This includes references
854
- // to other closure params such as in `{ x in { x }}` where the inner
855
- // closure is dependent on the outer closure's param type, as well as
856
- // cases like `for i in x where bar({ i })` where there's a dependency on
857
- // the type variable for the pattern `i`.
858
- struct VarRefCollector : public ASTWalker {
859
- ConstraintSystem &cs;
860
- llvm::SmallPtrSet<TypeVariableType *, 4 > varRefs;
861
-
862
- VarRefCollector (ConstraintSystem &cs) : cs(cs) {}
863
-
864
- bool shouldWalkCaptureInitializerExpressions () override { return true ; }
849
+ void VarRefCollector::inferTypeVars (Decl *D) {
850
+ // We're only interested in VarDecls.
851
+ if (!isa_and_nonnull<VarDecl>(D))
852
+ return ;
865
853
866
- MacroWalking getMacroWalkingBehavior () const override {
867
- return MacroWalking::Arguments;
868
- }
854
+ auto ty = CS. getTypeIfAvailable (D);
855
+ if (!ty)
856
+ return ;
869
857
870
- PreWalkResult<Expr *> walkToExprPre (Expr *expr) override {
871
- // Retrieve type variables from references to var decls.
872
- if (auto *declRef = dyn_cast<DeclRefExpr>(expr)) {
873
- if (auto *varDecl = dyn_cast<VarDecl>(declRef->getDecl ())) {
874
- if (auto varType = cs.getTypeIfAvailable (varDecl)) {
875
- varType->getTypeVariables (varRefs);
876
- }
877
- }
878
- }
858
+ SmallPtrSet<TypeVariableType *, 4 > typeVars;
859
+ ty->getTypeVariables (typeVars);
860
+ TypeVars.insert (typeVars.begin (), typeVars.end ());
861
+ }
879
862
880
- // FIXME: We can see UnresolvedDeclRefExprs here because we have
881
- // not yet run preCheckExpression() on the entire closure body
882
- // yet.
883
- //
884
- // We could consider pre-checking more eagerly.
885
- if ( auto *declRef = dyn_cast<UnresolvedDeclRefExpr>(expr)) {
886
- auto name = declRef-> getName ();
887
- auto loc = declRef-> getLoc ();
888
- if (name. isSimpleName () && loc. isValid ()) {
889
- auto *varDecl =
890
- dyn_cast_or_null<VarDecl>( ASTScope::lookupSingleLocalDecl (
891
- cs. DC -> getParentSourceFile (), name. getFullName (), loc) );
892
- if (varDecl)
893
- if ( auto varType = cs. getTypeIfAvailable (varDecl))
894
- varType-> getTypeVariables (varRefs );
895
- }
863
+ ASTWalker::PreWalkResult<Expr *>
864
+ VarRefCollector::walkToExprPre (Expr *expr) {
865
+ if ( auto *DRE = dyn_cast<DeclRefExpr>(expr))
866
+ inferTypeVars (DRE-> getDecl ());
867
+
868
+ // FIXME: We can see UnresolvedDeclRefExprs here because we don't walk into
869
+ // patterns when running preCheckExpression, since we don't resolve patterns
870
+ // until CSGen. We ought to consider moving pattern resolution into
871
+ // pre-checking, which would allow us to pre-check patterns normally.
872
+ if ( auto *declRef = dyn_cast<UnresolvedDeclRefExpr>(expr)) {
873
+ auto name = declRef-> getName ();
874
+ auto loc = declRef-> getLoc ( );
875
+ if (name. isSimpleName () && loc. isValid ()) {
876
+ auto *SF = CS. DC -> getParentSourceFile ();
877
+ auto *D = ASTScope::lookupSingleLocalDecl (SF, name. getFullName (), loc );
878
+ inferTypeVars (D);
896
879
}
897
-
898
- return Action::Continue (expr);
899
- }
900
-
901
- PreWalkAction walkToDeclPre (Decl *D) override {
902
- return Action::VisitChildrenIf (isa<PatternBindingDecl>(D));
903
880
}
904
- };
881
+ return Action::Continue (expr);
882
+ }
905
883
884
+ namespace {
906
885
class ConstraintGenerator : public ExprVisitor <ConstraintGenerator, Type> {
907
886
ConstraintSystem &CS;
908
887
DeclContext *CurDC;
@@ -1309,8 +1288,8 @@ struct VarRefCollector : public ASTWalker {
1309
1288
1310
1289
body->walk (refCollector);
1311
1290
1312
- referencedVars. append ( refCollector.varRefs . begin (),
1313
- refCollector. varRefs .end ());
1291
+ auto vars = refCollector.getTypeVars ();
1292
+ referencedVars. append (vars. begin (), vars .end ());
1314
1293
}
1315
1294
}
1316
1295
@@ -2971,9 +2950,7 @@ struct VarRefCollector : public ASTWalker {
2971
2950
if (!inferredType || inferredType->hasError ())
2972
2951
return Type ();
2973
2952
2974
- SmallVector<TypeVariableType *, 4 > referencedVars{
2975
- refCollector.varRefs .begin (), refCollector.varRefs .end ()};
2976
-
2953
+ auto referencedVars = refCollector.getTypeVars ();
2977
2954
CS.addUnsolvedConstraint (
2978
2955
Constraint::create (CS, ConstraintKind::FallbackType, closureType,
2979
2956
inferredType, locator, referencedVars));
0 commit comments