[clang] Crash in `GetFixedPointRank` with `BitIntType`
See:
constexpr _BitInt(128) i = 42;
static_assert(i == 42.0k);
https://godbolt.org/z/PfaKxM6df
Backtrace:
clang-23: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaExpr.cpp:1483: unsigned int GetFixedPointRank(clang::QualType): Assertion `BTy && "Expected a builtin type."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -fno-verbose-asm -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/lib/clang/23 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0/x86_64-linux-gnu -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0/backward -internal-isystem /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/lib/clang/23/include -internal-isystem /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/../include/x86_64-unknown-linux-gnu -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ffixed-point -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -fdwarf2-cfi-asm -o /tmp/example-11d270.o -x c++ <source>
1. <source>:3:25: current parser token ')'
#0 0x0000000004556d58 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x4556d58)
#1 0x0000000004553b84 llvm::sys::RunSignalHandlers() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x4553b84)
#2 0x0000000004553cfa SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x0000000004553d98 SignalHandlerTerminate(int, siginfo_t*, void*) Signals.cpp:0:0
#4 0x000072b686842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#5 0x000072b6868969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#6 0x000072b686842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#7 0x000072b6868287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#8 0x000072b68682871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#9 0x000072b686839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#10 0x000000000739a613 (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x739a613)
#11 0x000000000741aaf9 clang::Sema::UsualArithmeticConversions(clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::SourceLocation, clang::ArithConvKind) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x741aaf9)
#12 0x000000000742051d clang::Sema::CheckCompareOperands(clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::SourceLocation, clang::BinaryOperatorKind) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742051d)
#13 0x000000000742818f clang::Sema::CreateBuiltinBinOp(clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr*, clang::Expr*, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742818f)
#14 0x0000000007429e0b clang::Sema::BuildBinOp(clang::Scope*, clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr*, clang::Expr*, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x7429e0b)
#15 0x000000000742a276 clang::Sema::ActOnBinOp(clang::Scope*, clang::SourceLocation, clang::tok::TokenKind, clang::Expr*, clang::Expr*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742a276)
#16 0x0000000006f22aec clang::Parser::ParseRHSOfBinaryExpression(clang::ActionResult<clang::Expr*, true>, clang::prec::Level) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f22aec)
#17 0x0000000006f2bdba clang::Parser::ParseConstantExpressionInExprEvalContext(clang::TypoCorrectionTypeBehavior) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f2bdba)
#18 0x0000000006f03e86 clang::Parser::ParseStaticAssertDeclaration(clang::SourceLocation&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f03e86)
#19 0x0000000006ef24b6 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6ef24b6)
#20 0x0000000006eae486 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6eae486)
#21 0x0000000006eaf375 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6eaf375)
#22 0x0000000006e9025a clang::ParseAST(clang::Sema&, bool, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6e9025a)
#23 0x000000000506bf68 clang::CodeGenAction::ExecuteAction() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x506bf68)
#24 0x0000000005392de5 clang::FrontendAction::Execute() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x5392de5)
#25 0x0000000005311988 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x5311988)
#26 0x000000000548f6ad clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x548f6ad)
#27 0x0000000000df39db cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xdf39db)
#28 0x0000000000dea33a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#29 0x0000000000dee76e clang_main(int, char**, llvm::ToolContext const&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xdee76e)
#30 0x0000000000ca6a1a main (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xca6a1a)
#31 0x000072b686829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
@llvm/issue-subscribers-clang-frontend
Author: Timm Baeder (tbaederr)
Backtrace:
clang-23: /root/llvm-project/llvm/tools/clang/lib/Sema/SemaExpr.cpp:1483: unsigned int GetFixedPointRank(clang::QualType): Assertion `BTy && "Expected a builtin type."' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
0. Program arguments: /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23 -cc1 -triple x86_64-unknown-linux-gnu -emit-obj -dumpdir /app/output.s- -disable-free -clear-ast-before-backend -main-file-name example.cpp -mrelocation-model pic -pic-level 2 -pic-is-pie -mframe-pointer=all -fmath-errno -ffp-contract=on -fno-rounding-math -fno-verbose-asm -mconstructor-aliases -funwind-tables=2 -target-cpu x86-64 -tune-cpu generic -debug-info-kind=constructor -dwarf-version=5 -debugger-tuning=gdb -fdebug-compilation-dir=/app -fcoverage-compilation-dir=/app -resource-dir /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/lib/clang/23 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0 -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0/x86_64-linux-gnu -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../include/c++/17.0.0/backward -internal-isystem /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/lib/clang/23/include -internal-isystem /cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/../include/x86_64-unknown-linux-gnu -internal-isystem /usr/local/include -internal-isystem /opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/17.0.0/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -ffixed-point -fdeprecated-macro -ferror-limit 19 -fgnuc-version=4.2.1 -fskip-odr-check-in-gmf -fcxx-exceptions -fexceptions -fcolor-diagnostics -faddrsig -fdwarf2-cfi-asm -o /tmp/example-11d270.o -x c++ <source>
1. <source>:3:25: current parser token ')'
#<!-- -->0 0x0000000004556d58 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x4556d58)
#<!-- -->1 0x0000000004553b84 llvm::sys::RunSignalHandlers() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x4553b84)
#<!-- -->2 0x0000000004553cfa SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#<!-- -->3 0x0000000004553d98 SignalHandlerTerminate(int, siginfo_t*, void*) Signals.cpp:0:0
#<!-- -->4 0x000072b686842520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#<!-- -->5 0x000072b6868969fc pthread_kill (/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
#<!-- -->6 0x000072b686842476 gsignal (/lib/x86_64-linux-gnu/libc.so.6+0x42476)
#<!-- -->7 0x000072b6868287f3 abort (/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
#<!-- -->8 0x000072b68682871b (/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
#<!-- -->9 0x000072b686839e96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#<!-- -->10 0x000000000739a613 (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x739a613)
#<!-- -->11 0x000000000741aaf9 clang::Sema::UsualArithmeticConversions(clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::SourceLocation, clang::ArithConvKind) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x741aaf9)
#<!-- -->12 0x000000000742051d clang::Sema::CheckCompareOperands(clang::ActionResult<clang::Expr*, true>&, clang::ActionResult<clang::Expr*, true>&, clang::SourceLocation, clang::BinaryOperatorKind) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742051d)
#<!-- -->13 0x000000000742818f clang::Sema::CreateBuiltinBinOp(clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr*, clang::Expr*, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742818f)
#<!-- -->14 0x0000000007429e0b clang::Sema::BuildBinOp(clang::Scope*, clang::SourceLocation, clang::BinaryOperatorKind, clang::Expr*, clang::Expr*, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x7429e0b)
#<!-- -->15 0x000000000742a276 clang::Sema::ActOnBinOp(clang::Scope*, clang::SourceLocation, clang::tok::TokenKind, clang::Expr*, clang::Expr*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x742a276)
#<!-- -->16 0x0000000006f22aec clang::Parser::ParseRHSOfBinaryExpression(clang::ActionResult<clang::Expr*, true>, clang::prec::Level) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f22aec)
#<!-- -->17 0x0000000006f2bdba clang::Parser::ParseConstantExpressionInExprEvalContext(clang::TypoCorrectionTypeBehavior) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f2bdba)
#<!-- -->18 0x0000000006f03e86 clang::Parser::ParseStaticAssertDeclaration(clang::SourceLocation&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6f03e86)
#<!-- -->19 0x0000000006ef24b6 clang::Parser::ParseDeclaration(clang::DeclaratorContext, clang::SourceLocation&, clang::ParsedAttributes&, clang::ParsedAttributes&, clang::SourceLocation*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6ef24b6)
#<!-- -->20 0x0000000006eae486 clang::Parser::ParseExternalDeclaration(clang::ParsedAttributes&, clang::ParsedAttributes&, clang::ParsingDeclSpec*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6eae486)
#<!-- -->21 0x0000000006eaf375 clang::Parser::ParseTopLevelDecl(clang::OpaquePtr<clang::DeclGroupRef>&, clang::Sema::ModuleImportState&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6eaf375)
#<!-- -->22 0x0000000006e9025a clang::ParseAST(clang::Sema&, bool, bool) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x6e9025a)
#<!-- -->23 0x000000000506bf68 clang::CodeGenAction::ExecuteAction() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x506bf68)
#<!-- -->24 0x0000000005392de5 clang::FrontendAction::Execute() (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x5392de5)
#<!-- -->25 0x0000000005311988 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x5311988)
#<!-- -->26 0x000000000548f6ad clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0x548f6ad)
#<!-- -->27 0x0000000000df39db cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xdf39db)
#<!-- -->28 0x0000000000dea33a ExecuteCC1Tool(llvm::SmallVectorImpl<char const*>&, llvm::ToolContext const&, llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>) driver.cpp:0:0
#<!-- -->29 0x0000000000dee76e clang_main(int, char**, llvm::ToolContext const&) (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xdee76e)
#<!-- -->30 0x0000000000ca6a1a main (/cefs/0e/0e50efbab882e70f315e0878_clang-assertions-trunk-20260511/bin/clang-23+0xca6a1a)
#<!-- -->31 0x000072b686829d90 (/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
This could be a good first issue if I knew what the correct thing to do was.
After some research, I think the problem is that Clang currently lacks a set of rules for handling the conversion from BitInt to FixedPoint, and since BitInt is defined as an int type, it directly enters the operations between FixedPoint, triggering assertions.
https://github.com/llvm/llvm-project/blob/f8fcded5f550bbd332c5bb3591ab0b538ea70766/clang/lib/Sema/SemaExpr.cpp#L1775-L1776
If not explicitly rejected, it will still reach the downstream handleIntegerConversion, still triggering assertions.
https://github.com/llvm/llvm-project/blob/f8fcded5f550bbd332c5bb3591ab0b538ea70766/clang/lib/Sema/SemaExpr.cpp#L1782-L1784
Therefore, a patch would be to explicitly reject such operations (which is quite reasonable, as they shouldn't be possible anyway).
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 98062afae457..24fcaf63401b 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -1287,6 +1287,10 @@ static QualType handleFloatConversion(Sema &S, ExprResult &LHS,
/// Helper function of UsualArithmeticConversions().
static bool unsupportedTypeConversion(const Sema &S, QualType LHSType,
QualType RHSType) {
+ if ((LHSType->isFixedPointType() && RHSType->isBitIntType()) ||
+ (LHSType->isBitIntType() && RHSType->isFixedPointType()))
+ return true;
+
// No issue if either is not a floating point type.
if (!LHSType->isFloatingType() || !RHSType->isFloatingType())
return false;
After this, the diagnose has been more reasonable
repro.cpp:2:17: error: invalid operands to binary expression ('const _BitInt(128)' and '_Accum')
2 | static_assert(i == 42.0k);
| ~ ^ ~~~~~
1 error generated.
However, I'm wondering if it's possible to explore allowing such operations under specific circumstances (e.g., when BitInt is 32-bit or 64-bit).
I want to work on this.
I want to work on this.
Feel free to do it. This issue will be assigned when you initiate a pull request.
When we file issues we need to check for duplicates: https://github.com/llvm/llvm-project/issues/191701
Not catching duplicate can lead to duplication of work and confusing when triaging old issues.