]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Check if allocation functions are xobj members [PR114078]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 20 Apr 2024 05:08:02 +0000 (15:08 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 23 Apr 2024 05:59:54 +0000 (15:59 +1000)
A class allocation member function is implicitly 'static' by
[class.free] p3, so cannot have an explicit object parameter.

PR c++/114078

gcc/cp/ChangeLog:

* decl.cc (grokdeclarator): Check allocation functions for xobj
parameters.

gcc/testsuite/ChangeLog:

* g++.dg/cpp23/explicit-obj-ops-alloc.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/decl.cc
gcc/testsuite/g++.dg/cpp23/explicit-obj-ops-alloc.C [new file with mode: 0644]

index 65ab64885ff8bbf85c14eed8a7ae572c3fcb73cf..2af026d255d70dc94a0e449b89743997637140aa 100644 (file)
@@ -13728,6 +13728,12 @@ grokdeclarator (const cp_declarator *declarator,
                        inform (DECL_SOURCE_LOCATION (xobj_parm),
                                "explicit object parameter declared here");
                      }
+                   if (unqualified_id
+                       && identifier_p (unqualified_id)
+                       && IDENTIFIER_NEWDEL_OP_P (unqualified_id))
+                     error_at (DECL_SOURCE_LOCATION (xobj_parm),
+                               "%qD cannot be an explicit object member "
+                               "function", unqualified_id);
                  }
              }
            tree pushed_scope = NULL_TREE;
diff --git a/gcc/testsuite/g++.dg/cpp23/explicit-obj-ops-alloc.C b/gcc/testsuite/g++.dg/cpp23/explicit-obj-ops-alloc.C
new file mode 100644 (file)
index 0000000..8a277db
--- /dev/null
@@ -0,0 +1,11 @@
+// PR c++/114078
+// { dg-do compile { target c++23 } }
+
+using size_t = decltype(sizeof(0));
+
+struct S {
+  void* operator new(this size_t);  // { dg-error "explicit object" }
+  void* operator new[](this size_t);  // { dg-error "explicit object" }
+  void operator delete(this void*);  // { dg-error "explicit object" }
+  void operator delete[](this void*);  // { dg-error "explicit object" }
+};