@@ -1064,43 +1064,42 @@ Optional<Type> TypeChecker::getTypeOfExpressionWithoutApplying(
1064
1064
return exprType;
1065
1065
}
1066
1066
1067
- // / Private class to "cleanse" an expression tree of types. This is done in the
1068
- // / case of a typecheck failure, where we may want to re-typecheck partially-
1069
- // / typechecked subexpressions in a context-free manner.
1070
- class TypeNullifier : public ASTWalker {
1071
- public:
1072
- std::pair<bool , Expr *> walkToExprPre (Expr *expr) override {
1067
+ void TypeChecker::eraseTypeData (Expr *&expr) {
1068
+ // / Private class to "cleanse" an expression tree of types. This is done in the
1069
+ // / case of a typecheck failure, where we may want to re-typecheck partially-
1070
+ // / typechecked subexpressions in a context-free manner.
1071
+ class TypeNullifier : public ASTWalker {
1072
+ public:
1073
+ std::pair<bool , Expr *> walkToExprPre (Expr *expr) override {
1074
+ // Preserve module expr type data to prevent further lookups.
1075
+ if (auto *declRef = dyn_cast<DeclRefExpr>(expr))
1076
+ if (isa<ModuleDecl>(declRef->getDecl ()))
1077
+ return { false , expr };
1078
+
1079
+ expr->setType (nullptr );
1080
+ return { true , expr };
1081
+ }
1073
1082
1074
- // Preserve module expr type data to prevent further lookups.
1075
- if (auto *declRef = dyn_cast<DeclRefExpr>(expr))
1076
- if (isa<ModuleDecl>(declRef->getDecl ()))
1077
- return { false , expr };
1083
+ // If we find a TypeLoc (e.g. in an as? expr) with a type variable, rewrite
1084
+ // it.
1085
+ bool walkToTypeLocPre (TypeLoc &TL) override {
1086
+ if (TL.getTypeRepr ())
1087
+ TL.setType (Type (), /* was validated*/ false );
1088
+ return true ;
1089
+ }
1078
1090
1079
- expr->setType (nullptr );
1091
+ std::pair<bool , Pattern*> walkToPatternPre (Pattern *pattern) override {
1092
+ pattern->setType (nullptr );
1093
+ return { true , pattern };
1094
+ }
1080
1095
1081
- if (auto cast = dyn_cast<ExplicitCastExpr>(expr)) {
1082
- if (cast->getCastTypeLoc ().getTypeRepr ())
1083
- cast->getCastTypeLoc ().setType (nullptr );
1084
- } else if (auto typeExpr = dyn_cast<TypeExpr>(expr)) {
1085
- if (typeExpr->getTypeLoc ().getTypeRepr ())
1086
- typeExpr->getTypeLoc ().setType (nullptr );
1087
- } else if (auto unresolvedSpec = dyn_cast<UnresolvedSpecializeExpr>(expr)) {
1088
- for (auto &arg : unresolvedSpec->getUnresolvedParams ()) {
1089
- if (arg.getTypeRepr ())
1090
- arg.setType (nullptr );
1091
- }
1096
+ // Don't walk into statements. This handles the BraceStmt in
1097
+ // non-single-expr closures, so we don't walk into their body.
1098
+ std::pair<bool , Stmt *> walkToStmtPre (Stmt *S) override {
1099
+ return { false , S };
1092
1100
}
1101
+ };
1093
1102
1094
- return { true , expr };
1095
- }
1096
-
1097
- std::pair<bool , Pattern*> walkToPatternPre (Pattern *pattern) override {
1098
- pattern->setType (nullptr );
1099
- return { true , pattern };
1100
- }
1101
- };
1102
-
1103
- void TypeChecker::eraseTypeData (Expr *&expr) {
1104
1103
expr->walk (TypeNullifier ());
1105
1104
}
1106
1105
0 commit comments