https://github.com/Mr-Anyone updated https://github.com/llvm/llvm-project/pull/140278
>From 3945cae10c7f2629e7e875db29bd9aa4fa74055c Mon Sep 17 00:00:00 2001 From: Vincent <l...@viceroygroup.ca> Date: Wed, 14 May 2025 16:36:35 -0400 Subject: [PATCH 1/4] [clang] Fixed Constant Evaluation don't Call Destructor Within the condition statement of the for block, the destructor doesn't get called during compile time evaluated constants. resolves #139818 --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/AST/ExprConstant.cpp | 6 +++- clang/test/SemaCXX/static-assert-cxx26.cpp | 35 ++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 648b32c659b4f..4410810ba8145 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -629,6 +629,7 @@ Bug Fixes in This Version base classes. (GH139452) - Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130) - Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701) +- Fix constant evaluation in for loop not calling destructor (#GH139818) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index e9a269337404a..0bfb65d18ea9c 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -5756,8 +5756,12 @@ static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info, if (FS->getCond() && !EvaluateCond(Info, FS->getConditionVariable(), FS->getCond(), Continue)) return ESR_Failed; - if (!Continue) + + if (!Continue) { + if (!IterScope.destroy()) + return ESR_Failed; break; + } EvalStmtResult ESR = EvaluateLoopBody(Result, Info, FS->getBody()); if (ESR != ESR_Continue) { diff --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp index b53c67ee67932..7a47f2784ceb3 100644 --- a/clang/test/SemaCXX/static-assert-cxx26.cpp +++ b/clang/test/SemaCXX/static-assert-cxx26.cpp @@ -416,3 +416,38 @@ static_assert( // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} ); } + +// taken from: https://github.com/llvm/llvm-project/issues/139818 +namespace GH139818{ + struct A { + constexpr ~A() { ref = false; } + constexpr operator bool() { + return b; + } + bool b; + bool& ref; + }; + + constexpr bool f1() { + bool ret = true; + for (bool b = false; A x{b, ret}; b = true) {} + return ret; + } + + static_assert(!f1()); + + struct Y { + constexpr ~Y() noexcept(false) { throw "oops"; } // expected-error {{cannot use 'throw' with exceptions disabled}} + // expected-note@-1 {{subexpression not valid in a constant expression}} + constexpr operator bool() { + return b; + } + bool b; + }; + constexpr bool f2() { + for (bool b = false; Y x = {b}; b = true) {} // expected-note {{in call to 'x.~Y()'}} + return true; + } + static_assert(f2()); // expected-error {{static assertion expression is not an integral constant expression}} + // expected-note@-1 {{in call to 'f2()'}} +}; >From e3a586872f2f320273506cf97a2254c4b07a65c6 Mon Sep 17 00:00:00 2001 From: Vincent <vincent0...@outlook.com> Date: Tue, 20 May 2025 16:24:55 -0400 Subject: [PATCH 2/4] Update clang/docs/ReleaseNotes.rst Co-authored-by: Sirraide <aeternalm...@gmail.com> --- clang/docs/ReleaseNotes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 4410810ba8145..ac8a62fe4e548 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -629,7 +629,8 @@ Bug Fixes in This Version base classes. (GH139452) - Fixed an assertion failure in serialization of constexpr structs containing unions. (#GH140130) - Fixed duplicate entries in TableGen that caused the wrong attribute to be selected. (GH#140701) -- Fix constant evaluation in for loop not calling destructor (#GH139818) +- Constant evaluation now correctly runs the destructor of a variable declared in + the second clause of a C-style ``for`` loop. (#GH139818) Bug Fixes to Compiler Builtins ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ >From 78a2091479dc44100f50bce9903c3f6cc83632e9 Mon Sep 17 00:00:00 2001 From: Vincent <l...@viceroygroup.ca> Date: Tue, 20 May 2025 16:40:52 -0400 Subject: [PATCH 3/4] Moved Test to `cxx2a-consteval.cpp` --- clang/test/SemaCXX/cxx2a-consteval.cpp | 33 ++++++++++++++++++++ clang/test/SemaCXX/static-assert-cxx26.cpp | 35 ---------------------- 2 files changed, 33 insertions(+), 35 deletions(-) diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp index d9932e4dd8241..3df503fa1277f 100644 --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -1321,4 +1321,37 @@ namespace GH139160{ // expected-note@-2 {{non-constexpr function 'make_struct' cannot be used in a constant expression}} }; +// taken from: https://github.com/llvm/llvm-project/issues/139818 +namespace GH139818{ + struct A { + constexpr ~A() { ref = false; } + constexpr operator bool() { + return b; + } + bool b; + bool& ref; + }; + constexpr bool f1() { + bool ret = true; + for (bool b = false; A x{b, ret}; b = true) {} + return ret; + } + + static_assert(!f1()); + + struct Y { + constexpr ~Y() noexcept(false) { throw "oops"; } // expected-error {{cannot use 'throw' with exceptions disabled}} + // expected-note@-1 {{subexpression not valid in a constant expression}} + constexpr operator bool() { + return b; + } + bool b; + }; + constexpr bool f2() { + for (bool b = false; Y x = {b}; b = true) {} // expected-note {{in call to 'x.~Y()'}} + return true; + } + static_assert(f2()); // expected-error {{static assertion expression is not an integral constant expression}} + // expected-note@-1 {{in call to 'f2()'}} +}; diff --git a/clang/test/SemaCXX/static-assert-cxx26.cpp b/clang/test/SemaCXX/static-assert-cxx26.cpp index 7a47f2784ceb3..b53c67ee67932 100644 --- a/clang/test/SemaCXX/static-assert-cxx26.cpp +++ b/clang/test/SemaCXX/static-assert-cxx26.cpp @@ -416,38 +416,3 @@ static_assert( // expected-note@-1 {{read of dereferenced one-past-the-end pointer is not allowed in a constant expression}} ); } - -// taken from: https://github.com/llvm/llvm-project/issues/139818 -namespace GH139818{ - struct A { - constexpr ~A() { ref = false; } - constexpr operator bool() { - return b; - } - bool b; - bool& ref; - }; - - constexpr bool f1() { - bool ret = true; - for (bool b = false; A x{b, ret}; b = true) {} - return ret; - } - - static_assert(!f1()); - - struct Y { - constexpr ~Y() noexcept(false) { throw "oops"; } // expected-error {{cannot use 'throw' with exceptions disabled}} - // expected-note@-1 {{subexpression not valid in a constant expression}} - constexpr operator bool() { - return b; - } - bool b; - }; - constexpr bool f2() { - for (bool b = false; Y x = {b}; b = true) {} // expected-note {{in call to 'x.~Y()'}} - return true; - } - static_assert(f2()); // expected-error {{static assertion expression is not an integral constant expression}} - // expected-note@-1 {{in call to 'f2()'}} -}; >From a880c600b8a5a0ec2a8759d1544cd1ef846ee60e Mon Sep 17 00:00:00 2001 From: Vincent <l...@viceroygroup.ca> Date: Tue, 20 May 2025 17:12:22 -0400 Subject: [PATCH 4/4] update error messages --- clang/test/SemaCXX/cxx2a-consteval.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp index 3df503fa1277f..bf71894caa339 100644 --- a/clang/test/SemaCXX/cxx2a-consteval.cpp +++ b/clang/test/SemaCXX/cxx2a-consteval.cpp @@ -1342,7 +1342,9 @@ namespace GH139818{ struct Y { constexpr ~Y() noexcept(false) { throw "oops"; } // expected-error {{cannot use 'throw' with exceptions disabled}} - // expected-note@-1 {{subexpression not valid in a constant expression}} + // expected-error@-1 {{constexpr function never produces a constant expression}} + // expected-note@-2 {{subexpression not valid in a constant expression}} + // expected-note@-3 {{subexpression not valid in a constant expression}} constexpr operator bool() { return b; } _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits