On 7/5/25 2:20 AM, Jakub Jelinek wrote:
Hi!

https://eel.is/c++draft/stmt.ranged#2
says that in for-range-declaration only type-specifier or constexpr
can appear.  As the following testcases show, we've emitted some
diagnostics in most cases, but not for static/thread_local (the patch
handles __thread too) and register in the non-sb case.
For extern there was an error that it is both extern and has an
initializer (again, non-sb only, sb errors on extern).

The following patch diagnoses those cases with pedwarn.
I've used for-range-declaration in the diagnostics wording (there was
already a case of that for the typedef), so that in the future
we don't need to differentiate it between range for and expansion
statements.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2025-07-05  Jakub Jelinek  <ja...@redhat.com>

        PR c++/84009
        * parser.cc (cp_parser_decomposition_declaration): Pedwarn
        on thread_local, __thread or static in decl_specifiers for
        for-range-declaration.
        (cp_parser_init_declarator): Likewise, and also for extern
        or register.

        * g++.dg/cpp0x/range-for40.C: New test.
        * g++.dg/cpp0x/range-for41.C: New test.
        * g++.dg/cpp0x/range-for42.C: New test.
        * g++.dg/cpp0x/range-for43.C: New test.

--- gcc/cp/parser.cc.jj 2025-07-04 19:49:14.702864248 +0200
+++ gcc/cp/parser.cc    2025-07-04 21:29:46.568232075 +0200
@@ -16919,6 +16919,15 @@ cp_parser_decomposition_declaration (cp_
        /* Ensure DECL_VALUE_EXPR is created for all the decls but
         the underlying DECL.  */
        cp_finish_decomp (decl, &decomp);
+      if (decl_spec_seq_has_spec_p (decl_specifiers, ds_thread))
+       pedwarn (decl_specifiers->locations[ds_thread],
+                OPT_Wpedantic, "for-range-declaration cannot be %qs",
+                decl_specifiers->gnu_thread_keyword_p
+                ? "__thread" : "thread_local");
+      else if (decl_specifiers->storage_class == sc_static)
+       pedwarn (decl_specifiers->locations[ds_storage_class],
+                OPT_Wpedantic, "for-range-declaration cannot be %qs",

I think we want these diagnostics enabled by default; I don't feel strongly about unconditional pedwarn vs. permerror.

Jason

Reply via email to