https://gcc.gnu.org/g:833722e02b083a5828e0f7dbf67ae5456755f0de

commit r17-1041-g833722e02b083a5828e0f7dbf67ae5456755f0de
Author: Jakub Jelinek <[email protected]>
Date:   Sat May 30 17:49:18 2026 +0200

    c++: Don't ICE on the static constexpr expansion-stmt vars during mangling 
[PR125123]
    
    The following testcase ICEs, because we decide to mangle the (for the time
    being as workaround static constexpr variables created for expansion
    statements).  And if there is more than one in the same function and we
    mangle both, we ICE because they mangle the same.
    
    The problem is that cp_finish_decl does not determine_local_discriminator
    for DECL_ARTIFICIAL vars.
    The following patch fixes that by calling it even for DECL_ARTIFICIAL vars.
    The patch also sets DECL_IGNORED_P on those vars, I think there is no
    value exposing those in the debug information, the iterating is done
    at compile time and all user IMHO cares are the individual user variables
    initialized to whatever was derived from the temporaries.
    
    2026-05-29  Jakub Jelinek  <[email protected]>
    
            PR c++/125123
            * parser.cc (cp_build_range_for_decls): If range_temp or begin
            are static, set DECL_IGNORED_P on it.
            * pt.cc (finish_expansion_stmt): Similarly for iter.
            * decl.cc (cp_finish_decl): Call determine_local_discriminator
            etc. also for DECL_ARTIFICIAL TREE_STATIC vars.
    
            * g++.dg/cpp26/expansion-stmt42.C: New test.
    
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/decl.cc                                |  4 +---
 gcc/cp/parser.cc                              |  2 ++
 gcc/cp/pt.cc                                  |  1 +
 gcc/testsuite/g++.dg/cpp26/expansion-stmt42.C | 22 ++++++++++++++++++++++
 4 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 457d60bcd055..f5fe8673b774 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -9887,9 +9887,7 @@ cp_finish_decl (tree decl, tree init, bool 
init_const_expr_p,
         require a guard variable, and since the mangled name of the
         guard variable will depend on the mangled name of this
         variable.  */
-      if (DECL_FUNCTION_SCOPE_P (decl)
-         && TREE_STATIC (decl)
-         && !DECL_ARTIFICIAL (decl))
+      if (DECL_FUNCTION_SCOPE_P (decl) && TREE_STATIC (decl))
        {
          /* The variable holding an anonymous union will have had its
             discriminator set in finish_anon_union, after which it's
diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index f25ce71a237d..86b5e89214d7 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -16063,6 +16063,7 @@ cp_build_range_for_decls (location_t loc, tree 
range_expr, tree *end_p,
                  DECL_INTERFACE_KNOWN (range_temp) = 1;
                  DECL_DECLARED_CONSTEXPR_P (range_temp) = 1;
                  TREE_READONLY (range_temp) = 1;
+                 DECL_IGNORED_P (range_temp) = 1;
                }
            }
          else
@@ -16092,6 +16093,7 @@ cp_build_range_for_decls (location_t loc, tree 
range_expr, tree *end_p,
       TREE_STATIC (begin) = 1;
       DECL_DECLARED_CONSTEXPR_P (begin) = 1;
       TREE_READONLY (begin) = 1;
+      DECL_IGNORED_P (begin) = 1;
     }
   pushdecl (begin);
   cp_finish_decl (begin, begin_expr,
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ec8a4efbe297..da45a4971a71 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -33704,6 +33704,7 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
              TREE_STATIC (iter) = 1;
              DECL_DECLARED_CONSTEXPR_P (iter) = 1;
              TREE_READONLY (iter) = 1;
+             DECL_IGNORED_P (iter) = 1;
            }
          pushdecl (iter);
          cp_finish_decl (iter, iter_init, /*is_constant_init*/false,
diff --git a/gcc/testsuite/g++.dg/cpp26/expansion-stmt42.C 
b/gcc/testsuite/g++.dg/cpp26/expansion-stmt42.C
new file mode 100644
index 000000000000..240fa2a43f28
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt42.C
@@ -0,0 +1,22 @@
+// PR c++/125123
+// { dg-do compile { target c++20 } }
+// { dg-options "-g" }
+
+struct A
+{
+  int a[2];
+  constexpr int const *begin () const { return a; }
+  constexpr int const *end () const { return a + 2; }
+};
+
+template <typename T>
+requires true
+void
+foo ()
+{
+  template for (constexpr int i : A { 0, 1 })  // { dg-warning "'template for' 
only available with" "" { target c++23_down } }
+    ;
+  template for (constexpr int i : A { 0, 1 })  // { dg-warning "'template for' 
only available with" "" { target c++23_down } }
+    ;
+}
+template void foo <void> ();

Reply via email to