Skip to content

Commit 53104ac

Browse files
committed
simplify TypeNullifier by using the new walkToTypeLocPre hook of
ASTVisitor, and nest it inside the TypeChecker::eraseTypeData since that is its only client. NFC. Swift SVN r30184
1 parent 6d03fe0 commit 53104ac

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,43 +1064,42 @@ Optional<Type> TypeChecker::getTypeOfExpressionWithoutApplying(
10641064
return exprType;
10651065
}
10661066

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+
}
10731082

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+
}
10781090

1079-
expr->setType(nullptr);
1091+
std::pair<bool, Pattern*> walkToPatternPre(Pattern *pattern) override {
1092+
pattern->setType(nullptr);
1093+
return { true, pattern };
1094+
}
10801095

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 };
10921100
}
1101+
};
10931102

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) {
11041103
expr->walk(TypeNullifier());
11051104
}
11061105

0 commit comments

Comments
 (0)