From b6ff694e592669e7865d39a884100dd677e7ceec Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Fri, 18 Sep 2020 19:37:05 -0400 Subject: [PATCH] c++: Detect deduction guide redeclaration [PR97099] [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. 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) "% attribute"); else return G_("%q+D redeclared inline without " - "% attribute"); + "% 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 struct S { }; +template struct X { }; + +S() -> S; // { dg-message "previously declared here|old declaration" } +S() -> S; // { dg-error "redeclared" } +X() -> X; +S() -> S; // { dg-error "ambiguating new declaration of" } + +S(bool) -> S; // { dg-message "previously declared here" } +explicit S(bool) -> S; // { dg-error "redeclared" } + +explicit S(char) -> S; // { dg-message "previously declared here" } +S(char) -> S; // { dg-error "redeclared" } + +template S(T, T) -> S; // { dg-message "previously declared here" } +template X(T, T) -> X; +template S(T, T) -> S; // { dg-error "redeclared" } + +// OK: Use SFINAE. +template S(T) -> S; +template S(T) -> S; + +// OK: Non-template wins. +S(int) -> S; +template S(int) -> S; -- 2.30.2