[clang] cda4a0e - [Sema] Relax a failing assertion in TransformBlockExpr

Akira Hatanaka via cfe-commits cfe-commits at lists.llvm.org
Thu Feb 16 18:40:39 PST 2023


Author: Akira Hatanaka
Date: 2023-02-16T18:40:26-08:00
New Revision: cda4a0e918b591be9ae0732f7df07678efb75047

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

LOG: [Sema] Relax a failing assertion in TransformBlockExpr

The assertion fails when the expression causing the this pointer to be
captured is part of a constexpr if statement's branch and the branch
gets discarded when the enclosing method is instantiated.

Note that the test case is added to CodeGen instead of Sema since the
translation unit has to be free of errors in order for the assertion to
be checked.

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

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Sema/TreeTransform.h
    clang/test/CodeGenCXX/cxx1z-constexpr-if.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4f2007fcf5715..70a91bab0cb21 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -141,6 +141,9 @@ Bug Fixes in This Version
   driver mode and emit an error which suggests using ``/TC`` or ``/TP``
   ``clang-cl`` options instead.
   (`#59307 <https://github.com/llvm/llvm-project/issues/59307>`_)
+- Fix assert that fails when the expression causing the this pointer to be
+  captured by a block is part of a constexpr if statement's branch and
+  instantiation of the enclosing method causes the branch to be discarded.
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

diff  --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 25b3034a5efac..cca0383568201 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -14598,7 +14598,12 @@ TreeTransform<Derived>::TransformBlockExpr(BlockExpr *E) {
                                                  oldCapture));
       assert(blockScope->CaptureMap.count(newCapture));
     }
-    assert(oldBlock->capturesCXXThis() == blockScope->isCXXThisCaptured());
+
+    // The this pointer may not be captured by the instantiated block, even when
+    // it's captured by the original block, if the expression causing the
+    // capture is in the discarded branch of a constexpr if statement.
+    assert((!blockScope->isCXXThisCaptured() || oldBlock->capturesCXXThis()) &&
+           "this pointer isn't captured in the old block");
   }
 #endif
 

diff  --git a/clang/test/CodeGenCXX/cxx1z-constexpr-if.cpp b/clang/test/CodeGenCXX/cxx1z-constexpr-if.cpp
index 14695363b331c..d14e36406a45e 100644
--- a/clang/test/CodeGenCXX/cxx1z-constexpr-if.cpp
+++ b/clang/test/CodeGenCXX/cxx1z-constexpr-if.cpp
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -std=c++1z %s -emit-llvm -o - | FileCheck %s --implicit-check-not=should_not_be_used
+// RUN: %clang_cc1 -std=c++1z %s -emit-llvm -fblocks -triple x86_64-apple-darwin10 -o - | FileCheck %s --implicit-check-not=should_not_be_used
 
 void should_be_used_1();
 void should_be_used_2();
@@ -32,3 +32,20 @@ foo: should_not_be_used();
 // CHECK: should_be_used_1
 // CHECK: should_be_used_2
 // CHECK: should_be_used_3
+
+namespace BlockThisCapture {
+  void foo();
+  struct S {
+    template <bool b>
+    void m() {
+      ^{ if constexpr(b) (void)this; else foo();  }();
+    }
+  };
+
+  void test() {
+    S().m<false>();
+  }
+}
+
+// CHECK-LABEL: define internal void @___ZN16BlockThisCapture1S1mILb0EEEvv_block_invoke(
+// CHECK: call void @_ZN16BlockThisCapture3fooEv(


        


More information about the cfe-commits mailing list