Author: Nikita Popov Date: 2026-11-07T09:46:57+01:00 New Revision: 5730c9469dc224e64e1b5db4b60f3aacb774680e
URL: https://github.com/llvm/llvm-project/commit/5730c9469dc224e64e1b5db4b60f3aacb774680e DIFF: https://github.com/llvm/llvm-project/commit/5730c9469dc224e64e1b5db4b60f3aacb774680e.diff LOG: Revert "[clang][Sema] Recheck array size once the element type is complete (#…" This reverts commit dc550beb73ca66333785b235c79dc8947088555a. Added: 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: clang/test/SemaCXX/array-size.cpp ################################################################################ diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index b0fce4ab04f4d..56077adbf46dc 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 60e4c639be722..80de4627959c4 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 34b8a74d1a0df..3abbaf6b1941e 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 b30880ce6651f..d4fbf04ee284a 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 81fd8d34e0eb8..e16630ce9cd84 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 02ec4af7d4b07..0000000000000 --- 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 _______________________________________________ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
