Hi! On top of Marek's https://gcc.gnu.org/pipermail/gcc-patches/2026-November/738116.html P2686R5 PR117784 patch, the following patch changes iterating expansion statement handling to implement the so far omitted part of https://wg21.link/cwg3044 , in particular the removal of static keywords from range, begin (end is gone) and iter artificial variables.
Because the P2686R5 second half is only implemented for C++26, this patch doesn't remove the static keywords for C++23 and earlier when iterating expansion statements are used as an extension, that causes too much fallout. In expansion-stmt16.C which tests for various invalid cases I had to tweak the diagnostics for C++26 and later. Unfortunately, this patch causes a regression on reflect/expansion-stmt2.C, we IMHO bogusly reject it now. Xfailed for now, I'll file a PR, dunno where the bug it is. The diagnostics is on an artificial VAR_DECL added when performing lifetime extension for __for_range, somehow during constant evaluation we don't mark it as being already in scope by the time we do INIT_EXPR for it. So far lightly tested. 2026-11-16 Jakub Jelinek <[email protected]> * parser.cc (cp_build_range_for_decls): Only mark range_temp and begin VAR_DECLs TREE_STATIC for constexpr in C++23 and earlier. * pt.cc (finish_expansion_stmt): Similarly for iter. * g++.dg/cpp26/expansion-stmt16.C: Adjust expected diagnostics for C++26 and later. * g++.dg/cpp26/expansion-stmt47.C: New test. * g++.dg/cpp26/expansion-stmt48.C: New test. * g++.dg/reflect/expansion-stmt2.C: Add xfailed dg-bogus for now. --- a/gcc/cp/parser.cc 2026-11-13 14:51:36.460181649 +0100 +++ b/gcc/cp/parser.cc 2026-11-16 09:54:20.758340534 +0100 @@ -16226,16 +16226,19 @@ cp_build_range_for_decls (location_t loc = finish_parenthesized_expr (cp_expr (range_expr, range_loc)); range_temp = build_range_temp (range_expr, expansion_stmt_decl); - /* When P2686R4 is fully implemented, these 3 sets of TREE_STATIC - (on range_temp, begin and end) should be removed as per - CWG3044. If expansion_stmt_decl is not constexpr, we don't - need the static though. */ + /* As P2686R4 is not implemented for C++23 and earlier, + implement C++23 and earlier expansion statement (extension) + using the pre-CWG3044 way by having range and begin + static. */ if (DECL_DECLARED_CONSTEXPR_P (expansion_stmt_decl)) { - TREE_STATIC (range_temp) = 1; - TREE_PUBLIC (range_temp) = 0; - DECL_COMMON (range_temp) = 0; - DECL_INTERFACE_KNOWN (range_temp) = 1; + if (cxx_dialect < cxx26) + { + TREE_STATIC (range_temp) = 1; + TREE_PUBLIC (range_temp) = 0; + DECL_COMMON (range_temp) = 0; + DECL_INTERFACE_KNOWN (range_temp) = 1; + } DECL_DECLARED_CONSTEXPR_P (range_temp) = 1; TREE_READONLY (range_temp) = 1; DECL_IGNORED_P (range_temp) = 1; @@ -16265,7 +16268,8 @@ cp_build_range_for_decls (location_t loc DECL_ARTIFICIAL (begin) = 1; if (expansion_stmt_decl && DECL_DECLARED_CONSTEXPR_P (expansion_stmt_decl)) { - TREE_STATIC (begin) = 1; + if (cxx_dialect < cxx26) + TREE_STATIC (begin) = 1; DECL_DECLARED_CONSTEXPR_P (begin) = 1; TREE_READONLY (begin) = 1; DECL_IGNORED_P (begin) = 1; --- a/gcc/cp/pt.cc 2026-11-15 13:44:23.562235258 +0100 +++ b/gcc/cp/pt.cc 2026-11-16 09:54:49.355949550 +0100 @@ -34044,7 +34044,8 @@ finish_expansion_stmt (tree expansion_st DECL_ARTIFICIAL (iter) = 1; if (DECL_DECLARED_CONSTEXPR_P (range_decl)) { - TREE_STATIC (iter) = 1; + if (cxx_dialect < cxx26) + TREE_STATIC (iter) = 1; DECL_DECLARED_CONSTEXPR_P (iter) = 1; TREE_READONLY (iter) = 1; DECL_IGNORED_P (iter) = 1; --- a/gcc/testsuite/g++.dg/cpp26/expansion-stmt16.C 2026-04-07 18:07:53.427871937 +0200 +++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt16.C 2026-11-16 10:11:14.285492119 +0100 @@ -6,14 +6,14 @@ struct A { int s; }; constexpr A a[] = { { 3 }, { 4 }, { 5 }, { 6 }, { 7 } }; struct B { constexpr const A *begin () const { return &a[0]; } - constexpr const A *end () const { return &a[s]; } + constexpr const A *end () const { return &a[s]; } // { dg-error "the value of 'c' is not usable in a constant expression" "" { target c++26 } } int s; }; constexpr B b = { 3 }; struct C { C (int x) : s (x) {} constexpr const A *begin () const { return &a[0]; } - constexpr const A *end () const { return &a[s]; } + constexpr const A *end () const { return &a[s]; } // { dg-error "the value of 'd' is not usable in a constant expression" "" { target c++26 } } int s; }; struct D { @@ -41,27 +41,29 @@ struct H { int a; F b; int c; }; void foo () { - B c = { 3 }; + B c = { 3 }; // { dg-message "'c' was not declared 'constexpr'" "" { target c++26 } } template for (constexpr auto g : c) // { dg-warning "'template for' only available with" "" { target c++23_down } } - ; // { dg-error "'c' is not a constant expression" "" { target c++14 } .-1 } + ; // { dg-error "'c' is not a constant expression" "" { target { c++14 && c++23_down } } .-1 } // { dg-error "the value of 'c' is not usable in a constant expression" "" { target c++11_down } .-1 } - C d = { 3 }; + // { dg-message "in 'constexpr' expansion of" "" { target c++26 } .-2 } + C d = { 3 }; // { dg-message "'d' was not declared 'constexpr'" "" { target c++26 } } template for (constexpr auto g : d) // { dg-warning "'template for' only available with" "" { target c++23_down } } - ; // { dg-error "'d' is not a constant expression" "" { target c++14 } .-1 } + ; // { dg-error "'d' is not a constant expression" "" { target { c++14 && c++23_down } } .-1 } // { dg-error "call to non-'constexpr' function 'const A\\\* C::begin\\\(\\\) const'" "" { target c++11_down } .-1 } // { dg-error "the type 'const C' of 'constexpr' variable '__for_range ' is not literal" "" { target c++11_down } .-2 } + // { dg-message "in 'constexpr' expansion of" "" { target c++26 } .-3 } constexpr D e = { 3 }; template for (constexpr auto g : e) // { dg-warning "'template for' only available with" "" { target c++23_down } } - ; // { dg-error "'e' is not a constant expression" "" { target c++14 } .-1 } - // { dg-error "call to non-'constexpr' function 'const A\\\* D::end\\\(\\\) const'" "" { target c++11_down } .-1 } + ; // { dg-error "'e' is not a constant expression" "" { target { c++14 && c++23_down } } .-1 } + // { dg-error "call to non-'constexpr' function 'const A\\\* D::end\\\(\\\) const'" "" { target { c++11_down || c++26 } } .-1 } constexpr E f = { 3 }; template for (constexpr auto g : f) // { dg-warning "'template for' only available with" "" { target c++23_down } } - ; // { dg-error "'f' is not a constant expression" "" { target c++14 } .-1 } + ; // { dg-error "'f' is not a constant expression" "" { target { c++14 && c++23_down } } .-1 } // { dg-error "call to non-'constexpr' function 'const A\\\* E::begin\\\(\\\) const'" "" { target *-*-* } .-1 } constexpr G h = { 3 }; template for (constexpr auto g : h) // { dg-warning "'template for' only available with" "" { target c++23_down } } - ; // { dg-error "'h' is not a constant expression" "" { target c++14 } .-1 } - // { dg-error "the type 'const F' of 'constexpr' variable 'g' is not literal" "" { target c++11_down } .-2 } + ; // { dg-error "'h' is not a constant expression" "" { target { c++14 && c++23_down } } .-1 } + // { dg-error "the type 'const F' of 'constexpr' variable 'g' is not literal" "" { target { c++11_down || c++26 } } .-2 } template for (constexpr auto g : { 1, 2, F { 3 }, 4L }) // { dg-warning "'template for' only available with" "" { target c++23_down } } ; // { dg-error "the type 'const F' of 'constexpr' variable 'g' is not literal" "" { target *-*-* } .-1 } template for (constexpr auto g : H {})// { dg-warning "'template for' only available with" "" { target c++23_down } } --- a/gcc/testsuite/g++.dg/cpp26/expansion-stmt47.C 2026-11-16 10:37:53.748684356 +0100 +++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt47.C 2026-11-16 10:38:08.753480198 +0100 @@ -0,0 +1,18 @@ +// { dg-do run { target c++26 } } + +struct S { + constexpr S (int x, int y, int z) : a{} { a[0] = x; a[1] = y; a[2] = z; } + int a[3]; + constexpr const int *begin () const { return &a[0]; } + constexpr const int *end () const { return &a[3]; } +}; + +int +main () +{ + constexpr S s (1, 2, 3); + int r = 0; + template for (constexpr auto i : s) + r += i; + return r - 6; +} --- a/gcc/testsuite/g++.dg/cpp26/expansion-stmt48.C 2026-11-16 10:38:17.958354954 +0100 +++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt48.C 2026-11-16 10:48:44.997823351 +0100 @@ -0,0 +1,19 @@ +// { dg-do compile { target c++14 } } +// { dg-options "" } + +struct S { + constexpr S (int x, int y, int z) : a{} { a[0] = x; a[1] = y; a[2] = z; } + int a[3]; + constexpr const int *begin () const { return &a[0]; } + constexpr const int *end () const { return &a[3]; } +}; + +int +main () +{ + constexpr S s (1, 2, 3); // { dg-message "address of non-static constexpr variable 's' may differ on each invocation of the enclosing function; add 'static'" "" { target c++23_down } } + int r = 0; + template for (constexpr auto i : s) // { dg-warning "'template for' only available with" "" { target c++23_down } } + r += i; // { dg-error "'s' is not a constant expression" "" { target c++23_down } .-1 } + return r - 6; // { dg-message "reference to 's' is not a constant expression" "" { target c++23_down } .-2 } +} --- a/gcc/testsuite/g++.dg/reflect/expansion-stmt2.C 2026-03-27 10:17:16.123298282 +0100 +++ b/gcc/testsuite/g++.dg/reflect/expansion-stmt2.C 2026-11-16 10:14:01.647206457 +0100 @@ -23,14 +23,14 @@ f () using U = [:type_of(a):]; constexpr auto m1 = extract<U>(a); constexpr auto m2 = extract<typename [:type_of(a):]>(a); - } + } // { dg-bogus "modification of '<anonymous>' from outside current evaluation is not a constant expression" "" { xfail *-*-* } } template for (constexpr auto val : define_static_array (enumerators_of (^^E))) { constexpr auto a = annotations_of(val)[0]; using U = [:type_of(a):]; foo<U>(); - } + } // { dg-bogus "modification of '<anonymous>' from outside current evaluation is not a constant expression" "" { xfail *-*-* } } [](auto a) { using V = [:type_of(^^a):];; Jakub
