Skip to content

[libc++] Verifies std::forward_like's mandates clause. #127318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 27, 2025

Conversation

mordante
Copy link
Member

@mordante mordante commented Feb 15, 2025

The existing using _ForwardLike declaration already fails with a subsitution failure. The LWG issue was filed to clarify what should happen for non-referencable types.

Added test to verify libc++ is already enforcing the new Mandates.

Implements:

  • LWG3757 What's the effect of std::forward_like(x)

Closes: #105026

The existing using _ForwardLike declaration already fails with a
subsitution failure. The LWG issue was filed to clarify what should happen
for non-referencable types.

Added test to verify libc++ is alrady enforcing the new mandates.

Implements:
- LWG3757 What's the effect of std::forward_like<void>(x)

Closes: llvm#105026
@mordante mordante requested a review from a team as a code owner February 15, 2025 12:54
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Feb 15, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 15, 2025

@llvm/pr-subscribers-libcxx

Author: Mark de Wever (mordante)

Changes

The existing using _ForwardLike declaration already fails with a subsitution failure. The LWG issue was filed to clarify what should happen for non-referencable types.

Added test to verify libc++ is alrady enforcing the new mandates.

Implements:

  • LWG3757 What's the effect of std::forward_like<void>(x)

Closes: #105026


Full diff: https://github.com/llvm/llvm-project/pull/127318.diff

2 Files Affected:

  • (modified) libcxx/docs/Status/Cxx23Issues.csv (+1-1)
  • (added) libcxx/test/std/utilities/utility/forward/forward_like.verify.cpp (+46)
diff --git a/libcxx/docs/Status/Cxx23Issues.csv b/libcxx/docs/Status/Cxx23Issues.csv
index 1215f21985eb9..6471fdbf069a3 100644
--- a/libcxx/docs/Status/Cxx23Issues.csv
+++ b/libcxx/docs/Status/Cxx23Issues.csv
@@ -213,7 +213,7 @@
 "`LWG3753 <https://wg21.link/LWG3753>`__","Clarify entity vs. freestanding entity","2022-11 (Kona)","","",""
 "`LWG3754 <https://wg21.link/LWG3754>`__","Class template expected synopsis contains declarations that do not match the detailed description","2022-11 (Kona)","|Nothing To Do|","",""
 "`LWG3755 <https://wg21.link/LWG3755>`__","``tuple-for-each`` can call ``user-defined`` ``operator,``","2022-11 (Kona)","|Complete|","17",""
-"`LWG3757 <https://wg21.link/LWG3757>`__","What's the effect of ``std::forward_like<void>(x)``?","2022-11 (Kona)","","",""
+"`LWG3757 <https://wg21.link/LWG3757>`__","What's the effect of ``std::forward_like<void>(x)``?","2022-11 (Kona)","|Nothing To Do|","",""
 "`LWG3759 <https://wg21.link/LWG3759>`__","``ranges::rotate_copy`` should use ``std::move``","2022-11 (Kona)","|Complete|","15",""
 "`LWG3760 <https://wg21.link/LWG3760>`__","``cartesian_product_view::iterator``'s ``parent_`` is never valid","2022-11 (Kona)","","",""
 "`LWG3761 <https://wg21.link/LWG3761>`__","``cartesian_product_view::iterator::operator-`` should pass by reference","2022-11 (Kona)","","",""
diff --git a/libcxx/test/std/utilities/utility/forward/forward_like.verify.cpp b/libcxx/test/std/utilities/utility/forward/forward_like.verify.cpp
new file mode 100644
index 0000000000000..279f0e60a1c38
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/forward/forward_like.verify.cpp
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// REQUIRES: std-at-least-c++23
+
+// <utility>
+
+// template <typename T>
+// [[nodiscard]] constexpr
+// auto forward_like(auto&& x) noexcept -> see below;
+
+// Mandates: T is a referenceable type (3.45 [defns.referenceable]).
+
+#include <utility>
+
+struct incomplete;
+
+void test() {
+  int i;
+  (void)std::forward_like<incomplete>(i);
+
+  (void)std::forward_like<void>(i);                // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<const void>(i);          // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<volatile void>(i);       // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<const volatile void>(i); // expected-error {{no matching function for call to 'forward_like'}}
+
+  using fp   = void();
+  using cfp  = void() const;
+  using vfp  = void() volatile;
+  using cvfp = void() const volatile;
+  (void)std::forward_like<fp>(i);
+  (void)std::forward_like<cfp>(i);  // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<cfp>(i);  // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<vfp>(i);  // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<cvfp>(i); // expected-error {{no matching function for call to 'forward_like'}}
+
+  using fpr  = void()&;
+  using fprr = void()&&;
+  (void)std::forward_like<fpr>(i);  // expected-error {{no matching function for call to 'forward_like'}}
+  (void)std::forward_like<fprr>(i); // expected-error {{no matching function for call to 'forward_like'}}
+}

