[clang] [clang][Interp] Fix `ArrayInitLoopExpr` handling (PR #67886)

via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 30 08:10:51 PDT 2023


https://github.com/isuckatcs created https://github.com/llvm/llvm-project/pull/67886

This patch implements the changes from #67722 in Interp. 

>From df530e60872abc4a74e561b8479c4a28dfb39d85 Mon Sep 17 00:00:00 2001
From: isuckatcs <65320245+isuckatcs at users.noreply.github.com>
Date: Sat, 30 Sep 2023 17:05:02 +0200
Subject: [PATCH] implement fix in interp

---
 clang/lib/AST/Interp/ByteCodeExprGen.cpp |  1 +
 clang/test/AST/Interp/cxx20.cpp          | 10 ++++------
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/clang/lib/AST/Interp/ByteCodeExprGen.cpp b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
index 46906377863bd74..1e2db3219865a53 100644
--- a/clang/lib/AST/Interp/ByteCodeExprGen.cpp
+++ b/clang/lib/AST/Interp/ByteCodeExprGen.cpp
@@ -803,6 +803,7 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
   // where the LHS is on the stack (the target array)
   // and the RHS is our SubExpr.
   for (size_t I = 0; I != Size; ++I) {
+    BlockScope<Emitter> Scope(this);
     ArrayIndexScope<Emitter> IndexScope(this, I);
 
     if (ElemT) {
diff --git a/clang/test/AST/Interp/cxx20.cpp b/clang/test/AST/Interp/cxx20.cpp
index 197090b0a37d9df..5c0a88ce9612e1b 100644
--- a/clang/test/AST/Interp/cxx20.cpp
+++ b/clang/test/AST/Interp/cxx20.cpp
@@ -701,13 +701,12 @@ namespace ThreeWayCmp {
   static_assert(pa2 <=> pa1 == 1, "");
 }
 
-// FIXME: Interp should also be able to evaluate this snippet properly.
 namespace ConstexprArrayInitLoopExprDestructors
 {
   struct Highlander {
       int *p = 0;
       constexpr Highlander() {}
-      constexpr void set(int *p) { this->p = p; ++*p; if (*p != 1) throw "there can be only one"; } // expected-note {{not valid in a constant expression}}
+      constexpr void set(int *p) { this->p = p; ++*p; if (*p != 1) throw "there can be only one"; }
       constexpr ~Highlander() { --*p; }
   };
 
@@ -715,19 +714,18 @@ namespace ConstexprArrayInitLoopExprDestructors
       int *p;
       constexpr X(int *p) : p(p) {}
       constexpr X(const X &x, Highlander &&h = Highlander()) : p(x.p) {
-          h.set(p); // expected-note {{in call to '&Highlander()->set(&n)'}}
+          h.set(p);
       }
   };
 
   constexpr int f() {
       int n = 0;
       X x[3] = {&n, &n, &n};
-      auto [a, b, c] = x; // expected-note {{in call to 'X(x[0], Highlander())'}}
+      auto [a, b, c] = x;
       return n;
   }
 
-  static_assert(f() == 0); // expected-error {{not an integral constant expression}} \
-                           // expected-note {{in call to 'f()'}}
+  static_assert(f() == 0);
 
   int main() {
       return f();



More information about the cfe-commits mailing list