[libc++] Use addressof in forward_list.

This addresses the usage of `operator&` in `<forward_list>`.

(Note there are still more headers with the same issue.)

Reviewed By: #libc, Quuxplusone, ldionne

Differential Revision: https://reviews.llvm.org/D112660

GitOrigin-RevId: f7345de64fd2d6c5528e940a8aed1a9b95b46183
diff --git a/include/forward_list b/include/forward_list
index 717dc90..41059a5 100644
--- a/include/forward_list
+++ b/include/forward_list
@@ -1587,7 +1587,7 @@
 void
 forward_list<_Tp, _Alloc>::merge(forward_list& __x, _Compare __comp)
 {
-    if (this != &__x)
+    if (this != _VSTD::addressof(__x))
     {
         base::__before_begin()->__next_ = __merge(base::__before_begin()->__next_,
                                                     __x.__before_begin()->__next_,
diff --git a/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp
new file mode 100644
index 0000000..9542a7b
--- /dev/null
+++ b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue.addressof.compile.pass.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list& x);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(l);
+}
diff --git a/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp
new file mode 100644
index 0000000..012bbce
--- /dev/null
+++ b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_lvalue_pred.addressof.compile.pass.cpp
@@ -0,0 +1,24 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list& x, Compare comp);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(l, std::less<operator_hijacker>());
+}
diff --git a/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp
new file mode 100644
index 0000000..9955bed
--- /dev/null
+++ b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue.addressof.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list&& x);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(std::move(l));
+}
diff --git a/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp
new file mode 100644
index 0000000..6a33cf0
--- /dev/null
+++ b/test/std/containers/sequences/forwardlist/forwardlist.ops/merge_rvalue_pred.addressof.compile.pass.cpp
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03
+
+// <forward_list>
+
+// template <class Compare> void merge(forward_list&& x, Compare comp);
+
+// Validate whether the operation properly guards against ADL-hijacking operator&
+
+#include <forward_list>
+
+#include "test_macros.h"
+#include "operator_hijacker.h"
+
+void test() {
+  std::forward_list<operator_hijacker> lo;
+  std::forward_list<operator_hijacker> l;
+  lo.merge(std::move(l), std::less<operator_hijacker>());
+}