]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix up [[maybe_unused]] handling on expansion stmts [PR122788]
authorJakub Jelinek <jakub@redhat.com>
Sat, 22 Nov 2025 11:39:09 +0000 (12:39 +0100)
committerJakub Jelinek <jakub@gcc.gnu.org>
Sat, 22 Nov 2025 11:39:09 +0000 (12:39 +0100)
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  <jakub@redhat.com>

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.

gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp26/expansion-stmt27.C [new file with mode: 0644]

index 27d786d0e7779ecba2075cb76a57e62593a5d64f..c418edc69313255a12aa4f13ed45827c5d25d9ca 100644 (file)
@@ -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 (file)
index 0000000..2a0d325
--- /dev/null
@@ -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
+    }
+}