On 9/19/20 5:34 PM, Marek Polacek wrote:
[temp.deduct.guide]p3: Two deduction guide declarations in the same
translation unit for the same class template shall not have equivalent
parameter-declaration-clauses.

So let's detect that.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

OK.

gcc/cp/ChangeLog:

        PR c++/97099
        * decl.c (redeclaration_error_message): Detect a redeclaration of
        deduction guides.

gcc/testsuite/ChangeLog:

        PR c++/97099
        * g++.dg/cpp1z/class-deduction74.C: New test.
---
  gcc/cp/decl.c                                 | 20 ++++++++----
  .../g++.dg/cpp1z/class-deduction74.C          | 31 +++++++++++++++++++
  2 files changed, 45 insertions(+), 6 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction74.C

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 13f065d5058..af796499df7 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -3003,6 +3003,10 @@ redeclaration_error_message (tree newdecl, tree olddecl)
            }
        }
+ if (deduction_guide_p (olddecl)
+         && deduction_guide_p (newdecl))
+       return G_("deduction guide %q+D redeclared");
+
        /* [class.compare.default]: A definition of a comparison operator as
         defaulted that appears in a class shall be the first declaration of
         that function.  */
@@ -3053,24 +3057,28 @@ redeclaration_error_message (tree newdecl, tree olddecl)
                          "%<gnu_inline%> attribute");
              else
                return G_("%q+D redeclared inline without "
-                         "%<gnu_inline%> attribute");
+                         "%<gnu_inline%> attribute");
            }
        }
- /* Core issue #226 (C++0x):
-
+      if (deduction_guide_p (olddecl)
+         && deduction_guide_p (newdecl))
+       return G_("deduction guide %q+D redeclared");
+
+      /* Core issue #226 (C++11):
+
             If a friend function template declaration specifies a
             default template-argument, that declaration shall be a
             definition and shall be the only declaration of the
             function template in the translation unit.  */
-      if ((cxx_dialect != cxx98)
+      if ((cxx_dialect != cxx98)
            && TREE_CODE (ot) == FUNCTION_DECL && DECL_FRIEND_P (ot)
-          && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
+         && !check_default_tmpl_args (nt, DECL_TEMPLATE_PARMS (newdecl),
                                         /*is_primary=*/true,
                                       /*is_partial=*/false,
                                         /*is_friend_decl=*/2))
          return G_("redeclaration of friend %q#D "
-                 "may not have default template arguments");
+                 "may not have default template arguments");
return NULL;
      }
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C 
b/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C
new file mode 100644
index 00000000000..fe113819a95
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction74.C
@@ -0,0 +1,31 @@
+// PR c++/97099
+// { dg-do compile { target c++17 } }
+// [temp.deduct.guide]p3: Two deduction guide declarations in the same
+// translation unit for the same class template shall not have equivalent
+// parameter-declaration-clauses.
+
+template<typename> struct S { };
+template<typename> struct X { };
+
+S() -> S<int>; // { dg-message "previously declared here|old declaration" }
+S() -> S<int>; // { dg-error "redeclared" }
+X() -> X<int>;
+S() -> S<float>; // { dg-error "ambiguating new declaration of" }
+
+S(bool) -> S<int>; // { dg-message "previously declared here" }
+explicit S(bool) -> S<int>; // { dg-error "redeclared" }
+
+explicit S(char) -> S<int>; // { dg-message "previously declared here" }
+S(char) -> S<int>; // { dg-error "redeclared" }
+
+template<typename T> S(T, T) -> S<int>; // { dg-message "previously declared 
here" }
+template<typename T> X(T, T) -> X<int>;
+template<typename T> S(T, T) -> S<int>; // { dg-error "redeclared" }
+
+// OK: Use SFINAE.
+template<typename T> S(T) -> S<typename T::foo>;
+template<typename T> S(T) -> S<typename T::bar>;
+
+// OK: Non-template wins.
+S(int) -> S<int>;
+template<typename T = int> S(int) -> S<char>;

base-commit: 4a5ff2b56bfea0b3e154a15e809c5c42dc3b9e9f


Reply via email to