[libcxx-commits] [libcxx] [libcxx][test][NFC] Fix std::pair convertible tests in light of CWG2137 (PR #97403)

Mital Ashok via libcxx-commits libcxx-commits at lists.llvm.org
Tue Jul 2 03:07:56 PDT 2024


https://github.com/MitalAshok created https://github.com/llvm/llvm-project/pull/97403

https://cplusplus.github.io/CWG/issues/2137.html

This change was previously made as part of 924701311aa79180e86ad8ce43d253f27d25ec7d (#77768) and later reverted in 6e4930c67508a90bdfd756f6e45417b5253cd741

This change is still needed because the comment is still true: A standards-conformant compiler is currently supposed to fail this test.

This also means that any future work on CWG2137 with Clang would not need to modify the libc++ test suite


>From bfbbee350dae7eae8c16551afd5fcedc035d3685 Mon Sep 17 00:00:00 2001
From: Mital Ashok <mital at mitalashok.co.uk>
Date: Tue, 2 Jul 2024 10:51:43 +0100
Subject: [PATCH] [libcxx][test][NFC] Fix std::pair convertible tests in light
 of CWG2137

---
 .../pairs.pair/ctor.pair_U_V_move.pass.cpp    | 23 ++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)

diff --git a/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp
index 3b2d093eb34d4..8ba9b5696eff1 100644
--- a/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp
+++ b/libcxx/test/std/utilities/utility/pairs/pairs.pair/ctor.pair_U_V_move.pass.cpp
@@ -121,7 +121,28 @@ int main(int, char**)
         test_pair_rv<CopyOnly, CopyOnly&>();
         test_pair_rv<CopyOnly, CopyOnly&&>();
 
-        test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly>();
+        /* For ExplicitTypes::CopyOnly, two of the viable candidates for initializing from a non-const xvalue are:
+         *   pair(const pair&);  // (defaulted copy constructor)
+         *   template<class U1, class U2> explicit pair(const pair<U1, U2>&&); [U1 = ExplicitTypes::CopyOnly, U2 = int]
+         *
+         * This results in diverging behavior for test_convertible which uses copy-list-initialization.
+         * Prior to CWG2137, this would have selected the first (non-explicit) ctor as explicit ctors
+         * would not be considered. Afterwards, it should select the second since it is a better match,
+         * and then failed because it is explicit.
+         *
+         * This may change with future defect reports, and some compilers only have partial support
+         * for CWG2137, so use std::is_convertible directly to avoid a copy-list-initialization
+         */
+        {
+          using P1  = std::pair<ExplicitTypes::CopyOnly, int>;
+          using P2  = std::pair<int, ExplicitTypes::CopyOnly>;
+          using UP1 = std::pair<ExplicitTypes::CopyOnly, int>&&;
+          using UP2 = std::pair<int, ExplicitTypes::CopyOnly>&&;
+          static_assert(std::is_constructible<P1, UP1>::value, "");
+          static_assert(std::is_convertible<P1, UP1>::value, "");
+          static_assert(std::is_constructible<P2, UP2>::value, "");
+          static_assert(std::is_convertible<P2, UP2>::value, "");
+        }
         test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&, true, false>();
         test_pair_rv<ExplicitTypes::CopyOnly, ExplicitTypes::CopyOnly&&, true, false>();
 



More information about the libcxx-commits mailing list