[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:15:27 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: babadany2999
<details>
<summary>Changes</summary>
### 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).
---
Full diff: https://github.com/llvm/llvm-project/pull/213449.diff
2 Files Affected:
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+1)
- (modified) clang/test/CXX/dcl.dcl/dcl.spec/dcl.constexpr/p3.cpp (+8)
``````````diff
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;
``````````
</details>
https://github.com/llvm/llvm-project/pull/213449
More information about the cfe-commits
mailing list