https://github.com/efriedma-quic updated https://github.com/llvm/llvm-project/pull/182203
>From f602ea3c241cee00eb194d80bf0df1e95fb65ece Mon Sep 17 00:00:00 2001 From: Eli Friedman <[email protected]> Date: Wed, 18 Feb 2026 17:02:05 -0800 Subject: [PATCH 1/4] [clang] Fix filler handling for new expression with unspecified bound. Array new with a runtime bound requires an "array filler" to initialize elements which aren't explicitly initialized. Array new with an unspecified bound doesn't need this; the array length is a compile-time constant. Make sure SemaInit doesn't confuse the two cases. Fixes #81157. --- clang/include/clang/Sema/Initialization.h | 16 +++++++++++----- clang/lib/Sema/SemaExprCXX.cpp | 10 ++++++---- clang/test/SemaCXX/new-delete.cpp | 16 ++++++++++++++++ 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index 0e2891f1ff5c2..cd729aa16dc28 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -160,6 +160,10 @@ class alignas(8) InitializedEntity { /// Whether the entity being initialized may end up using the /// named return value optimization (NRVO). bool NRVO; + + /// When Kind == EK_New, whether this is initializing an array of runtime + /// size (which needs an array filler). + bool VariableLengthArrayNew; }; struct VD { @@ -227,11 +231,12 @@ class alignas(8) InitializedEntity { /// function, throwing an object, performing an explicit cast, or /// initializing a parameter for which there is no declaration. InitializedEntity(EntityKind Kind, SourceLocation Loc, QualType Type, - bool NRVO = false) + bool NRVO = false, bool VariableLengthArrayNew = false) : Kind(Kind), Type(Type) { new (&LocAndNRVO) LN; LocAndNRVO.Location = Loc; LocAndNRVO.NRVO = NRVO; + LocAndNRVO.VariableLengthArrayNew = VariableLengthArrayNew; } /// Create the initialization entity for a member subobject. @@ -335,8 +340,10 @@ class alignas(8) InitializedEntity { } /// Create the initialization entity for an object allocated via new. - static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type) { - return InitializedEntity(EK_New, NewLoc, Type); + static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type, + bool VariableLengthArrayNew) { + return InitializedEntity(EK_New, NewLoc, Type, /*NRVO*/ false, + VariableLengthArrayNew); } /// Create the initialization entity for a temporary. @@ -506,8 +513,7 @@ class alignas(8) InitializedEntity { /// Determine whether this is an array new with an unknown bound. bool isVariableLengthArrayNew() const { - return getKind() == EK_New && isa_and_nonnull<IncompleteArrayType>( - getType()->getAsArrayTypeUnsafe()); + return getKind() == EK_New && LocAndNRVO.VariableLengthArrayNew; } /// Is this the implicit initialization of a member of a class from diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 91967a7a9ff97..a1e95dbe676de 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2206,8 +2206,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, << /*array*/ 2 << (*ArraySize ? (*ArraySize)->getSourceRange() : TypeRange)); - InitializedEntity Entity - = InitializedEntity::InitializeNew(StartLoc, AllocType); + InitializedEntity Entity = + InitializedEntity::InitializeNew(StartLoc, AllocType, + /*VariableLengthArrayNew*/ false); AllocType = DeduceTemplateSpecializationFromInitializer( AllocTypeInfo, Entity, Kind, Exprs); if (AllocType.isNull()) @@ -2589,8 +2590,9 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, else InitType = AllocType; - InitializedEntity Entity - = InitializedEntity::InitializeNew(StartLoc, InitType); + bool VariableLengthArrayNew = ArraySize && *ArraySize && !KnownArraySize; + InitializedEntity Entity = InitializedEntity::InitializeNew( + StartLoc, InitType, VariableLengthArrayNew); InitializationSequence InitSeq(*this, Entity, Kind, Exprs); ExprResult FullInit = InitSeq.Perform(*this, Entity, Kind, Exprs); if (FullInit.isInvalid()) diff --git a/clang/test/SemaCXX/new-delete.cpp b/clang/test/SemaCXX/new-delete.cpp index 1adb993f46c8f..cda49a02098b0 100644 --- a/clang/test/SemaCXX/new-delete.cpp +++ b/clang/test/SemaCXX/new-delete.cpp @@ -725,3 +725,19 @@ int (*const_fold)[12] = new int[3][&const_fold + 12 - &const_fold]; // expected-error@-5 {{cannot allocate object of variably modified type}} // expected-warning@-6 {{variable length arrays in C++ are a Clang extension}} #endif + +#if __cplusplus >= 201103L +namespace PR81157 { + struct C { + C(int); + }; + int f(int n) { + C *ptr1{new C[]{1L}}; + C *ptr2{new C[n]{1L}}; + // expected-error@-1 {{no matching constructor}} + // expected-note@-6 {{candidate constructor}} + // expected-note@-8 2 {{candidate constructor}} + // expected-note@-4 {{in implicit initialization of trailing array elements in runtime-sized array new}} + } +} +#endif >From 542229eef1620112fd2530f775423e3c914b835b Mon Sep 17 00:00:00 2001 From: Eli Friedman <[email protected]> Date: Thu, 19 Feb 2026 10:03:10 -0800 Subject: [PATCH 2/4] Code format review comment. --- clang/include/clang/Sema/Initialization.h | 2 +- clang/lib/Sema/SemaExprCXX.cpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index cd729aa16dc28..a7df59414406a 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -342,7 +342,7 @@ class alignas(8) InitializedEntity { /// Create the initialization entity for an object allocated via new. static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type, bool VariableLengthArrayNew) { - return InitializedEntity(EK_New, NewLoc, Type, /*NRVO*/ false, + return InitializedEntity(EK_New, NewLoc, Type, /*NRVO=*/ false, VariableLengthArrayNew); } diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index a1e95dbe676de..35419cfdabe92 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -1,3 +1,4 @@ +// //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. @@ -2208,7 +2209,7 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, InitializedEntity Entity = InitializedEntity::InitializeNew(StartLoc, AllocType, - /*VariableLengthArrayNew*/ false); + /*VariableLengthArrayNew=*/ false); AllocType = DeduceTemplateSpecializationFromInitializer( AllocTypeInfo, Entity, Kind, Exprs); if (AllocType.isNull()) >From bcf9b7af47d8c5c6695db5558167d5662fbf4181 Mon Sep 17 00:00:00 2001 From: Eli Friedman <[email protected]> Date: Thu, 19 Feb 2026 10:03:59 -0800 Subject: [PATCH 3/4] Stray newline --- clang/lib/Sema/SemaExprCXX.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 35419cfdabe92..18c359062649b 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -1,4 +1,3 @@ -// //===--- SemaExprCXX.cpp - Semantic Analysis for Expressions --------------===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. >From 8ff7ac86e48957a3e34c3a5c01aa111a46aa45ad Mon Sep 17 00:00:00 2001 From: Eli Friedman <[email protected]> Date: Thu, 19 Feb 2026 10:20:11 -0800 Subject: [PATCH 4/4] Fix formatting --- clang/include/clang/Sema/Initialization.h | 2 +- clang/lib/Sema/SemaExprCXX.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/clang/include/clang/Sema/Initialization.h b/clang/include/clang/Sema/Initialization.h index a7df59414406a..1978ac41c58a4 100644 --- a/clang/include/clang/Sema/Initialization.h +++ b/clang/include/clang/Sema/Initialization.h @@ -342,7 +342,7 @@ class alignas(8) InitializedEntity { /// Create the initialization entity for an object allocated via new. static InitializedEntity InitializeNew(SourceLocation NewLoc, QualType Type, bool VariableLengthArrayNew) { - return InitializedEntity(EK_New, NewLoc, Type, /*NRVO=*/ false, + return InitializedEntity(EK_New, NewLoc, Type, /*NRVO=*/false, VariableLengthArrayNew); } diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index 18c359062649b..78d442fdda87b 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -2208,7 +2208,7 @@ ExprResult Sema::BuildCXXNew(SourceRange Range, bool UseGlobal, InitializedEntity Entity = InitializedEntity::InitializeNew(StartLoc, AllocType, - /*VariableLengthArrayNew=*/ false); + /*VariableLengthArrayNew=*/false); AllocType = DeduceTemplateSpecializationFromInitializer( AllocTypeInfo, Entity, Kind, Exprs); if (AllocType.isNull()) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
