From 15f88e9be3ad84b4c9831975a2acb1f1ddc442de Mon Sep 17 00:00:00 2001 From: Suyash Srijan Date: Thu, 3 Sep 2020 00:06:24 +0100 Subject: [PATCH] [NFC, Typechecker] Remove UnsupportedProtocolVisitor and checkUnsupportedProtocolType() --- lib/Sema/MiscDiagnostics.cpp | 4 - lib/Sema/TypeCheckDeclPrimary.cpp | 2 - lib/Sema/TypeCheckType.cpp | 122 ------------------------------ lib/Sema/TypeChecker.h | 16 ---- 4 files changed, 144 deletions(-) diff --git a/lib/Sema/MiscDiagnostics.cpp b/lib/Sema/MiscDiagnostics.cpp index 8e425b4d589f5..28fdbcdc60cde 100644 --- a/lib/Sema/MiscDiagnostics.cpp +++ b/lib/Sema/MiscDiagnostics.cpp @@ -3329,8 +3329,6 @@ static void checkSwitch(ASTContext &ctx, const SwitchStmt *stmt) { // We want to warn about "case .Foo, .Bar where 1 != 100:" since the where // clause only applies to the second case, and this is surprising. for (auto cs : stmt->getCases()) { - TypeChecker::checkUnsupportedProtocolType(ctx, cs); - // The case statement can have multiple case items, each can have a where. // If we find a "where", and there is a preceding item without a where, and // if they are on the same source line, then warn. @@ -4747,8 +4745,6 @@ void swift::performSyntacticExprDiagnostics(const Expr *E, void swift::performStmtDiagnostics(const Stmt *S, DeclContext *DC) { auto &ctx = DC->getASTContext(); - TypeChecker::checkUnsupportedProtocolType(ctx, const_cast(S)); - if (auto switchStmt = dyn_cast(S)) checkSwitch(ctx, switchStmt); diff --git a/lib/Sema/TypeCheckDeclPrimary.cpp b/lib/Sema/TypeCheckDeclPrimary.cpp index 1f4c1b4eba1b4..e2b21faafbf2b 100644 --- a/lib/Sema/TypeCheckDeclPrimary.cpp +++ b/lib/Sema/TypeCheckDeclPrimary.cpp @@ -1659,8 +1659,6 @@ class DeclChecker : public DeclVisitor { DeclVisitor::visit(decl); - TypeChecker::checkUnsupportedProtocolType(decl); - if (auto VD = dyn_cast(decl)) { auto &Context = getASTContext(); TypeChecker::checkForForbiddenPrefix(Context, VD->getBaseName()); diff --git a/lib/Sema/TypeCheckType.cpp b/lib/Sema/TypeCheckType.cpp index 8bd0e193b0e20..078cd050f5ef8 100644 --- a/lib/Sema/TypeCheckType.cpp +++ b/lib/Sema/TypeCheckType.cpp @@ -3949,128 +3949,6 @@ Type TypeChecker::substMemberTypeWithBase(ModuleDecl *module, return resultType; } -namespace { - -class UnsupportedProtocolVisitor - : public TypeReprVisitor, public ASTWalker -{ - ASTContext &Ctx; - bool checkStatements; - bool hitTopStmt; - -public: - UnsupportedProtocolVisitor(ASTContext &ctx, bool checkStatements) - : Ctx(ctx), checkStatements(checkStatements), hitTopStmt(false) { } - - bool walkToTypeReprPre(TypeRepr *T) override { - if (T->isInvalid()) - return false; - if (auto compound = dyn_cast(T)) { - // Only visit the last component to check, because nested typealiases in - // existentials are okay. - visit(compound->getComponentRange().back()); - return false; - } - // Arbitrary protocol constraints are OK on opaque types. - if (isa(T)) - return false; - - visit(T); - return true; - } - - std::pair walkToStmtPre(Stmt *S) override { - if (checkStatements && !hitTopStmt) { - hitTopStmt = true; - return { true, S }; - } - - return { false, S }; - } - - bool walkToDeclPre(Decl *D) override { - return !checkStatements; - } - - void visitTypeRepr(TypeRepr *T) { - // Do nothing for all TypeReprs except the ones listed below. - } - - void visitIdentTypeRepr(IdentTypeRepr *T) { - return; - } - - void visitRequirements(ArrayRef reqts) { - for (auto reqt : reqts) { - if (reqt.getKind() == RequirementReprKind::SameType) { - if (auto *repr = reqt.getFirstTypeRepr()) - repr->walk(*this); - if (auto *repr = reqt.getSecondTypeRepr()) - repr->walk(*this); - } - } - } -}; - -} // end anonymous namespace - -void TypeChecker::checkUnsupportedProtocolType(Decl *decl) { - if (!decl || decl->isInvalid()) - return; - - auto &ctx = decl->getASTContext(); - if (auto *protocolDecl = dyn_cast(decl)) { - checkUnsupportedProtocolType(ctx, protocolDecl->getTrailingWhereClause()); - } else if (auto *genericDecl = dyn_cast(decl)) { - checkUnsupportedProtocolType(ctx, genericDecl->getGenericParams()); - checkUnsupportedProtocolType(ctx, genericDecl->getTrailingWhereClause()); - } else if (auto *assocType = dyn_cast(decl)) { - checkUnsupportedProtocolType(ctx, assocType->getTrailingWhereClause()); - } else if (auto *extDecl = dyn_cast(decl)) { - checkUnsupportedProtocolType(ctx, extDecl->getTrailingWhereClause()); - } else if (auto *subscriptDecl = dyn_cast(decl)) { - checkUnsupportedProtocolType(ctx, subscriptDecl->getGenericParams()); - checkUnsupportedProtocolType(ctx, subscriptDecl->getTrailingWhereClause()); - } else if (auto *funcDecl = dyn_cast(decl)) { - if (!isa(funcDecl)) { - checkUnsupportedProtocolType(ctx, funcDecl->getGenericParams()); - checkUnsupportedProtocolType(ctx, funcDecl->getTrailingWhereClause()); - } - } - - if (isa(decl) || isa(decl)) - return; - - UnsupportedProtocolVisitor visitor(ctx, /*checkStatements=*/false); - decl->walk(visitor); -} - -void TypeChecker::checkUnsupportedProtocolType(ASTContext &ctx, Stmt *stmt) { - if (!stmt) - return; - - UnsupportedProtocolVisitor visitor(ctx, /*checkStatements=*/true); - stmt->walk(visitor); -} - -void TypeChecker::checkUnsupportedProtocolType( - ASTContext &ctx, TrailingWhereClause *whereClause) { - if (whereClause == nullptr) - return; - - UnsupportedProtocolVisitor visitor(ctx, /*checkStatements=*/false); - visitor.visitRequirements(whereClause->getRequirements()); -} - -void TypeChecker::checkUnsupportedProtocolType( - ASTContext &ctx, GenericParamList *genericParams) { - if (genericParams == nullptr) - return; - - UnsupportedProtocolVisitor visitor(ctx, /*checkStatements=*/false); - visitor.visitRequirements(genericParams->getRequirements()); -} - Type CustomAttrTypeRequest::evaluate(Evaluator &eval, CustomAttr *attr, DeclContext *dc, CustomAttrTypeKind typeKind) const { diff --git a/lib/Sema/TypeChecker.h b/lib/Sema/TypeChecker.h index d89144a636847..027a8afda44f2 100644 --- a/lib/Sema/TypeChecker.h +++ b/lib/Sema/TypeChecker.h @@ -242,22 +242,6 @@ Type getOptionalType(SourceLoc loc, Type elementType); Expr *resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context, bool replaceInvalidRefsWithErrors); -/// Check for unsupported protocol types in the given declaration. -void checkUnsupportedProtocolType(Decl *decl); - -/// Check for unsupported protocol types in the given statement. -void checkUnsupportedProtocolType(ASTContext &ctx, Stmt *stmt); - -/// Check for unsupported protocol types in the given generic requirement -/// list. -void checkUnsupportedProtocolType(ASTContext &ctx, - TrailingWhereClause *whereClause); - -/// Check for unsupported protocol types in the given generic requirement -/// list. -void checkUnsupportedProtocolType(ASTContext &ctx, - GenericParamList *genericParams); - /// Substitute the given base type into the type of the given nested type, /// producing the effective type that the nested type will have. ///