[clang] Revert "[clang][Sema] Recheck array size once the element type is complete" (PR #239935)
Nikita Popov via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 7 00:47:35 PST 2026
https://github.com/nikic created https://github.com/llvm/llvm-project/pull/239935
Reverts llvm/llvm-project#219817
Causes a large compile-time regression.
>From 5730c9469dc224e64e1b5db4b60f3aacb774680e Mon Sep 17 00:00:00 2001
From: Nikita Popov <github at npopov.com>
Date: Sat, 7 Nov 2026 09:46:57 +0100
Subject: [PATCH] =?UTF-8?q?Revert=20"[clang][Sema]=20Recheck=20array=20siz?=
=?UTF-8?q?e=20once=20the=20element=20type=20is=20complete=20(#=E2=80=A6"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
This reverts commit dc550beb73ca66333785b235c79dc8947088555a.
---
clang/docs/ReleaseNotes.md | 6 --
.../clang/Basic/DiagnosticSemaKinds.td | 2 -
clang/include/clang/Sema/Sema.h | 3 +-
clang/lib/Sema/SemaDecl.cpp | 7 ---
clang/lib/Sema/SemaType.cpp | 42 --------------
clang/test/SemaCXX/array-size.cpp | 55 -------------------
6 files changed, 1 insertion(+), 114 deletions(-)
delete mode 100644 clang/test/SemaCXX/array-size.cpp
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index b0fce4ab04f4db..56077adbf46dc7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -1045,12 +1045,6 @@ 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 60e4c639be7222..80de4627959c4b 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td
@@ -6758,8 +6758,6 @@ 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 34b8a74d1a0dfb..3abbaf6b1941ec 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -15797,8 +15797,7 @@ 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 same applies to an array type
- /// that turns out to be too large once its element type is complete.
+ /// the type @p T) and returns true.
///
/// @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 b30880ce6651fc..d4fbf04ee284af 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/lib/Sema/SemaDecl.cpp
@@ -14925,13 +14925,6 @@ 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 81fd8d34e0eb8a..e16630ce9cd844 100644
--- a/clang/lib/Sema/SemaType.cpp
+++ b/clang/lib/Sema/SemaType.cpp
@@ -9753,35 +9753,6 @@ 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) {
@@ -9833,19 +9804,6 @@ 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
deleted file mode 100644
index 02ec4af7d4b07e..00000000000000
--- a/clang/test/SemaCXX/array-size.cpp
+++ /dev/null
@@ -1,55 +0,0 @@
-// 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