[clang] [clang][bytecode] Add missing condition scope to CXXForRangeStmt (PR #221520)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Sat Sep 5 21:14:33 PDT 2026


https://github.com/tbaederr created https://github.com/llvm/llvm-project/pull/221520

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

>From 0cd81dd0221b8efdc1c8825452b971c1870c0f44 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <tbaeder at redhat.com>
Date: Sun, 6 Sep 2026 06:11:44 +0200
Subject: [PATCH] [clang][bytecode] Add missing condition scope to
 CXXForRangeStmt

Fixes https://github.com/llvm/llvm-project/issues/221401
---
 clang/lib/AST/ByteCode/Compiler.cpp          |  3 +++
 clang/test/AST/ByteCode/initializer_list.cpp | 27 +++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 03f9478a6e4e0..4572c3a8307d6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6940,6 +6940,7 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
   if (!this->visitStmt(EndStmt))
     return false;
 
+  LocalScope<Emitter> CondScope(this);
   // Now the condition as well as the loop variable assignment.
   this->fallthrough(CondLabel);
   this->emitLabel(CondLabel);
@@ -6962,6 +6963,8 @@ bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
       return false;
   }
 
+  if (!CondScope.destroyLocals())
+    return false;
   if (!this->jump(CondLabel, S))
     return false;
 
diff --git a/clang/test/AST/ByteCode/initializer_list.cpp b/clang/test/AST/ByteCode/initializer_list.cpp
index f882e4ff1b124..b3adefc580cc3 100644
--- a/clang/test/AST/ByteCode/initializer_list.cpp
+++ b/clang/test/AST/ByteCode/initializer_list.cpp
@@ -1,5 +1,5 @@
 // RUN: %clang_cc1 -fexperimental-new-constant-interpreter -fms-extensions -std=c++20 -verify=expected,both %s
-// RUN: %clang_cc1 -std=c++20 -fms-extensions -verify=ref,both %s
+// RUN: %clang_cc1                                         -fms-extensions -std=c++20 -verify=ref,both      %s
 
 namespace std {
   typedef decltype(sizeof(int)) size_t;
@@ -68,4 +68,29 @@ namespace rdar13395022 {
   }
 }
 
+namespace CopiedForRangeIterator {
+  struct holder {
+    int *p = nullptr;
+    constexpr holder() {
+      p = new int;
+    }
+    constexpr holder(holder&&) {
+      delete p;
+      p = new int;
+    }
+    constexpr holder(const holder&) {
+      delete p;
+      p = new int;
+    }
+    constexpr ~holder() {
+      delete p;
+    }
+  };
 
+  constexpr int copies() {
+    for (holder h : {holder(), holder()}) {
+    }
+    return 0;
+  }
+  static_assert(copies() == 0);
+}



More information about the cfe-commits mailing list