https://gcc.gnu.org/g:cf865c811be1b83e960b3e661123cb19105d3181

commit r16-5507-gcf865c811be1b83e960b3e661123cb19105d3181
Author: Jakub Jelinek <[email protected]>
Date:   Sat Nov 22 12:39:09 2025 +0100

    c++: Fix up [[maybe_unused]] handling on expansion stmts [PR122788]
    
    This PR complains that [[maybe_unused]] attribute is ignored on
    the range-for-declaration of expansion-statement.
    
    We copy DECL_ATTRIBUTES and apply late attributes, but early attributes
    don't have their handlers called again, so some extra flags need to be
    copied as well.
    This copies TREE_USED and DECL_READ_P flags.
    
    2025-11-22  Jakub Jelinek  <[email protected]>
    
            PR c++/122788
            * pt.cc (finish_expansion_stmt): Or in TREE_USED and DECL_READ_P
            flags from range_decl to decl or from corresponding structured 
binding
            to this_decl.
    
            * g++.dg/cpp26/expansion-stmt27.C: New test.

Diff:
---
 gcc/cp/pt.cc                                  |  4 ++++
 gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C | 17 +++++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 27d786d0e777..c418edc69313 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -32900,6 +32900,8 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
        type = tsubst (type, args, complain | tf_tst_ok, in_decl);
       tree decl = build_decl (loc, VAR_DECL, DECL_NAME (range_decl), type);
       DECL_ATTRIBUTES (decl) = DECL_ATTRIBUTES (range_decl);
+      TREE_USED (decl) |= TREE_USED (range_decl);
+      DECL_READ_P (decl) |= DECL_READ_P (range_decl);
       if (args)
        apply_late_template_attributes (&decl, DECL_ATTRIBUTES (decl),
                                        /*flags=*/0, args, complain,
@@ -32958,6 +32960,8 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
              DECL_ARTIFICIAL (this_decl) = 1;
              DECL_ATTRIBUTES (this_decl)
                = DECL_ATTRIBUTES (TREE_VEC_ELT (v, i + 1));
+             TREE_USED (this_decl) |= TREE_USED (TREE_VEC_ELT (v, i + 1));
+             DECL_READ_P (this_decl) |= DECL_READ_P (TREE_VEC_ELT (v, i + 1));
              if (DECL_PACK_P (TREE_VEC_ELT (v, i + 1)))
                {
                  tree dtype = cxx_make_type (DECLTYPE_TYPE);
diff --git a/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C 
b/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C
new file mode 100644
index 000000000000..2a0d325e7a5b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C
@@ -0,0 +1,17 @@
+// PR c++/122788
+// { dg-do compile { target c++14 } }
+// { dg-options "-Wunused" }
+
+void
+foo ()
+{
+  template for ([[maybe_unused]] auto i : { 42 })              // { dg-warning 
"'template for' only available with" "" { target c++23_down } }
+    ;                                                          // { dg-bogus 
"unused variable 'i'" "" { target *-*-* } .-1 }
+  template for ([[maybe_unused]] auto j : { 1, 2, 3 })         // { dg-warning 
"'template for' only available with" "" { target c++23_down } }
+    {                                                          // { dg-bogus 
"unused variable 'j'" "" { target *-*-* } .-1 }
+#if __cpp_if_constexpr >= 201606
+      if constexpr (false)
+       (void) j;
+#endif
+    }
+}

Reply via email to