[PATCH] c++: Fix up switch constexpr evaluation with vacuous initializations jumped across [PR127777]
Jakub Jelinek
jakub@redhat.com
Fri Oct 9 07:46:52 GMT 2026
Hi!
The following testcase is incorrectly rejected with errors about
accessing variables outside of their lifetime.
The reason is that we don't start their lifetime when
switches (jump_target) is true.
The following patch fixes it, by not doing the whole DECL_EXPR
handling when switches (jump_target) is true (we don't want to
initialize them, just start their lifetime. At the end of a BIND_EXPR
vars in that scope have their lifetime ended if we uselessly started
it even for scopes which (or their nested scopes) don't have the
right case/default label.
Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?
A slightly related question is whether we need to diagnose control
flow passing through non-const useable static/thread block scope
declaration https://eel.is/c++draft/expr.const#core-2.2 when jumping
across their declaration through switch.
constexpr int foo (int x) { if (x) { static int a = 42; } return 1; }
constexpr int bar (int x) { switch (x) { { static int a = 42; case 42: break; } default: break; } return 1; }
constexpr int a = foo (1);
constexpr int b = bar (1);
constexpr int c = bar (42);
g++ diagnoses just foo (1), clang++ all 3. Diagnosing bar (1)
seems like a clear clang bug to me. Tomasz said he thinks that
bar (42) isn't invalid either, that at runtime we don't initialize
the var either (at least if it is not initialized by constant
expression), the var is not usable in constant expressions and so
using it after the case 42: would be diagnosed.
If you agree, I can post incrementally a testcase covering it just
to verify that.
2026-10-09 Jakub Jelinek <jakub@redhat.com>
PR c++/127777
* constexpr.cc (cxx_eval_constant_expression): For DECL_EXPR
if switches (jump_target), call ctx->global->put_value for
vaucously initialized vars.
* g++.dg/cpp2a/constexpr-init25.C: New test.
--- a/gcc/cp/constexpr.cc 2026-10-08 13:36:40.982004201 +0200
+++ b/gcc/cp/constexpr.cc 2026-10-08 15:59:34.699946811 +0200
@@ -10010,6 +10010,18 @@ cxx_eval_constant_expression (const cons
/* Found it. */
*jump_target = NULL_TREE;
return NULL_TREE;
+ case DECL_EXPR:
+ if (switches (jump_target))
+ {
+ /* When jumping across a vacuous initialization in a switch
+ body, remember it started lifetime. */
+ tree r = DECL_EXPR_DECL (t);
+ if (VAR_P (r)
+ && !TREE_STATIC (r)
+ && !DECL_NONTRIVIALLY_INITIALIZED_P (r))
+ ctx->global->put_value (r, NULL_TREE);
+ }
+ return NULL_TREE;
default:
return NULL_TREE;
}
--- a/gcc/testsuite/g++.dg/cpp2a/constexpr-init25.C 2026-10-08 16:10:29.657413320 +0200
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-init25.C 2026-10-08 16:12:40.271669139 +0200
@@ -0,0 +1,55 @@
+// PR c++/127777
+// { dg-do compile { target c++20 } }
+
+struct S { int a, b; };
+
+constexpr int
+foo (int x)
+{
+ switch (x)
+ {
+ int a;
+ case 1:
+ {
+ int b;
+ [[fallthrough]];
+ case 2:
+ {
+ int c;
+ [[fallthrough]];
+ case 3:
+ a = 1;
+ b = 2;
+ c = 3;
+ return a + b + c;
+ }
+ case 4:
+ a = 4;
+ b = 5;
+ return a + b;
+ }
+ case 5:
+ a = 7;
+ return a;
+ S d;
+ case 6:
+ d.a = 8;
+ d.b = 9;
+ return d.a + d.b;
+ int e[3];
+ case 7:
+ e[0] = 10;
+ e[1] = 11;
+ return e[0] + e[1];
+ }
+ return -1;
+}
+
+static_assert (foo (1) == 6);
+static_assert (foo (2) == 6);
+static_assert (foo (3) == 6);
+static_assert (foo (4) == 9);
+static_assert (foo (5) == 7);
+static_assert (foo (6) == 17);
+static_assert (foo (7) == 21);
+static_assert (foo (0) == -1);
Jakub
More information about the Gcc-patches
mailing list