https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127777
Bug ID: 127777
Summary: Incorrect rejection of stores to bypassed vacuous
initializers inside of switch
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: jakub at gcc dot gnu.org
Target Milestone: ---
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;
}
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 (0) == -1);
should be valid in C++20, clang++ accepts it but g++ rejects with
ab.C:33:24: error: non-constant condition for static assertion
33 | static_assert (foo (1) == 6);
| ~~~~~~~~^~~~
ab.C:33:20: in ‘constexpr’ expansion of ‘foo(1)’
33 | static_assert (foo (1) == 6);
| ~~~~^~~
ab.C:16:13: error: modification of ‘a’ from outside current evaluation is not a
constant expression
16 | a = 1;
| ~~^~~
ab.C:34:24: error: non-constant condition for static assertion
34 | static_assert (foo (2) == 6);
| ~~~~~~~~^~~~
ab.C:34:20: in ‘constexpr’ expansion of ‘foo(2)’
34 | static_assert (foo (2) == 6);
| ~~~~^~~
ab.C:16:13: error: modification of ‘a’ from outside current evaluation is not a
constant expression
16 | a = 1;
| ~~^~~
ab.C:35:24: error: non-constant condition for static assertion
35 | static_assert (foo (3) == 6);
| ~~~~~~~~^~~~
ab.C:35:20: in ‘constexpr’ expansion of ‘foo(3)’
35 | static_assert (foo (3) == 6);
| ~~~~^~~
ab.C:16:13: error: modification of ‘a’ from outside current evaluation is not a
constant expression
16 | a = 1;
| ~~^~~
ab.C:36:24: error: non-constant condition for static assertion
36 | static_assert (foo (4) == 9);
| ~~~~~~~~^~~~
ab.C:36:20: in ‘constexpr’ expansion of ‘foo(4)’
36 | static_assert (foo (4) == 9);
| ~~~~^~~
ab.C:22:11: error: modification of ‘a’ from outside current evaluation is not a
constant expression
22 | a = 4;
| ~~^~~
ab.C:37:24: error: non-constant condition for static assertion
37 | static_assert (foo (5) == 7);
| ~~~~~~~~^~~~
ab.C:37:20: in ‘constexpr’ expansion of ‘foo(5)’
37 | static_assert (foo (5) == 7);
| ~~~~^~~
ab.C:27:9: error: modification of ‘a’ from outside current evaluation is not a
constant expression
27 | a = 7;
| ~~^~~