llvm / llvm/llvm-project

[Clang] Assertion failure in getASTRecordLayout() when handling explicit template specialization after instantiation

Open
#207,953 4 comments 0 reactions 1 assignee Claimed by @AditiRM View on GitHub
clang:diagnostics crash regression:22
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Clang crashes with an assertion failure instead of gracefully reporting a compilation error when encountering explicit template specialization after instantiation.

Environment
- Clang Version: trunk/main (also affects released versions)
- Build Config: +assertions
- Platforms Affected: All platforms (reproduced on AIX, likely affects Linux/macOS/Windows)
- Compiler Flags: `-std=c++11` (affects all C++ standards)

Reproducer Test Case (test.cpp):

```c++
template
struct X {
struct Y {
Y() : v(0) {}
int v;
int getValue();
} y;
};

template
int X::Y::getValue() {
return ++v;
}

template <> struct X::Y { int getValue() { return 55; } };

extern template class X::Y;

int main() {
X x;
return x.y.getValue();
}
```

Compilation Command:

`clang++ -std=c++11 -c test.cpp`

Expected Behavior

```
test.cpp:15:28: error: explicit specialization of 'Y' after instantiation
15 | template <> struct X::Y { int getValue() { return 55; } };
| ^
test.cpp:7:7: note: implicit instantiation first required here
7 | } y;
| ^
1 error generated.

Exit code: 1
```

Actual Behavior
```
test.cpp:15:28: error: explicit specialization of 'Y' after instantiation
15 | template <> struct X::Y { int getValue() { return 55; } };
| ^
test.cpp:7:7: note: implicit instantiation first required here
7 | } y;
| ^
Assertion failed: !D->isInvalidDecl() && "Cannot get layout of invalid decl!"
File: clang/lib/AST/RecordLayoutBuilder.cpp
Line: 3388
Function: const ASTRecordLayout &clang::ASTContext::getASTRecordLayout(const RecordDecl *) const

Stack dump:
0. Program arguments: clang++ -std=c++11 -c test.cpp
1. test.cpp:20:13: current parser token ';'
2. test.cpp:19:12: parsing function body 'main'
3. test.cpp:19:12: in compound statement ('{}')
[Stack trace...]
clang: error: clang frontend command failed due to signal

Exit code: 134 (SIGABRT)
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.