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

We need to fallthrough here in case we're not jumping to the labels. This is 
only needed in expression contexts.

>From a15db935e8b389dde90224f972d0010530586a2a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timm=20B=C3=A4der?= <[email protected]>
Date: Tue, 18 Nov 2025 06:55:43 +0100
Subject: [PATCH] [clang][bytecode] Fix fallthrough to switch labels

We need to fallthrough here in case we're not jumping to the labels.
This is only needed in expression contexts.
---
 clang/lib/AST/ByteCode/Compiler.cpp  |  2 ++
 clang/test/AST/ByteCode/literals.cpp | 11 +++++++++++
 2 files changed, 13 insertions(+)

diff --git a/clang/lib/AST/ByteCode/Compiler.cpp 
b/clang/lib/AST/ByteCode/Compiler.cpp
index f8bbfed8bb387..8779ffab13b86 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -6057,6 +6057,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt 
*S) {
                           DefaultLabel);
   if (!this->visitStmt(S->getBody()))
     return false;
+  this->fallthrough(EndLabel);
   this->emitLabel(EndLabel);
 
   return LS.destroyLocals();
@@ -6064,6 +6065,7 @@ bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt 
*S) {
 
 template <class Emitter>
 bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) {
+  this->fallthrough(CaseLabels[S]);
   this->emitLabel(CaseLabels[S]);
   return this->visitStmt(S->getSubStmt());
 }
diff --git a/clang/test/AST/ByteCode/literals.cpp 
b/clang/test/AST/ByteCode/literals.cpp
index 5028ebfa3de30..c6d79f9c60058 100644
--- a/clang/test/AST/ByteCode/literals.cpp
+++ b/clang/test/AST/ByteCode/literals.cpp
@@ -1270,6 +1270,17 @@ namespace StmtExprs {
   namespace CrossFuncLabelDiff {
     constexpr long a(bool x) { return x ? 0 : (intptr_t)&&lbl + (0 && ({lbl: 
0;})); }
   }
+
+  /// GCC agrees with the bytecode interpreter here.
+  void switchInSE() {
+    static_assert(({ // ref-error {{not an integral constant expression}}
+          int i = 20;
+           switch(10) {
+             case 10: i = 300; // ref-note {{a constant expression cannot 
modify an object that is visible outside that expression}}
+           }
+           i;
+        }) == 300);
+  }
 }
 #endif
 

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to