From: Lunex <[email protected]>

a friend which is not a member but 'this Self &&' was used on that that
is only for members. GCC faild to check and later it crashed with no access
info to check.

now we just reject only [non-member]friend that uses an explicit object 
parameter.

gcc/cp/ChangeLog:

        PR c++/122258
        * decl.cc (grokdeclarator): Extend non-member explicit object
        parameter check to include unqualified friends and clear
        is_xobj_member_function.

gcc/testsuite/ChangeLog:

        PR c++/122258
        * g++.dg/cpp23/explicit-obj-diagnostics13.C: New test.
---
 gcc/cp/decl.cc                                | 13 ++--
 .../g++.dg/cpp23/explicit-obj-diagnostics13.C | 63 +++++++++++++++++++
 2 files changed, 72 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics13.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 55badc9ee..2912f2c0f 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -15666,13 +15666,18 @@ grokdeclarator (const cp_declarator *declarator,
 
            if (xobj_parm)
              {
-               if (!ctype
+               if ((!ctype
                    && decl_context == NORMAL
                    && (in_namespace
                        || !declarator->declarator->u.id.qualifying_scope))
-                 error_at (DECL_SOURCE_LOCATION (xobj_parm),
-                           "a non-member function cannot have "
-                           "an explicit object parameter");
+                   || (friendp
+                       && !declarator->declarator->u.id.qualifying_scope))
+                 {
+                   error_at (DECL_SOURCE_LOCATION (xobj_parm),
+                             "a non-member function cannot have "
+                             "an explicit object parameter");
+                   is_xobj_member_function = false;
+                 }
                else
                  {
                    if (virtualp)
diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics13.C 
b/gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics13.C
new file mode 100644
index 000000000..b0791045b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp23/explicit-obj-diagnostics13.C
@@ -0,0 +1,63 @@
+// P0847R7
+// { dg-do compile { target c++23 } }
+
+#include <tuple>
+
+// PR c++/122258 - friend with explicit object param must be rejected
+
+struct type {
+  template<unsigned I, class Self>
+  friend auto get(this Self &&self) { return 0; } // { dg-error "a non-member 
function cannot have an explicit object parameter" }
+};
+
+struct S {
+  friend void f(this S &); // { dg-error "a non-member function cannot have an 
explicit object parameter" }
+  friend void g(this S &&); // { dg-error "a non-member function cannot have 
an explicit object parameter" }
+};
+
+struct U {
+  template<class Self>
+  friend void h(this Self &&); // { dg-error "a non-member function cannot 
have an explicit object parameter" }
+  template<class Self>
+  friend void k(this Self &&) {} // { dg-error "a non-member function cannot 
have an explicit object parameter" }
+};
+
+struct V {
+  friend void m(this V &) {} // { dg-error "a non-member function cannot have 
an explicit object parameter" }
+};
+
+struct Mem {
+  void mem(this Mem&);
+};
+
+struct A {
+  friend void Mem::mem(this Mem&);
+};
+
+struct B {
+  friend void Mem::mem(this Mem&);
+};
+
+struct C {
+  friend void Mem::mem(this Mem&);
+};
+
+namespace std {
+template<> struct tuple_size<type> : integral_constant<size_t, 1> {};
+template<size_t I> struct tuple_element<I, type> { using type = int; };
+}
+
+void test_sb()
+{
+  auto [a] = type();
+}
+
+void test_friend_member(void)
+{
+  struct Mem2 {
+    void mem(this Mem2&);
+  };
+  struct A {
+    friend void Mem2::mem(this Mem2&);
+  };
+}
-- 
2.54.0

Reply via email to