Copy link
Contributor

@philnik777 philnik777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we test all of this with SFINAE tests instead?

@mordante
Copy link
Member Author

Can't we test all of this with SFINAE tests instead?

I don't think we can. With SFINAE we test a constraint clause and this is a mandates clause.

@philnik777
Copy link
Contributor

Can't we test all of this with SFINAE tests instead?

I don't think we can. With SFINAE we test a constraint clause and this is a mandates clause.

Yeah, I'm actually not convinced this is "nothing to do". We're currently SFINAEing away AFAICT, but the standard asks us to reject the code out right.

@frederick-vs-ja
Copy link
Contributor

Can't we test all of this with SFINAE tests instead?

I don't think we can. With SFINAE we test a constraint clause and this is a mandates clause.

Yeah, I'm actually not convinced this is "nothing to do". We're currently SFINAEing away AFAICT, but the standard asks us to reject the code out right.

Since there's only one std::forward_like overload in the standard library (true for both standard wording and implementations), as long as one writes std::forward_like, it's not observable whether Mandates is implemented in the same way as Constraints.

It's not very clear to me what should happen when user-provided overloads are involved...

@mordante
Copy link
Member Author

Can't we test all of this with SFINAE tests instead?

I don't think we can. With SFINAE we test a constraint clause and this is a mandates clause.

Yeah, I'm actually not convinced this is "nothing to do". We're currently SFINAEing away AFAICT, but the standard asks us to reject the code out right.

Since there's only one std::forward_like overload in the standard library (true for both standard wording and implementations), as long as one writes std::forward_like, it's not observable whether Mandates is implemented in the same way as Constraints.

Indeed, that's why I added tests to make sure the SFINAE acts as a mandates.

It's not very clear to me what should happen when user-provided overloads are involved...

AFAIK they are not allowed to do that.

@frederick-vs-ja
Copy link
Contributor

It's not very clear to me what should happen when user-provided overloads are involved...

AFAIK they are not allowed to do that.

Oh, sorry for causing misunderstanding. I meant something like the following, not user-provided std::forward_like:

using std::forward_like;
using nonstd::forward_like;
forward_like<void>(x); // Should std::forward_like catch this and cause error?

@philnik777
Copy link
Contributor

Can't we test all of this with SFINAE tests instead?

I don't think we can. With SFINAE we test a constraint clause and this is a mandates clause.

Yeah, I'm actually not convinced this is "nothing to do". We're currently SFINAEing away AFAICT, but the standard asks us to reject the code out right.

Since there's only one std::forward_like overload in the standard library (true for both standard wording and implementations), as long as one writes std::forward_like, it's not observable whether Mandates is implemented in the same way as Constraints.

Indeed, that's why I added tests to make sure the SFINAE acts as a mandates.

Can we instead refactor the code to not SFINAE away? I'd be much happier with that, since it avoids portability issues and static_asserts are generally less heavy than SFINAE.

@mordante
Copy link
Member Author

mordante commented Mar 2, 2025

Indeed, that's why I added tests to make sure the SFINAE acts as a mandates.

Can we instead refactor the code to not SFINAE away? I'd be much happier with that, since it avoids portability issues and static_asserts are generally less heavy than SFINAE.

What portability issues do you expect?

I had a look at your suggestion and it's not that trivial, adding a constrained overload with a dummy return type fails due to Clang being unhappy with the function signature. Clang has some diagnostics related to certain Standard library functions and types.

Copy link
Member

@ldionne ldionne left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think Nikolas' point is that if we implement a Mandates as SFINAE, that's something that users can rely on to disable their own overloads. But if we switched to a static_assert instead, their code would start failing with a hard error. So in that way, implementing a Mandates with a static_assert is stricter and "closer to the strict interpretation of the spec".

I would also rather implement this Mandates as a static_assert if we can: for example we could add the following to the body of __forward_like:

static_assert(__is_referenceable_v<_Tp>, "some nice message");

But in practice, the signature of forward_like itself means that a non-referenceable type is always going to result in forward_like SFINAE-ing away. So I don't see a way to improve the situation and even adding a static_assert to the body of the function wouldn't help, since we'd never reach it.

In light of that, this patch LGTM.

@ldionne ldionne merged commit f9aa7a2 into llvm:main Mar 27, 2025
79 checks passed
@mordante mordante deleted the review/LWG3757 branch March 27, 2025 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LWG3757: What's the effect of std::forward_like<void>(x)?
5 participants