llvm / llvm/llvm-project

[Clang] Assertions clang::Type::castAs() const [with T = clang::AtomicType]: Assertion `isa<T>(CanonicalType)' failed

Open
#207,610 3 comments 0 reactions 0 assignees View on GitHub
clang:frontend confirmed crash-on-valid regression:17
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

I guess that it's related to the AST changes after Clang-16, but it requires more investigations

Code:

```cpp
struct S {
char a;
};

void static_cast_non_atomic_to_atomic() {
_Atomic struct S a;
a = static_cast<_Atomic(struct S)>(S{});
}
```

Compiler Explorer: https://godbolt.org/z/9PoGsEsEh

This goes back to clang-17.

Stack trace

```
clang++: /root/llvm-project/llvm/tools/clang/include/clang/AST/TypeBase.h:9349: const T* clang::Type::castAs() const [with T = clang::AtomicType]: Assertion `isa(CanonicalType)' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and dumped files.
Stack dump:
0. Program arguments: /opt/compiler-explorer/clang-assertions-trunk/bin/clang++ -g -o /app/output.s -mllvm --x86-asm-syntax=intel -fno-verbose-asm -S --gcc-toolchain=/opt/compiler-explorer/gcc-snapshot -fcolor-diagnostics -fno-crash-diagnostics
1. :8:1: current parser token '}'
2. :5:41: parsing function body 'static_cast_non_atomic_to_atomic'
3. :5:41: in compound statement ('{}')
#0 0x000000000439bfa8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x439bfa8)
#1 0x0000000004398ec4 llvm::sys::RunSignalHandlers() (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x4398ec4)
#2 0x00000000043994e4 llvm::sys::CleanupOnSignal(unsigned long) (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x43994e4)
#3 0x00000000042d7598 CrashRecoverySignalHandler(int) CrashRecoveryContext.cpp:0:0
#4 0x00007e13b3045330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x00007e13b309eb2c pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x9eb2c)
#6 0x00007e13b304527e raise (/lib/x86_64-linux-gnu/libc.so.6+0x4527e)
#7 0x00007e13b30288ff abort (/lib/x86_64-linux-gnu/libc.so.6+0x288ff)
#8 0x00007e13b302881b (/lib/x86_64-linux-gnu/libc.so.6+0x2881b)
#9 0x00007e13b303b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#10 0x000000000800ec1a (/opt/compiler-explorer/clang-assertions-trunk/bin/clang+++0x800ec1a)
#11 0x00000000080830ce (anonymous namespace)::AtomicExprEvaluator::ZeroInitialization(clang::Expr const*) ExprConstant.cpp:0:0
#12 0x00000000080afdd5 clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#13 0x00000000080b0540 clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#14 0x00000000080b0990 (anonymous namespace)::ExprEvaluatorBase<(anonymous namespace)::AtomicExprEvaluator>::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#15 0x00000000080af21f clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#16 0x00000000080b0990 (anonymous namespace)::ExprEvaluatorBase<(anonymous namespace)::AtomicExprEvaluator>::VisitCastExpr(clang::CastExpr const*) ExprConstant.cpp:0:0
#17 0x00000000080af391 clang::StmtVisitorBase::Visit(clang::Stmt const*) ExprConstant.cpp:0:0
#18 0x00000000080b0866 EvaluateAtomic(clang::Expr const*, (anonymous namespace)::LValue const*, clang::APValue&, (anonymous namespace)::EvalInfo&) ExprConstant.cpp:0:0
#19 0x0000000008070e82 Evaluate(clang::APValue&, (anonymous namespace)::EvalInfo&, clang::Expr const*) ExprConstant.cpp:0:0
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the assertion with the provided C++ example and compiler options, then inspect ExprConstant.cpp around AtomicExprEvaluator::ZeroInitialization and the stack-trace path through EvaluateAtomic. Determine why the non-atomic-to-atomic cast reaches an invalid AtomicType cast. Done means the example no longer asserts; add or update a regression test if the surrounding test structure identifies an appropriate location.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.