Skip to content

Commit

Permalink
[NFC, Typechecker] Remove UnsupportedProtocolVisitor and checkUnsuppo…
Browse files Browse the repository at this point in the history
…rtedProtocolType()
  • Loading branch information
theblixguy authored and AnthonyLatsis committed Aug 31, 2021
1 parent c2e5d04 commit 15f88e9
Show file tree
Hide file tree
Showing 4 changed files with 0 additions and 144 deletions.
4 changes: 0 additions & 4 deletions lib/Sema/MiscDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Stmt *>(S));

if (auto switchStmt = dyn_cast<SwitchStmt>(S))
checkSwitch(ctx, switchStmt);

Expand Down
2 changes: 0 additions & 2 deletions lib/Sema/TypeCheckDeclPrimary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1659,8 +1659,6 @@ class DeclChecker : public DeclVisitor<DeclChecker> {

DeclVisitor<DeclChecker>::visit(decl);

TypeChecker::checkUnsupportedProtocolType(decl);

if (auto VD = dyn_cast<ValueDecl>(decl)) {
auto &Context = getASTContext();
TypeChecker::checkForForbiddenPrefix(Context, VD->getBaseName());
Expand Down
122 changes: 0 additions & 122 deletions lib/Sema/TypeCheckType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3949,128 +3949,6 @@ Type TypeChecker::substMemberTypeWithBase(ModuleDecl *module,
return resultType;
}

namespace {

class UnsupportedProtocolVisitor
: public TypeReprVisitor<UnsupportedProtocolVisitor>, 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<CompoundIdentTypeRepr>(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<OpaqueReturnTypeRepr>(T))
return false;

visit(T);
return true;
}

std::pair<bool, Stmt*> 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<RequirementRepr> 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<ProtocolDecl>(decl)) {
checkUnsupportedProtocolType(ctx, protocolDecl->getTrailingWhereClause());
} else if (auto *genericDecl = dyn_cast<GenericTypeDecl>(decl)) {
checkUnsupportedProtocolType(ctx, genericDecl->getGenericParams());
checkUnsupportedProtocolType(ctx, genericDecl->getTrailingWhereClause());
} else if (auto *assocType = dyn_cast<AssociatedTypeDecl>(decl)) {
checkUnsupportedProtocolType(ctx, assocType->getTrailingWhereClause());
} else if (auto *extDecl = dyn_cast<ExtensionDecl>(decl)) {
checkUnsupportedProtocolType(ctx, extDecl->getTrailingWhereClause());
} else if (auto *subscriptDecl = dyn_cast<SubscriptDecl>(decl)) {
checkUnsupportedProtocolType(ctx, subscriptDecl->getGenericParams());
checkUnsupportedProtocolType(ctx, subscriptDecl->getTrailingWhereClause());
} else if (auto *funcDecl = dyn_cast<AbstractFunctionDecl>(decl)) {
if (!isa<AccessorDecl>(funcDecl)) {
checkUnsupportedProtocolType(ctx, funcDecl->getGenericParams());
checkUnsupportedProtocolType(ctx, funcDecl->getTrailingWhereClause());
}
}

if (isa<TypeDecl>(decl) || isa<ExtensionDecl>(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 {
Expand Down
16 changes: 0 additions & 16 deletions lib/Sema/TypeChecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 15f88e9

Please sign in to comment.