On Wed, Jan 28, 2026 at 02:34:48PM +0800, Jason Merrill wrote:
> > > This is so close to build_range_temp that I think it should get
> > > a "bool expansion_stmt_p" parm so that we can reuse the do_auto_deduction
> > > and further code.
> > 
> > Perhaps, it looked small enough for me, just 4 function calls and two flag
> > setting stmts, the rest is different.  But can change sure, or part of
> > build_range_temp could be outlined to finish_range_temp that this could use.
> 
> I think I agree with Marek's suggestion.  OK with that change.

Here is what I'm going to test tonight, so far tested with
GXX_TESTSUITE_STDS=98,11,14,17,20,23,26 make check-g++ -j32 -k 
RUNTESTFLAGS="dg.exp='reflection/* expansion-stmt*'"
I've kept the paren expr construction in the caller because it later needs
that value, so build_range_temp is just auto &&__for_range = range_expr;
vs. const decltype(auto) __for_range = range_expr; for now (and guess later
on for CWG3140 we'll also need to tell it whether it should do const or
not, but that will need to be done in cp_build_range_for_decls too).

2026-01-28  Jakub Jelinek  <[email protected]>

        * cp-tree.h (build_range_temp): Implement CWG3131 - Value
        categories and types for the range in iterable expansion statements.
        Add bool argument defaulted to false.
        * parser.cc (build_range_temp): Add expansion_stmt_p argument,
        if true build const decltype(auto) __for_range = range_expr instead of
        auto &&__for_range = range_expr.
        (cp_build_range_for_decls): For expansion stmts build __for_range as
        static constexpr decltype(auto) __for_range = (range_expr);
        rather than static constexpr auto &&__for_range = range_expr;.

        * g++.dg/cpp26/expansion-stmt1.C (N::begin, N::end, O::begin,
        O::end): Change argument type from B & to const B & or from
        D & to const D &.
        * g++.dg/cpp26/expansion-stmt2.C (N::begin, N::end, O::begin,
        O::end): Likewise.
        * g++.dg/cpp26/expansion-stmt3.C (N::begin, N::end, O::begin,
        O::end): Likewise.
        * g++.dg/cpp26/expansion-stmt16.C: Expect different diagnostics
        for C++11.
        * g++.dg/cpp26/expansion-stmt18.C (N::begin, N::end): Change
        argument type from B & to const B &.
        * g++.dg/cpp26/expansion-stmt25.C (N::begin, N::end): Likewise.
        * g++.dg/cpp26/expansion-stmt26.C: New test.
        * g++.dg/reflect/p3491-2.C (baz): Move workaround to a new
        function garply, use the previously #if 0 guarded implementation.
        (garply): New function.

--- gcc/cp/cp-tree.h.jj 2026-01-28 09:50:17.238775416 +0100
+++ gcc/cp/cp-tree.h    2026-01-28 12:27:52.252582912 +0100
@@ -7995,7 +7995,7 @@ extern bool maybe_clone_body                      (tree);
 extern tree cp_build_range_for_decls (location_t, tree, tree *, bool);
 extern tree cp_convert_range_for (tree, tree, tree, cp_decomp *, bool,
                                  tree, bool);
