[clang] [Clang] Fix assertion failure in getASTRecordLayout for invalid declarations (PR #208153)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 8 20:52:53 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Aditi Medhane (AditiRM)
<details>
<summary>Changes</summary>
When encountering invalid declarations during error recovery (e.g., explicit template specialization after instantiation), `getASTRecordLayout()` would assert and crash instead of handling the error gracefully.
This patch adds a check for invalid/incomplete declarations and returns a minimal safe layout (1-byte size/alignment) to allow error recovery to continue, enabling the compiler to report all errors and exit cleanly.
Before this fix:
- Assertion failed: `!D->isInvalidDecl()`
- Exit code: 134 (SIGABRT)
After this fix:
- Clean error messages
- Exit code: 1
Test updated: `clang/test/SemaCXX/explicit-specialization-after-instantiation-crash.cpp`
Addresses this issue : https://github.com/llvm/llvm-project/issues/207953
---
Full diff: https://github.com/llvm/llvm-project/pull/208153.diff
2 Files Affected:
- (modified) clang/lib/AST/RecordLayoutBuilder.cpp (+26)
- (added) clang/test/SemaCXX/explicit-specialization-after-instantiation-crash.cpp (+42)
``````````diff
diff --git a/clang/lib/AST/RecordLayoutBuilder.cpp b/clang/lib/AST/RecordLayoutBuilder.cpp
index 854f88f20b2b6..6b5027772885d 100644
--- a/clang/lib/AST/RecordLayoutBuilder.cpp
+++ b/clang/lib/AST/RecordLayoutBuilder.cpp
@@ -3384,6 +3384,32 @@ ASTContext::getASTRecordLayout(const RecordDecl *D) const {
// not a complete definition (which is what isCompleteDefinition() tests)
// until we *finish* parsing the definition.
D = D->getDefinition();
+
+ // 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
+ // Use 1-byte size and alignment to avoid division by zero or other issues
+ ASTRecordLayout *NewEntry = new (*this)
+ ASTRecordLayout(*this,
+ CharUnits::One(), // Size = 1 byte
+ CharUnits::One(), // Alignment = 1 byte
+ CharUnits::One(), // PreferredAlignment = 1 byte
+ CharUnits::One(), // UnadjustedAlignment = 1 byte
+ CharUnits::One(), // RequiredAlignment = 1 byte
+ CharUnits::One(), // DataSize = 1 byte
+ ArrayRef<uint64_t>() // Empty field offsets
+ );
+
+ ASTRecordLayouts[D] = NewEntry;
+ return *NewEntry;
+ }
+
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!");
diff --git a/clang/test/SemaCXX/explicit-specialization-after-instantiation-crash.cpp b/clang/test/SemaCXX/explicit-specialization-after-instantiation-crash.cpp
new file mode 100644
index 0000000000000..37b00db4d3877
--- /dev/null
+++ b/clang/test/SemaCXX/explicit-specialization-after-instantiation-crash.cpp
@@ -0,0 +1,42 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+//
+// Test that explicit template specialization after instantiation
+// is handled gracefully without assertion failure.
+//
+// Before the fix, this code triggered an assertion failure:
+// Assertion failed: !D->isInvalidDecl() && "Cannot get layout of invalid decl!"
+// Location: clang/lib/AST/RecordLayoutBuilder.cpp:3388
+// Exit code: 134 (SIGABRT)
+//
+// After the fix:
+// Clean error messages are reported
+// Exit code: 1
+//
+// The fix adds error handling in getASTRecordLayout() to return a minimal
+// safe layout (1-byte size/alignment) for invalid declarations, allowing
+// error recovery to continue and report all errors.
+
+template <typename T>
+struct X {
+ struct Y {
+ Y() : v(0) {}
+ int v;
+ int getValue();
+ } y;
+};
+
+template <typename T>
+int X<T>::Y::getValue() {
+ return ++v;
+}
+
+// expected-error at +1 {{explicit specialization of 'Y' after instantiation}}
+template <> struct X<int>::Y { int getValue() { return 55; } };
+// expected-note at -10 {{implicit instantiation first required here}}
+
+extern template class X<int>::Y;
+
+int main() {
+ X<int> x;
+ return x.y.getValue(); // expected-error {{no member named 'getValue' in 'X<int>::Y'}}
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/208153
More information about the cfe-commits
mailing list