Tested powerpc64le-linux, pushed to trunk.

The new deleted constructors added by P2166R1 are a breaking change,
making previously valid code ill-formed in C++23. As a result, they
should only be defined for C++23 and not for C++11 and up.

libstdc++-v3/ChangeLog:

        PR libstdc++/104099
        * include/bits/basic_string.h (basic_string(nullptr_t)): Only
        define for C++23.
        (operator=(nullptr_t)): Likewise.
        * include/bits/cow_string.h: Likewise.
        * include/std/string_view (basic_string_view(nullptr_t)):
        Likewise.
        * testsuite/21_strings/basic_string/cons/char/nullptr.cc: Adjust
        expected error. Add examples that become ill-formed in C++23.
        * testsuite/21_strings/basic_string_view/cons/char/nonnull.cc:
        Adjust expected errors.
        * testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc:
        Likewise.
---
 libstdc++-v3/include/bits/basic_string.h      |  4 ++-
 libstdc++-v3/include/bits/cow_string.h        |  4 ++-
 libstdc++-v3/include/std/string_view          |  3 ++-
 .../basic_string/cons/char/nullptr.cc         | 26 ++++++++++++++++++-
 .../basic_string_view/cons/char/nonnull.cc    |  3 ++-
 .../basic_string_view/cons/wchar_t/nonnull.cc |  3 ++-
 6 files changed, 37 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/include/bits/basic_string.h 
b/libstdc++-v3/include/bits/basic_string.h
index a91ba5114b1..fc6a303a957 100644
--- a/libstdc++-v3/include/bits/basic_string.h
+++ b/libstdc++-v3/include/bits/basic_string.h
@@ -728,10 +728,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
        else
          _M_construct(__str.begin(), __str.end(), std::forward_iterator_tag());
       }
+#endif // C++11
 
+#if __cplusplus >= 202100L
       basic_string(nullptr_t) = delete;
       basic_string& operator=(nullptr_t) = delete;
-#endif // C++11
+#endif // C++23
 
       /**
        *  @brief  Construct string as copy of a range.
diff --git a/libstdc++-v3/include/bits/cow_string.h 
b/libstdc++-v3/include/bits/cow_string.h
index 84aab2f33c6..a49a5b04f2f 100644
--- a/libstdc++-v3/include/bits/cow_string.h
+++ b/libstdc++-v3/include/bits/cow_string.h
@@ -665,10 +665,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        else
          _M_dataplus._M_p = _S_construct(__str.begin(), __str.end(), __a);
       }
+#endif // C++11
 
+#if __cplusplus >= 202100L
       basic_string(nullptr_t) = delete;
       basic_string& operator=(nullptr_t) = delete;
-#endif // C++11
+#endif // C++23
 
       /**
        *  @brief  Construct string as copy of a range.
diff --git a/libstdc++-v3/include/std/string_view 
b/libstdc++-v3/include/std/string_view
index 99080e9eccd..bccf4d1847f 100644
--- a/libstdc++-v3/include/std/string_view
+++ b/libstdc++-v3/include/std/string_view
@@ -167,10 +167,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
        noexcept(noexcept(ranges::size(__r)) && noexcept(ranges::data(__r)))
        : _M_len(ranges::size(__r)), _M_str(ranges::data(__r))
        { }
+
+      basic_string_view(nullptr_t) = delete;
 #endif // C++23
 #endif // C++20
 
-      basic_string_view(nullptr_t) = delete;
 
       constexpr basic_string_view&
       operator=(const basic_string_view&) noexcept = default;
diff --git 
a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc 
b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
index fdb24aeeb89..a69fa614ba3 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string/cons/char/nullptr.cc
@@ -1,4 +1,28 @@
 // { dg-do compile { target c++11 } }
 #include <string>
 
-std::string s = nullptr; // { dg-error "deleted" "P2166R1" }
+std::string s = nullptr; // { dg-error "deleted" "P2166R1" { target c++23 } }
+
+struct S
+{
+  operator const char*() const { return ""; }
+  operator std::nullptr_t() const { return {}; }
+};
+
+std::string s2{ S{} }; // { dg-error "deleted" "PR 104099" { target c++23 } }
+
+#if __cpp_concepts
+struct J
+{
+  // In C++20 this selects basic_string(const char*),
+  // in C++23 it's ambiguous due to basic_string(nullptr_t).
+  template<typename T>
+    requires (!std::is_same_v<std::allocator<char>, T>)
+    && (!std::is_same_v<std::string, T>)
+    && (!std::is_same_v<char, T>)
+    && (!std::is_same_v<std::string_view, T>)
+    operator T() const { return {}; }
+};
+
+std::string s3{ J{} }; // { dg-error "ambiguous" "PR 104099" { target c++23 } }
+#endif
diff --git 
a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc 
b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
index 00bb8e414b8..2e43788e4f5 100644
--- a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
+++ b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/char/nonnull.cc
@@ -25,5 +25,6 @@ test01()
 {
   std::string_view s((const char*)nullptr); // { dg-warning "\\\[-Wnonnull" }
   std::string_view t((char*)nullptr);      // { dg-warning "\\\[-Wnonnull" }
-  std::string_view u(nullptr);             // { dg-error "deleted" }
+  std::string_view u(nullptr);             // { dg-warning "\\\[-Wnonnull" "" 
{ target c++20_down } }
+// { dg-error "deleted" "P2166R1" { target c++23 } 0 }
 }
diff --git 
a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc 
b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
index 685d48c3fd7..a146d383324 100644
--- 
a/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
+++ 
b/libstdc++-v3/testsuite/21_strings/basic_string_view/cons/wchar_t/nonnull.cc
@@ -25,5 +25,6 @@ test01()
 {
   std::wstring_view s((const wchar_t*)nullptr);        // { dg-warning 
"\\\[-Wnonnull" }
   std::wstring_view t((wchar_t*)nullptr);      // { dg-warning "\\\[-Wnonnull" 
}
-  std::wstring_view u(nullptr);                        // { dg-error "deleted" 
}
+  std::wstring_view u(nullptr);                        // { dg-warning 
"\\\[-Wnonnull" "" { target c++20_down } }
+// { dg-error "deleted" "P2166R1" { target c++23 } 0 }
 }
-- 
2.31.1

Reply via email to