-extern tree build_range_temp (tree);
+extern tree build_range_temp (tree, bool = false);
 extern tree cp_perform_range_for_lookup        (tree, tree *, tree *,
                                         tsubst_flags_t = tf_warning_or_error);
 extern void cp_convert_omp_range_for (tree &, tree &, tree &,
--- gcc/cp/parser.cc.jj 2026-01-28 10:20:25.783446142 +0100
+++ gcc/cp/parser.cc    2026-01-28 12:48:38.974311063 +0100
@@ -15855,12 +15855,24 @@ cp_parser_range_for (cp_parser *parser,
    builds up the range temporary.  */
 
 tree
-build_range_temp (tree range_expr)
+build_range_temp (tree range_expr, bool expansion_stmt_p /* = false */)
 {
-  /* Find out the type deduced by the declaration
-     `auto &&__range = range_expr'.  */
-  tree auto_node = make_auto ();
-  tree range_type = cp_build_reference_type (auto_node, true);
+  tree range_type, auto_node;
+
+  if (expansion_stmt_p)
+    {
+      /* Build const decltype(auto) __range = range_expr;
+         - range_expr provided by the caller already is (range_expr).  */
+      auto_node = make_decltype_auto ();
+      range_type = cp_build_qualified_type (auto_node, TYPE_QUAL_CONST);
+    }
+  else
+    {
+      /* Find out the type deduced by the declaration
+         `auto &&__range = range_expr'.  */
+      auto_node = make_auto ();
+      range_type = cp_build_reference_type (auto_node, true);
+    }
   range_type = do_auto_deduction (range_type, range_expr, auto_node);
 
   /* Create the __range variable.  */
@@ -16019,13 +16031,17 @@ cp_build_range_for_decls (location_t loc
        range_temp = range_expr;
       else
        {
-         range_temp = build_range_temp (range_expr);
          if (expansion_stmt_p)
            {
-             /* Depending on CWG3044 resolution, we might want to remove
-                these 3 sets of TREE_STATIC (on range_temp, begin and end).
-                Although it can only be done when P2686R4 is fully
-                implemented.  */
+             /* Build constexpr decltype(auto) __for_range = (range_expr);  */
+             location_t range_loc = cp_expr_loc_or_loc (range_expr, loc);
+             range_expr
+               = finish_parenthesized_expr (cp_expr (range_expr, range_loc));
+             range_temp = build_range_temp (range_expr, true);
+
+             /* When P2686R4 is fully implemented, these 3 sets of TREE_STATIC
+                (on range_temp, begin and end) should be removed as per
+                CWG3044.  */
              TREE_STATIC (range_temp) = 1;
              TREE_PUBLIC (range_temp) = 0;
              DECL_COMMON (range_temp) = 0;
@@ -16033,6 +16049,10 @@ cp_build_range_for_decls (location_t loc
              DECL_DECLARED_CONSTEXPR_P (range_temp) = 1;
              TREE_READONLY (range_temp) = 1;
            }
+         else
+           /* Build auto &&__for_range = range_expr;  */
+           range_temp = build_range_temp (range_expr);
+
          pushdecl (range_temp);
          cp_finish_decl (range_temp, range_expr,
                          /*is_constant_init*/false, NULL_TREE,
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt1.C.jj     2026-01-28 
09:54:50.584498257 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt1.C        2026-01-28 
12:27:22.289326150 +0100
@@ -40,15 +40,15 @@ struct C
 namespace N
 {
   struct B { constexpr B () {} };
-  constexpr A begin (B &) { return A (0); }
-  constexpr A end (B &) { return A (6); }
+  constexpr A begin (const B &) { return A (0); }
+  constexpr A end (const B &) { return A (6); }
 }
 
 namespace O
 {
   struct D { constexpr D () {} };
-  constexpr C begin (D &) { return C (0, 42, 5); }
-  constexpr C end (D &) { return C (6, 36, 11); }
+  constexpr C begin (const D &) { return C (0, 42, 5); }
+  constexpr C end (const D &) { return C (6, 36, 11); }
 }
 
 long long
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt2.C.jj     2026-01-28 
09:54:50.586659034 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt2.C        2026-01-28 
12:27:22.289479158 +0100
@@ -40,15 +40,15 @@ struct C
 namespace N
 {
   struct B { constexpr B () {} };
-  constexpr A begin (B &) { return A (0); }
-  constexpr A end (B &) { return A (6); }
+  constexpr A begin (const B &) { return A (0); }
+  constexpr A end (const B &) { return A (6); }
 }
 
 namespace O
 {
   struct D { constexpr D () {} };
-  constexpr C begin (D &) { return C (0, 42, 5); }
-  constexpr C end (D &) { return C (6, 36, 11); }
+  constexpr C begin (const D &) { return C (0, 42, 5); }
+  constexpr C end (const D &) { return C (6, 36, 11); }
 }
 
 #if __cpp_nontype_template_parameter_class >= 201806L
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt3.C.jj     2026-01-28 
09:54:50.587177815 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt3.C        2026-01-28 
12:27:22.289594999 +0100
@@ -40,15 +40,15 @@ struct C
 namespace N
 {
   struct B { constexpr B () {} };
-  constexpr A begin (B &) { return A (0); }
-  constexpr A end (B &) { return A (6); }
+  constexpr A begin (const B &) { return A (0); }
+  constexpr A end (const B &) { return A (6); }
 }
 
 namespace O
 {
   struct D { constexpr D () {} };
-  constexpr C begin (D &) { return C (0, 42, 5); }
-  constexpr C end (D &) { return C (6, 36, 11); }
+  constexpr C begin (const D &) { return C (0, 42, 5); }
+  constexpr C end (const D &) { return C (6, 36, 11); }
 }
 
 template <int N>
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt16.C.jj    2026-01-20 
01:13:20.349260041 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt16.C       2026-01-28 
12:27:22.289706924 +0100
@@ -43,23 +43,26 @@ foo ()
 {
   B c = { 3 };
   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 *-*-* } .-1 }
+    ;                                  // { dg-error "'c' is not a constant 
expression" "" { target c++14 } .-1 }
+                                       // { dg-error "the value of 'c' is not 
usable in a constant expression" "" { target c++11_down } .-1 }
   C d = { 3 };
   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 *-*-* } .-1 }
+    ;                                  // { dg-error "'d' is not a constant 
expression" "" { target c++14 } .-1 }
                                        // { dg-error "call to non-'constexpr' 
function 'const A\\\* C::begin\\\(\\\) const'" "" { target c++11_down } .-1 }
                                        // { dg-error "call to non-'constexpr' 
function 'const A\\\* C::end\\\(\\\) const'" "" { target c++11_down } .-2 }
+                                       // { dg-error "the type 'const C' of 
'constexpr' variable '__for_range ' is not literal" "" { target c++11_down } 
.-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 *-*-* } .-1 }
+    ;                                  // { 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 *-*-* } .-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 *-*-* } .-1 }
+    ;                                  // { dg-error "'f' is not a constant 
expression" "" { target c++14 } .-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 *-*-* } .-1 }
+    ;                                  // { 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 }
   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 } }
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt18.C.jj    2026-01-28 
09:54:50.587619229 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt18.C       2026-01-28 
12:27:22.289840511 +0100
@@ -17,8 +17,8 @@ struct A
 namespace N
 {
   struct B { constexpr B () {} };
-  constexpr A begin (B &) { return A (0); }
-  constexpr A end (B &) { return A (6); }
+  constexpr A begin (const B &) { return A (0); }
+  constexpr A end (const B &) { return A (6); }
 }
 
 void
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt25.C.jj    2026-01-28 
09:54:50.587756365 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt25.C       2026-01-28 
12:27:22.289942910 +0100
@@ -15,8 +15,8 @@ struct A
 namespace N
 {
   struct B { constexpr B () {} };
-  constexpr A begin (B &) { return A (0); }
-  constexpr A end (B &) { return A (6); }
+  constexpr A begin (const B &) { return A (0); }
+  constexpr A end (const B &) { return A (6); }
 }
 
 void
