diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -381,6 +381,8 @@ - Fix overly aggressive lifetime checks for parenthesized aggregate initialization. (`#61567 `_) +- Fix handling of constexpr dynamic memory allocations in template + arguments. (`#62462 `_) Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -15354,8 +15354,15 @@ LValue LVal; LVal.set(Base); - if (!::EvaluateInPlace(Result.Val, Info, LVal, this) || Result.HasSideEffects) - return false; + { + // C++23 [intro.execution]/p5 + // A full-expression is [...] a constant-expression + // So we need to make sure temporary objects are destroyed after having evaluating + // the expression (per C++23 [class.temporary]/p4). + FullExpressionRAII Scope(Info); + if (!::EvaluateInPlace(Result.Val, Info, LVal, this) || Result.HasSideEffects || !Scope.destroy()) + return false; + } if (!Info.discardCleanups()) llvm_unreachable("Unhandled cleanup; missing full expression marker?"); diff --git a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp --- a/clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp +++ b/clang/test/SemaCXX/cxx2a-constexpr-dynalloc.cpp @@ -215,3 +215,27 @@ } static_assert(h()); } + +namespace GH62462 { + +class string { +public: + char *mem; + constexpr string() { + this->mem = new char(1); + } + constexpr ~string() { + delete this->mem; + } + constexpr unsigned size() const { return 4; } +}; + + +template +void test() {}; + +void f() { + test(); +} + +}