diff options
author | Marek Polacek <polacek@redhat.com> | 2020-09-18 19:37:05 -0400 |
---|---|---|
committer | Marek Polacek <polacek@redhat.com> | 2020-09-21 10:11:41 -0400 |
commit | b6ff694e592669e7865d39a884100dd677e7ceec (patch) | |
tree | fe5ac97074119251d228e9f5a028978e1914f806 /gcc | |
parent | 2ec58cfcea146a61755516ce4ed160827fe0b4ff (diff) | |
download | gcc-b6ff694e592669e7865d39a884100dd677e7ceec.zip gcc-b6ff694e592669e7865d39a884100dd677e7ceec.tar.gz gcc-b6ff694e592669e7865d39a884100dd677e7ceec.tar.bz2 |
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.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/decl.c | 20 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp1z/class-deduction74.C | 31 |
2 files changed, 45 insertions, 6 deletions
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index 13f065d..af79649 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 0000000..fe11381 --- /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>; |