Skip to content

[AST|ASTWalker] Fix assertion hit walking an invalid AST with a protocol decl inside an extension #29338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 10 additions & 15 deletions lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,9 +223,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

bool visitTypeAliasDecl(TypeAliasDecl *TAD) {
if (TAD->getGenericParams() &&
Walker.shouldWalkIntoGenericParams()) {

if (Walker.shouldWalkIntoGenericParams() && TAD->getGenericParams()) {
if (visitGenericParamList(TAD->getGenericParams()))
return true;
}
Expand All @@ -237,8 +235,7 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

bool visitOpaqueTypeDecl(OpaqueTypeDecl *OTD) {
if (OTD->getGenericParams() &&
Walker.shouldWalkIntoGenericParams()) {
if (Walker.shouldWalkIntoGenericParams() && OTD->getGenericParams()) {
if (visitGenericParamList(OTD->getGenericParams()))
return true;
}
Expand Down Expand Up @@ -272,16 +269,15 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,
}

// Visit requirements
if (auto *Protocol = dyn_cast<ProtocolDecl>(NTD)) {
if (auto *WhereClause = Protocol->getTrailingWhereClause()) {
for (auto &Req: WhereClause->getRequirements()) {
if (doIt(Req))
return true;
}
}
}
if (WalkGenerics) {
for (auto Req: NTD->getGenericParams()->getTrailingRequirements()) {
ArrayRef<swift::RequirementRepr> Reqs = None;
if (auto *Protocol = dyn_cast<ProtocolDecl>(NTD)) {
if (auto *WhereClause = Protocol->getTrailingWhereClause())
Reqs = WhereClause->getRequirements();
} else {
Reqs = NTD->getGenericParams()->getTrailingRequirements();
}
for (auto Req: Reqs) {
if (doIt(Req))
return true;
}
Expand Down Expand Up @@ -1329,7 +1325,6 @@ class Traversal : public ASTVisitor<Traversal, Expr*, Stmt*,

private:
bool visitGenericParamListIfNeeded(GenericContext *gc) {
// Must check this first in case extensions have not been bound yet
if (Walker.shouldWalkIntoGenericParams()) {
if (auto *params = gc->getGenericParams()) {
visitGenericParamList(params);
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3613,7 +3613,7 @@ static Type computeNominalType(NominalTypeDecl *decl, DeclTypeKind kind) {
// Get the parent type.
Type Ty;
DeclContext *dc = decl->getDeclContext();
if (dc->isTypeContext()) {
if (!isa<ProtocolDecl>(decl) && dc->isTypeContext()) {
switch (kind) {
case DeclTypeKind::DeclaredType: {
auto *nominal = dc->getSelfNominalTypeDecl();
Expand Down
8 changes: 8 additions & 0 deletions test/IDE/coloring_invalid.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
// RUN: %target-swift-ide-test -syntax-coloring -source-filename %s | %FileCheck %s

public enum Foo {}
// CHECK: <attr-builtin>public</attr-builtin> <kw>enum</kw> Foo {}
public extension Foo {
// CHECK: <attr-builtin>public</attr-builtin> <kw>extension</kw> <type>Foo</type> {
protocol Bar {}
// CHECK: <kw>protocol</kw> Bar {}
}

public enum Result {
// CHECK: <attr-builtin>public</attr-builtin> <kw>enum</kw> Result {
case success(a b = {
Expand Down