AST: fix casting from Decl to GenericContext

Fixes `cast<GenericContext>(someDecl)`.
I don't know why this didn't show up as a crash so far. But slightly changing `Decl` results in a wrong type cast and a crash.
This commit is contained in:
Erik Eckstein
2024-10-01 09:30:18 +02:00
parent fadff007a1
commit 7f43e52c45
2 changed files with 26 additions and 0 deletions

View File

@@ -1568,6 +1568,8 @@ public:
/// Retrieve the position of any where clause for this context's
/// generic parameters.
SourceRange getGenericTrailingWhereClauseSourceRange() const;
static bool classof(const Decl *D);
};
static_assert(sizeof(_GenericContext) + sizeof(DeclContext) ==
sizeof(GenericContext), "Please add fields to _GenericContext");
@@ -9589,6 +9591,16 @@ inline bool DeclContext::classof(const Decl *D) {
}
}
inline bool GenericContext::classof(const Decl *D) {
switch (D->getKind()) { //
default: return false;
#define DECL(ID, PARENT) // See previous line
#define GENERIC_DECL(ID, PARENT) \
case DeclKind::ID: return true;
#include "swift/AST/DeclNodes.def"
}
}
inline DeclContext *DeclContext::castDeclToDeclContext(const Decl *D) {
// XXX -- ModuleDecl is not defined in Decl.h, but because DeclContexts
// preface decls in memory, any DeclContext type will due.