--- gcc/testsuite/g++.dg/cpp26/expansion-stmt26.C.jj    2026-01-28 
12:27:22.290040084 +0100
+++ gcc/testsuite/g++.dg/cpp26/expansion-stmt26.C       2026-01-28 
12:27:22.290040084 +0100
@@ -0,0 +1,18 @@
+// C++26 P1306R5 - Expansion statements
+// { dg-do run { target c++23 } }
+// { dg-options "" }
+
+#include <span>
+
+constexpr int arr[3] = { 1, 2, 3 };
+consteval std::span <const int> foo () { return std::span <const int> (arr); }
+
+int
+main ()
+{
+  int r = 0;
+  template for (constexpr auto m : foo ())     // { dg-warning "'template for' 
only available with" "" { target c++23_down } }
+    r += m;
+  if (r != 6)
+    __builtin_abort ();
+}
--- gcc/testsuite/g++.dg/reflect/p3491-2.C.jj   2026-01-15 16:33:47.014097840 
+0100
+++ gcc/testsuite/g++.dg/reflect/p3491-2.C      2026-01-28 13:04:13.083366251 
+0100
@@ -17,17 +17,9 @@ consteval void bar() {
 
 consteval int baz() {
     int r = 0;
-#if 0
-    // TODO: This doesn't work yet.
     template for (constexpr int I : std::define_static_array(foo())) {
        r += I;
     }
-#else
-    // Ugly workaround for that.
-    template for (constexpr int I : (const std::span<const 
int>)std::define_static_array(foo())) {
-       r += I;
-    }
-#endif
     return r;
 }
 
@@ -45,6 +37,15 @@ consteval int fred() {
     return (... + m);
 }
 
+consteval int garply() {
+    int r = 0;
+    template for (constexpr int I : (const std::span<const 
int>)std::define_static_array(foo())) {
+       r += I;
+    }
+    return r;
+}
+
 static_assert (baz() == 6);
 static_assert (qux() == 6);
 static_assert (fred<int>() == 6);
+static_assert (garply() == 6);


        Jakub

Reply via email to