[clang] [clang][constexpr] Allow IndirectGotoStmt in constexpr functions for C++23 (PR #213449)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 1 07:14:26 PDT 2026
https://github.com/babadany2999 created https://github.com/llvm/llvm-project/pull/213449
### Summary
P2242R3 relaxed `constexpr` function requirements in C++23 to allow jump statements such as `goto` and labels inside `constexpr` functions. In `CheckConstexprFunctionStmt`, `GotoStmtClass` and `LabelStmtClass` were added, but `IndirectGotoStmtClass` (GNU computed goto) was missed.
This patch adds `case Stmt::IndirectGotoStmtClass:` alongside `GotoStmtClass` in `CheckConstexprFunctionStmt`.
### Details
- Before this patch, writing `goto *p;` inside a `constexpr` function resulted in `error: statement not allowed in constexpr function` because `IndirectGotoStmtClass` fell through to the `default` case in `CheckConstexprFunctionStmt`.
- With this patch, indirect gotos behave identically to standard gotos in `constexpr` function bodies:
- Allowed in C++23 mode.
- Issued as a C++23 extension warning in earlier modes (C++11 through C++20).
- Executing an indirect goto during constant evaluation continues to produce an error during evaluation, while valid runtime paths (e.g. guarded by `if consteval`) succeed.
### Test Plan
- Added test case covering indirect gotos (`goto *(&&x);`) to `clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp`.
- Verified test fails without the patch and passes with the patch across all standard modes (`-std=c++11` through C++23).
>From db3e8ab18e94e552c4e48dc128373c66b9551491 Mon Sep 17 00:00:00 2001
From: Baba Dan Constantin <babadany2999 at gmail.com>
Date: Sat, 1 Aug 2026 17:09:50 +0300
Subject: [PATCH] [clang][constexpr] Allow IndirectGotoStmt in constexpr
functions for C++23
P2242R3 ("More Relaxed Constexpr Functions") permitted jump statements such as `goto` and labels inside `constexpr` functions starting in C++23. When handling `GotoStmtClass` and `LabelStmtClass` in `CheckConstexprFunctionStmt`, `IndirectGotoStmtClass` (GNU computed goto) was inadvertently omitted.
As a result, any indirect goto inside a `constexpr` function body hit the default case in `CheckConstexprFunctionStmt` and triggered an unrecoverable hard error (`err_constexpr_body_invalid_stmt`), even when placed in unexecuted branches (e.g. behind `if consteval`).
This commit adds `case Stmt::IndirectGotoStmtClass:` alongside `GotoStmtClass` so that indirect gotos are treated consistently:
- Permitted in C++23 `constexpr` functions.
- Allowed in earlier standard dialects (C++11/14/17/20) as a C++23 extension warning.
- Evaluated properly by constant evaluation (which rejects actual compile-time execution of computed gotos if reached).
Regression test added to `clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp`.
---
clang/lib/Sema/SemaDeclCXX.cpp | 1 +
clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp | 8 ++++++++
2 files changed, 9 insertions(+)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 47b01b913b428..76b3d8cca9701 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -2343,6 +2343,7 @@ CheckConstexprFunctionStmt(Sema &SemaRef, const FunctionDecl *Dcl, Stmt *S,
case Stmt::LabelStmtClass:
case Stmt::GotoStmtClass:
+ case Stmt::IndirectGotoStmtClass:
if (Cxx2bLoc.isInvalid())
Cxx2bLoc = S->getBeginLoc();
for (Stmt *SubStmt : S->children()) {
diff --git a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
index 51990ee4341d2..508941ae0429d 100644
--- a/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
+++ b/clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp
@@ -167,6 +167,14 @@ constexpr int DisallowedStmtsCXX14_7() {
int n; // beforecxx20-warning {{uninitialized variable in a constexpr function}}
}
+constexpr int DisallowedStmtsCXX14_8() {
+ return 0; // beforecxx14-note {{previous}}
+ // - an indirect goto statement
+ goto *(&&x); // beforecxx23-warning {{use of this statement in a constexpr function is a C++23 extension}}
+ x:;
+ return 0; // beforecxx14-warning {{multiple return}}
+}
+
constexpr int ForStmt() {
for (int n = 0; n < 10; ++n) {} // beforecxx14-error {{statement not allowed in constexpr function}}
return 0;
More information about the cfe-commits
mailing list