[clang] [Clang] Fix assertion failure in getASTRecordLayout for invalid declarations (PR #208153)
Aditi Medhane via cfe-commits
cfe-commits at lists.llvm.org
Tue Aug 11 01:30:26 PDT 2026
================
@@ -3384,9 +3384,30 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
// not a complete definition (which is what isCompleteDefinition() tests)
// until we *finish* parsing the definition.
D = D->getDefinition();
- assert(D && "Cannot get layout of forward declarations!");
- assert(!D->isInvalidDecl() && "Cannot get layout of invalid decl!");
- assert(D->isCompleteDefinition() && "Cannot layout type before complete!");
+
+ // Handle invalid declarations gracefully during error recovery
+ // This can happen when there are template specialization errors
+ if (!D || D->isInvalidDecl() || !D->isCompleteDefinition()) {
+ // Check if we already have a cached layout
+ const ASTRecordLayout *Entry = ASTRecordLayouts[D];
+ if (Entry)
+ return *Entry;
+
+ // Create a minimal safe layout for error recovery
----------------
AditiRM wrote:
The new approach avoids this by not creating a dummy layout or continuing with potentially invalid data. Invalid declarations are simply skipped using continue or an early return, avoiding fake layout information and potential follow-on diagnostics.
https://github.com/llvm/llvm-project/pull/208153
More information about the cfe-commits
mailing list