[clang] dc550be - [clang][Sema] Recheck array size once the element type is complete (#219817)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Nov 6 21:08:51 PST 2026
Author: Akash Manna
Date: 2026-11-07T13:08:46+08:00
New Revision: dc550beb73ca66333785b235c79dc8947088555a
URL: https://github.com/llvm/llvm-project/commit/dc550beb73ca66333785b235c79dc8947088555a
DIFF: https://github.com/llvm/llvm-project/commit/dc550beb73ca66333785b235c79dc8947088555a.diff
LOG: [clang][Sema] Recheck array size once the element type is complete (#219817)
Fixes #213855
`BuildArrayType` can't check an array's byte size when the element type
is still incomplete, for example an array of a class template
specialization that hasn't been instantiated yet. Nothing rechecked it
afterwards, so a `~2^67` byte array type survived as a valid field and
the first size query asserted in `getTypeInfoImpl`.
`RequireCompleteType` now checks the size of a constant array once it is
complete, using the same limit as `BuildArrayType`. The error reports
bytes (new `err_array_size_too_large`) since the element count was
already accepted. Uninitialized variable definitions now require the
whole array type to be complete rather than just the base element type,
as initialized ones already do.
LLM tools were used for this contribution. I've reviewed, built, and
tested the change myself before pushing to GitHub.
Added:
clang/test/SemaCXX/array-size.cpp
Modified:
clang/docs/ReleaseNotes.md
clang/include/clang/Basic/DiagnosticSemaKinds.td
clang/include/clang/Sema/Sema.h
clang/lib/Sema/SemaDecl.cpp
clang/lib/Sema/SemaType.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 56077adbf46dc..b0fce4ab04f4d 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1045,6 +1045,12 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
by a using-declaration (e.g. `using Base::operator auto;`). Such a conversion
function is now also diagnosed if it is deleted. (#GH189146)
+- Fixed an assertion failure when an array whose element type was still
+ incomplete when the array type was formed (for example, an array of a class
+ template specialization that is only instantiated later) turned out to be too
+ large once the element type was completed. Clang now diagnoses the oversized
+ array instead of asserting. (#GH213855)
+
- A workaround that was introduced to fix an issue with the `<format>` header present in some versions of
libstdc++15 has been extended to support preprocessed input. Previously, splitting the preprocessing and
compilation step would result in the fix not being applied. (#GH160314)
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 80de4627959c4..60e4c639be722 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6758,6 +6758,8 @@ def err_vm_func_decl : Error<
"function declaration cannot have variably modified type">;
def err_array_too_large : Error<
"array is too large (%0 elements)">;
+def err_array_size_too_large : Error<
+ "array is too large (%0 bytes)">;
def err_type_too_large_for_address_space : Error<
"%0 is too large for the address space (maximum allowed size of %1 bytes)">;
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 3abbaf6b1941e..34b8a74d1a0df 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -15797,7 +15797,8 @@ class Sema final : public SemaBase {
/// this routine then attempts to perform class template
/// instantiation. If instantiation fails, or if @p T is incomplete
/// and cannot be completed, issues the diagnostic @p diag (giving it
- /// the type @p T) and returns true.
+ /// the type @p T) and returns true. The same applies to an array type
+ /// that turns out to be too large once its element type is complete.
///
/// @param Loc The location in the source that the incomplete type
/// diagnostic should refer to.
diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index d4fbf04ee284a..b30880ce6651f 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14925,6 +14925,13 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl) {
Var->setInvalidDecl();
return;
}
+ // Completing the element type may reveal that the array is too large.
+ if (Type->isConstantArrayType() &&
+ RequireCompleteType(Var->getLocation(), Type,
+ diag::err_typecheck_decl_incomplete_type)) {
+ Var->setInvalidDecl();
+ return;
+ }
} else {
return;
}
diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp
index e16630ce9cd84..81fd8d34e0eb8 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -9753,6 +9753,35 @@ static void assignInheritanceModel(Sema &S, CXXRecordDecl *RD) {
}
}
+/// If \p T is (or contains) a constant array type whose size in bytes cannot
+/// be represented within ConstantArrayType::getMaxSizeBits, return the
+/// offending array type. Nested arrays are checked from the inside out, since
+/// the size of an outer array can only be computed once its element's size is
+/// known to fit. BuildArrayType can only check the element count if the
+/// element type is still incomplete when the array type is formed, so the
+/// size has to be rechecked once the element type is completed.
+static const ConstantArrayType *
+getOversizedConstantArray(const ASTContext &Context, QualType T) {
+ if (T->isDependentType() || T->isVariablyModifiedType() ||
+ T->isUndeducedType())
+ return nullptr;
+
+ const ConstantArrayType *CAT = Context.getAsConstantArrayType(T);
+ if (!CAT)
+ return nullptr;
+
+ QualType ElementType = CAT->getElementType();
+ if (const ConstantArrayType *Inner =
+ getOversizedConstantArray(Context, ElementType))
+ return Inner;
+
+ if (ConstantArrayType::getNumAddressingBits(Context, ElementType,
+ CAT->getSize()) >
+ ConstantArrayType::getMaxSizeBits(Context))
+ return CAT;
+ return nullptr;
+}
+
bool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
CompleteTypeKind Kind,
TypeDiagnoser *Diagnoser) {
@@ -9804,6 +9833,19 @@ bool Sema::RequireCompleteTypeImpl(SourceLocation Loc, QualType T,
/*Recover*/ TreatAsComplete);
return !TreatAsComplete;
}
+
+ // The element type may have been incomplete when the array type was
+ // formed, in which case BuildArrayType could not check the array's size.
+ if (const ConstantArrayType *CAT = getOversizedConstantArray(Context, T)) {
+ if (Diagnoser) {
+ CharUnits ElementSize =
+ Context.getTypeSizeInChars(CAT->getElementType());
+ llvm::APInt SizeInBytes = CAT->getSize().zext(128) *
+ llvm::APInt(128, ElementSize.getQuantity());
+ Diag(Loc, diag::err_array_size_too_large) << SizeInBytes;
+ }
+ return true;
+ }
return false;
}
diff --git a/clang/test/SemaCXX/array-size.cpp b/clang/test/SemaCXX/array-size.cpp
new file mode 100644
index 0000000000000..02ec4af7d4b07
--- /dev/null
+++ b/clang/test/SemaCXX/array-size.cpp
@@ -0,0 +1,55 @@
+// RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-linux-gnu %s
+
+// An array whose element type is incomplete when the array type is formed can
+// only have its size checked once the element type is completed (#GH213855).
+
+namespace GH213855 {
+template <unsigned Size> struct S : public CBdVfsImpl { // expected-error {{expected class name}}
+ double A[Size];
+};
+template <unsigned Size> struct SS {
+ S<Size> A[Size]; // expected-error {{array is too large (147'573'944'137'180'895'432 bytes)}}
+void foo() { SS<-123> ss; } // expected-error {{non-type template argument evaluates to -123, which cannot be narrowed to type 'unsigned int'}} \
+ // expected-note {{in instantiation of template class 'GH213855::SS<4294967173>' requested here}}
+};
+} // namespace GH213855
+
+namespace array_variable {
+template <unsigned Size> struct S { double A[Size]; };
+S<4294967173u> arr[4294967173u]; // expected-error {{array is too large (147'573'944'137'180'895'432 bytes)}}
+} // namespace array_variable
+
+namespace dependent_context {
+template <unsigned Size> struct S { double A[Size]; };
+
+// Diagnosed once, at the template definition, not once per instantiation.
+template <typename T> struct Parent {
+ S<4294967173u> A[4294967173u]; // expected-error {{array is too large (147'573'944'137'180'895'432 bytes)}}
+};
+Parent<int> p1;
+Parent<long> p2;
+
+template <typename T> struct Parent2 {
+ double A[2305843009213693952u]; // expected-error {{array is too large (2'305'843'009'213'693'952 elements)}}
+};
+Parent2<int> p3;
+Parent2<long> p4;
+
+template <typename T> struct ParentOK {
+ double A[4294967173u]; // ~2^35 bytes, still representable.
+};
+ParentOK<int> ok;
+} // namespace dependent_context
+
+namespace incomplete_element_type {
+struct Incomplete;
+extern Incomplete ok[2];
+extern Incomplete arr[4294967173];
+extern Incomplete arr2[2][4294967173];
+struct Incomplete { double A[4294967173]; };
+Incomplete arr3[4294967173]; // expected-error {{array is too large (4'294'967'173 elements)}}
+
+unsigned long n0 = sizeof(ok);
+unsigned long n1 = sizeof(arr); // expected-error {{array is too large (147'573'944'137'180'895'432 bytes)}}
+unsigned long n2 = sizeof(arr2); // expected-error {{array is too large (147'573'944'137'180'895'432 bytes)}}
+} // namespace incomplete_element_type
More information about the cfe-commits
mailing list