See https://godbolt.org/z/v8arThEs1. ```C++ #include <type_traits> template<class T> class X { friend auto f(X); }; template<class T, class = std::void_t<>> struct downcast_t { using type = void; }; template<class T> struct downcast_t<T, std::void_t<decltype(f(X<T>{}))>> { using type = decltype(f(X<T>{})); }; template<class T> using downcast = typename downcast_t<T>::type; static_assert(std::is_same_v<void, downcast<int>>); struct Y : X<long> { friend auto f(X) { return 0L; } }; static_assert(std::is_same_v<long, downcast<long>>); ``` ``` <source>:4:18: warning: friend declaration 'auto f(X<T>)' declares a non-template function [-Wnon-template-friend] 4 | friend auto f(X); | ^ <source>:4:18: note: (if this is not what you intended, make sure the function template has already been declared and add '<>' after the function name here) <source>:15:20: internal compiler error: in push_template_decl, at cp/pt.cc:6125 15 | friend auto f(X) { return 0L; } | ^ 0x21ffa39 internal_error(char const*, ...) ???:0 0x7452f3 fancy_abort(char const*, int, char const*) ???:0 0x82a0cd start_preparsed_function(tree_node*, tree_node*, int) ???:0 0x98cb8d c_parse_file() ???:0 0xb21d91 c_common_parse_file() ???:0 Please submit a full bug report, with preprocessed source (by using -freport-bug). Please include the complete backtrace with any bug report. See <https://gcc.gnu.org/bugs/> for instructions. ```
Simplified: https://godbolt.org/z/66Gd84dos. ```C++ template <class T> class X { friend auto f(X); }; struct Y : X<long> { friend auto f(X) { return 0L; } }; ```
Started with r11-388-gc4bff4c230c8d341.
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:284ae8b46f5eef74c0b660a87a7169497f559e73 commit r13-985-g284ae8b46f5eef74c0b660a87a7169497f559e73 Author: Jason Merrill <jason@redhat.com> Date: Fri Jun 3 12:35:12 2022 -0400 c++: redeclared hidden friend [PR105761] Here, when we see the second declaration of f we match it with the first one, copy over DECL_TEMPLATE_INFO, and then try to use it when parsing the definition, leading to confusion. PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Don't copy DECL_TEMPLATE_INFO from a hidden friend. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/auto-fn64.C: New test.
The releases/gcc-12 branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:2843bfa21073dd1ac222540e189e8bcf40afc2c0 commit r12-8456-g2843bfa21073dd1ac222540e189e8bcf40afc2c0 Author: Jason Merrill <jason@redhat.com> Date: Fri Jun 3 12:35:12 2022 -0400 c++: redeclared hidden friend [PR105761] Here, when we see the second declaration of f we match it with the first one, copy over DECL_TEMPLATE_INFO, and then try to use it when parsing the definition, leading to confusion. PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Don't copy DECL_TEMPLATE_INFO from a hidden friend. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/auto-fn64.C: New test.
The releases/gcc-11 branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:c27cbeb985f7b6e1759399971b4fe2773e91ca71 commit r11-10047-gc27cbeb985f7b6e1759399971b4fe2773e91ca71 Author: Jason Merrill <jason@redhat.com> Date: Fri Jun 3 12:35:12 2022 -0400 c++: redeclared hidden friend [PR105761] Here, when we see the second declaration of f we match it with the first one, copy over DECL_TEMPLATE_INFO, and then try to use it when parsing the definition, leading to confusion. PR c++/105761 gcc/cp/ChangeLog: * decl.c (duplicate_decls): Don't copy DECL_TEMPLATE_INFO from a hidden friend. gcc/testsuite/ChangeLog: * g++.dg/cpp1y/auto-fn64.C: New test.
Fixed.
The master branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:7d87790a871482e9c5142a8e885b4a5f76d197c8 commit r13-1017-g7d87790a871482e9c5142a8e885b4a5f76d197c8 Author: Jason Merrill <jason@redhat.com> Date: Mon Jun 6 21:49:06 2022 -0400 c++: redeclared hidden friend take 2 [PR105852] My previous patch for 105761 avoided copying DECL_TEMPLATE_INFO from a friend to a later definition, but in this testcase we have first a non-friend declaration and then a definition, and we need to avoid copying in that case as well. But we do still want to set new_template_info to avoid GC trouble. With this change, the modules dump correctly identifies ::foo as a non-template function in tpl-friend-2_a.C. Along the way I noticed that the duplicate_decls handling of DECL_UNIQUE_FRIEND_P was backwards for templates, where we don't clobber DECL_LANG_SPECIFIC (olddecl) with DECL_LANG_SPECIFIC (newdecl) like we do for non-templates. PR c++/105852 PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Avoid copying template info from non-templated friend even if newdecl isn't a definition. Correct handling of DECL_UNIQUE_FRIEND_P on templates. * pt.cc (non_templated_friend_p): New. * cp-tree.h (non_templated_friend_p): Declare it. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-2_a.C: Adjust expected dump. * g++.dg/template/friend74.C: New test.
The releases/gcc-12 branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:e057d454db4dcf48c22f75e57599f797d8e55baf commit r12-8467-ge057d454db4dcf48c22f75e57599f797d8e55baf Author: Jason Merrill <jason@redhat.com> Date: Mon Jun 6 21:49:06 2022 -0400 c++: redeclared hidden friend take 2 [PR105852] My previous patch for 105761 avoided copying DECL_TEMPLATE_INFO from a friend to a later definition, but in this testcase we have first a non-friend declaration and then a definition, and we need to avoid copying in that case as well. But we do still want to set new_template_info to avoid GC trouble. With this change, the modules dump correctly identifies ::foo as a non-template function in tpl-friend-2_a.C. Along the way I noticed that the duplicate_decls handling of DECL_UNIQUE_FRIEND_P was backwards for templates, where we don't clobber DECL_LANG_SPECIFIC (olddecl) with DECL_LANG_SPECIFIC (newdecl) like we do for non-templates. PR c++/105852 PR c++/105761 gcc/cp/ChangeLog: * decl.cc (duplicate_decls): Avoid copying template info from non-templated friend even if newdecl isn't a definition. Correct handling of DECL_UNIQUE_FRIEND_P on templates. * pt.cc (non_templated_friend_p): New. * cp-tree.h (non_templated_friend_p): Declare it. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-2_a.C: Adjust expected dump. * g++.dg/template/friend74.C: New test.
The releases/gcc-11 branch has been updated by Jason Merrill <jason@gcc.gnu.org>: https://gcc.gnu.org/g:3e39a0a4b71dce674ee30fe71590b0152663b6d8 commit r11-10055-g3e39a0a4b71dce674ee30fe71590b0152663b6d8 Author: Jason Merrill <jason@redhat.com> Date: Mon Jun 6 21:49:06 2022 -0400 c++: redeclared hidden friend take 2 [PR105852] My previous patch for 105761 avoided copying DECL_TEMPLATE_INFO from a friend to a later definition, but in this testcase we have first a non-friend declaration and then a definition, and we need to avoid copying in that case as well. But we do still want to set new_template_info to avoid GC trouble. With this change, the modules dump correctly identifies ::foo as a non-template function in tpl-friend-2_a.C. Along the way I noticed that the duplicate_decls handling of DECL_UNIQUE_FRIEND_P was backwards for templates, where we don't clobber DECL_LANG_SPECIFIC (olddecl) with DECL_LANG_SPECIFIC (newdecl) like we do for non-templates. PR c++/105852 PR c++/105761 gcc/cp/ChangeLog: * decl.c (duplicate_decls): Avoid copying template info from non-templated friend even if newdecl isn't a definition. Correct handling of DECL_UNIQUE_FRIEND_P on templates. * pt.c (non_templated_friend_p): New. * cp-tree.h (non_templated_friend_p): Declare it. gcc/testsuite/ChangeLog: * g++.dg/modules/tpl-friend-2_a.C: Adjust expected dump. * g++.dg/template/friend74.C: New test.