Issue 207953
Summary [Clang] Assertion failure in getASTRecordLayout() when handling explicit template specialization after instantiation
Labels bug, clang, clang:diagnostics, crash
Assignees AditiRM
Reporter AditiRM
    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):
```
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;
}

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

extern template class X<int>::Y;

int main() {
    X<int> 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<int>::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<int>::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)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to