]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fn redecl in fn scope wrongly accepted [PR116239]
authorMarek Polacek <polacek@redhat.com>
Fri, 30 Aug 2024 18:12:22 +0000 (14:12 -0400)
committerMarek Polacek <polacek@redhat.com>
Thu, 5 Sep 2024 15:07:03 +0000 (11:07 -0400)
Redeclaration such as

  void f(void);
  consteval void f(void);

is invalid.  In a namespace scope, we detect the collision in
validate_constexpr_redeclaration, but not when one declaration is
at block scope.

When we have

  void f(void);
  void g() { consteval void f(void); }

we call pushdecl on the second f and call push_local_extern_decl_alias.
It finds the namespace-scope f:

        for (ovl_iterator iter (binding); iter; ++iter)
          if (decls_match (decl, *iter, /*record_versions*/false))
            {
              alias = *iter;
              break;
            }

but decls_match says they match so we just set DECL_LOCAL_DECL_ALIAS
(and do not call another pushdecl leading to duplicate_decls which
would detect mismatching return types, for example).  I don't think
we want to change decls_match, so a simple fix is to detect the
problem in push_local_extern_decl_alias.

PR c++/116239

gcc/cp/ChangeLog:

* cp-tree.h (validate_constexpr_redeclaration): Declare.
* decl.cc (validate_constexpr_redeclaration): No longer static.
* name-lookup.cc (push_local_extern_decl_alias): Call
validate_constexpr_redeclaration.

gcc/testsuite/ChangeLog:

* g++.dg/diagnostic/redeclaration-6.C: New test.

gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/name-lookup.cc
gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C [new file with mode: 0644]

index 2eeb5e3e8b16e0a5d52ac823ef3a82042ac1c802..1a763b683de8e068cb8b9be09ebba7c20630be7f 100644 (file)
@@ -6992,6 +6992,7 @@ extern bool member_like_constrained_friend_p      (tree);
 extern bool fns_correspond                     (tree, tree);
 extern int decls_match                         (tree, tree, bool = true);
 extern bool maybe_version_functions            (tree, tree, bool);
+extern bool validate_constexpr_redeclaration   (tree, tree);
 extern bool merge_default_template_args                (tree, tree, bool);
 extern tree duplicate_decls                    (tree, tree,
                                                 bool hiding = false,
index 7bad3047ad94c132b9f5f4026c07531d71ee27ff..f4128dbccdf07f80c68ed1caf05591b0628dc028 100644 (file)
@@ -1412,7 +1412,7 @@ check_redeclaration_exception_specification (tree new_decl,
 /* Return true if OLD_DECL and NEW_DECL agree on constexprness.
    Otherwise issue diagnostics.  */
 
-static bool
+bool
 validate_constexpr_redeclaration (tree old_decl, tree new_decl)
 {
   old_decl = STRIP_TEMPLATE (old_decl);
index 7a6cc244c153af5257dcdb5d8ec8b63d50c5340d..cd3947cbe4f5d633370e625cd1ac7446cbf4e0b2 100644 (file)
@@ -3637,6 +3637,8 @@ push_local_extern_decl_alias (tree decl)
          if (decls_match (decl, *iter, /*record_versions*/false))
            {
              alias = *iter;
+             if (!validate_constexpr_redeclaration (alias, decl))
+               return;
              break;
            }
 
diff --git a/gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C b/gcc/testsuite/g++.dg/diagnostic/redeclaration-6.C
new file mode 100644 (file)
index 0000000..ed8d4af
--- /dev/null
@@ -0,0 +1,34 @@
+// PR c++/116239
+// { dg-do compile { target c++20 } }
+
+consteval void f1();
+void f2();
+constexpr void f3();
+void f4();
+consteval void f5();
+constexpr void f6();
+
+void
+g ()
+{
+  void f1();           // { dg-error "differs in .consteval." }
+  consteval void f2(); // { dg-error "differs in .consteval." }
+
+  void f3();           // { dg-error "differs in .constexpr." }
+  constexpr void f4();  // { dg-error "differs in .constexpr." }
+
+  consteval void f5();
+  constexpr void f6();
+
+  void f7();
+  consteval void f7(); // { dg-error "differs in .consteval." }
+
+  consteval void f8();
+  void f8();           // { dg-error "differs in .consteval." }
+
+  void f9();
+  constexpr void f9(); // { dg-error "differs in .constexpr." }
+
+  constexpr void f10();
+  void f10();          // { dg-error "differs in .constexpr." }
+}