[clang] 83c90e6 - [clang] Fix consteval initializers of temporaries

Mariya Podchishchaeva via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 28 04:42:18 PDT 2023


Author: Mariya Podchishchaeva
Date: 2023-03-28T07:41:53-04:00
New Revision: 83c90e623a308a6313783f320eeaabc7e3f9f4b2

URL: https://github.com/llvm/llvm-project/commit/83c90e623a308a6313783f320eeaabc7e3f9f4b2
DIFF: https://github.com/llvm/llvm-project/commit/83c90e623a308a6313783f320eeaabc7e3f9f4b2.diff

LOG: [clang] Fix consteval initializers of temporaries

When a potential immediate invocation is met, it is immediately wrapped by a
`ConstantExpr`. There is also a TreeTransform that removes this `ConstantExpr`
wrapper when corresponding expression evaluation context is popped.
So, in case initializer was an immediate invocation, `CXXTemporaryObjectExpr`
was wrapped by a `ConstantExpr`, and that caused additional unnecessary
`CXXFunctionalCastExpr` to be added, which later confused the TreeTransform
that rebuilds immediate invocations, so it was adding unnecessary
constructor call.

Fixes https://github.com/llvm/llvm-project/issues/60286

Reviewed By: aaron.ballman

Differential Revision: https://reviews.llvm.org/D146801

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/SemaExprCXX.cpp
    clang/test/SemaCXX/cxx2a-consteval.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 295532a9bfeca..882e5069e4539 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -238,6 +238,9 @@ Bug Fixes in This Version
   the initializer, so it behaves consistently with other ``VarDecls`` and ends
   on the last token of initializer, instead of right angle bracket of
   the template argument list.
+- Fix false-positive diagnostic issued for consteval initializers of temporary
+  objects.
+  (`#60286 <https://github.com/llvm/llvm-project/issues/60286>`_)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index e7f3852ae34cd..d5aeb38245ad7 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -1590,6 +1590,9 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
   Expr *Inner = Result.get();
   if (CXXBindTemporaryExpr *BTE = dyn_cast_or_null<CXXBindTemporaryExpr>(Inner))
     Inner = BTE->getSubExpr();
+  if (auto *CE = dyn_cast<ConstantExpr>(Inner);
+      CE && CE->isImmediateInvocation())
+    Inner = CE->getSubExpr();
   if (!isa<CXXTemporaryObjectExpr>(Inner) &&
       !isa<CXXScalarValueInitExpr>(Inner)) {
     // If we created a CXXTemporaryObjectExpr, that node also represents the

diff  --git a/clang/test/SemaCXX/cxx2a-consteval.cpp b/clang/test/SemaCXX/cxx2a-consteval.cpp
index 3047c3f51b5ec..0ffa6bc3b404e 100644
--- a/clang/test/SemaCXX/cxx2a-consteval.cpp
+++ b/clang/test/SemaCXX/cxx2a-consteval.cpp
@@ -1050,3 +1050,18 @@ void test() {
 
 }
 }
+
+namespace GH60286 {
+
+struct A {
+  int i = 0;
+
+  consteval A() {}
+  A(const A&) { i = 1; }
+  consteval int f() { return i; }
+};
+
+constexpr auto B = A{A{}}.f();
+static_assert(B == 0);
+
+}


        


More information about the cfe-commits mailing list