[clang] [Clang] Fix APSInt width of template argument in FinishCXXExpansionStmt() (PR #208859)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Jul 10 16:50:11 PDT 2026
https://github.com/Sirraide created https://github.com/llvm/llvm-project/pull/208859
After merging #169680, we started getting assertions in IntegerLiteral::Create() on some ARM systems:
Assertion `V.getBitWidth() == C.getIntWidth(type) &&
"Integer type is not the correct size for constant."'
The expansion statements patch doesn't ever create an IntegerLiteral directly, but there is one place where we create a TemplateArgument of type 'ptrdiff_t', but we unconditionally set the bit width of its APSInt to 64 bits.
Presumably, the assertion is due to 'ptrdiff_t' not being a 64-bit type on these systems, so make sure that we query its bit width and use that as the width of the APSInt.
I don't have a way of verifying if this patch is correct since pre-commit CI doesn't run on the affected system and I don't have an ARM machine, but I'm merging this anyway since this issue is breaking CI.
>From d252a7e3902623b66b5b43b4024a4ce186948444 Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Sat, 11 Jul 2026 01:43:51 +0200
Subject: [PATCH 1/2] [Clang] Fix APSInt width of template argument in
FinishCXXExpansionStmt()
After merging #169680, we started getting assertions in
IntegerLiteral::Create() on some ARM systems:
Assertion `V.getBitWidth() == C.getIntWidth(type) &&
"Integer type is not the correct size for constant."'
The expansion statements patch doesn't ever create an
IntegerLiteral directly, but there is one place where we
create a TemplateArgument of type 'ptrdiff_t', but we
unconditionally set the bit width of its APSInt to 64
bits.
Presumably, the assertion is due to 'ptrdiff_t' not being
a 64-bit type on these systems, so make sure that we query
its bit width and use that as the width of the APSInt.
I don't have a way of verifying if this patch is correct
since pre-commit CI doesn't run on the affected system and
I don't have an ARM machine, but I'm merging this anyway
since this issue is breaking CI.
---
clang/lib/Sema/SemaExpand.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExpand.cpp b/clang/lib/Sema/SemaExpand.cpp
index 9d8893dbdb731..43c27df0ddac6 100644
--- a/clang/lib/Sema/SemaExpand.cpp
+++ b/clang/lib/Sema/SemaExpand.cpp
@@ -558,9 +558,13 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
// Expand the body for each instantiation.
SmallVector<Stmt *, 4> Instantiations;
CXXExpansionStmtDecl *ESD = Expansion->getDecl();
+ QualType PtrDiffT = Context.getPointerDiffType();
+ unsigned PtrDiffTWidth = Context.getIntWidth(PtrDiffT);
+ bool PtrDiffTIsSigned = PtrDiffT->isSignedIntegerType();
for (uint64_t I = 0; I < *NumInstantiations; ++I) {
- TemplateArgument Arg{Context, llvm::APSInt::get(I),
- Context.getPointerDiffType()};
+ llvm::APInt IVal{PtrDiffTWidth, I};
+ TemplateArgument Arg{
+ Context, llvm::APSInt{std::move(IVal), PtrDiffTIsSigned}, PtrDiffT};
MultiLevelTemplateArgumentList MTArgList(ESD, Arg, true);
MTArgList.addOuterRetainedLevels(
Expansion->getDecl()->getIndexTemplateParm()->getDepth());
>From 22e4248e124e2d31924c67fc137d990f28c1dd7b Mon Sep 17 00:00:00 2001
From: Sirraide <aeternalmail at gmail.com>
Date: Sat, 11 Jul 2026 01:49:55 +0200
Subject: [PATCH 2/2] APSInt takes IsUnsigned, not IsSigned
---
clang/lib/Sema/SemaExpand.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Sema/SemaExpand.cpp b/clang/lib/Sema/SemaExpand.cpp
index 43c27df0ddac6..779b7add08344 100644
--- a/clang/lib/Sema/SemaExpand.cpp
+++ b/clang/lib/Sema/SemaExpand.cpp
@@ -560,11 +560,11 @@ StmtResult Sema::FinishCXXExpansionStmt(Stmt *Exp, Stmt *Body) {
CXXExpansionStmtDecl *ESD = Expansion->getDecl();
QualType PtrDiffT = Context.getPointerDiffType();
unsigned PtrDiffTWidth = Context.getIntWidth(PtrDiffT);
- bool PtrDiffTIsSigned = PtrDiffT->isSignedIntegerType();
+ bool PtrDiffTIsUnsigned = PtrDiffT->isUnsignedIntegerType();
for (uint64_t I = 0; I < *NumInstantiations; ++I) {
llvm::APInt IVal{PtrDiffTWidth, I};
TemplateArgument Arg{
- Context, llvm::APSInt{std::move(IVal), PtrDiffTIsSigned}, PtrDiffT};
+ Context, llvm::APSInt{std::move(IVal), PtrDiffTIsUnsigned}, PtrDiffT};
MultiLevelTemplateArgumentList MTArgList(ESD, Arg, true);
MTArgList.addOuterRetainedLevels(
Expansion->getDecl()->getIndexTemplateParm()->getDepth());
More information about the cfe-commits
mailing list