Skip to content

[libc++][test] Test nasty_string in C++20 #135338

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

Conversation

frederick-vs-ja
Copy link
Contributor

@frederick-vs-ja frederick-vs-ja commented Apr 11, 2025

It seems that we can only rely on C++20 features and make nasty_string also tested for MSVC STL.

It seems that we can only rely on C++20 features and make
`nasty_string` also tested for MSVC STL.

Drive-by: Removes an unnecessary deduction guide.
@frederick-vs-ja frederick-vs-ja requested a review from a team as a code owner April 11, 2025 09:50
@llvmbot llvmbot added the libc++ libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi. label Apr 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Apr 11, 2025

@llvm/pr-subscribers-libcxx

Author: A. Jiang (frederick-vs-ja)

Changes

It seems that we can only rely on C++20 features and make nasty_string also tested for MSVC STL.

Drive-by: Removes an unnecessary deduction guide.


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

3 Files Affected:

  • (modified) libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp (+1-1)
  • (modified) libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp (+1-1)
  • (modified) libcxx/test/support/nasty_string.h (+7-27)
diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp
index f1f5828bfe21d..d494cd0435b96 100644
--- a/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_append/initializer_list.pass.cpp
@@ -43,7 +43,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
 
   test<std::basic_string<char, std::char_traits<char>, min_allocator<char>>>();
   test<std::basic_string<char, std::char_traits<char>, safe_allocator<char>>>();
-#ifndef TEST_HAS_NO_NASTY_STRING
+#if TEST_STD_VER >= 20
   test<nasty_string>();
 #endif
 
diff --git a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
index 8b310630bf07a..edd3913fa01f6 100644
--- a/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
+++ b/libcxx/test/std/strings/basic.string/string.modifiers/string_assign/string.pass.cpp
@@ -74,7 +74,7 @@ TEST_CONSTEXPR_CXX20 bool test() {
   test_assign<std::u16string>();
   test_assign<std::u32string>();
 #endif
-#ifndef TEST_HAS_NO_NASTY_STRING
+#if TEST_STD_VER >= 20
   test_assign<nasty_string>();
 #endif
 
diff --git a/libcxx/test/support/nasty_string.h b/libcxx/test/support/nasty_string.h
index fa4c1b6764314..f98dc1cb7332e 100644
--- a/libcxx/test/support/nasty_string.h
+++ b/libcxx/test/support/nasty_string.h
@@ -23,21 +23,7 @@
 // library uses the provided `CharTraits` instead of using operations on
 // the value_type directly.
 
-
-// When using the code during constant evaluation it relies on
-//   P2647R1 Permitting static constexpr variables in constexpr functions
-// This is a C++23 feature, which is not supported by all compilers yet.
-// * GCC >= 13
-// * Clang >= 16
-// * MSVC no support yet
-//
-// TODO After there is proper compiler support use TEST_STD_VER >= 23 instead
-// of this macro in the tests.
-#if TEST_STD_VER < 23 || __cpp_constexpr < 202211L
-#  define TEST_HAS_NO_NASTY_STRING
-#endif
-
-#ifndef TEST_HAS_NO_NASTY_STRING
+#if TEST_STD_VER >= 20
 // Make sure the char-like operations in strings do not depend on the char-like type.
 struct nasty_char {
   template <typename T>
@@ -162,13 +148,8 @@ struct ToNastyChar {
   nasty_char text[N];
 };
 
-template <std::size_t N>
-ToNastyChar(const char (&)[N]) -> ToNastyChar<N>;
-
-template <ToNastyChar t>
-constexpr auto to_nasty_char() {
-  return t;
-}
+template <ToNastyChar Str>
+inline constexpr auto static_nasty_text = Str;
 
 // A macro like MAKE_CSTRING
 //
@@ -178,13 +159,12 @@ constexpr auto to_nasty_char() {
 #  define CONVERT_TO_CSTRING(CHAR, STR)                                                                                \
     []<class CharT> {                                                                                                  \
       if constexpr (std::is_same_v<CharT, nasty_char>) {                                                               \
-        static constexpr auto result = to_nasty_char<STR>();                                                           \
-        return result.text;                                                                                            \
+        return static_nasty_text<STR>.text;                                                                            \
       } else                                                                                                           \
         return MAKE_CSTRING(CharT, STR);                                                                               \
     }.template operator()<CHAR>() /* */
-#else                             // TEST_HAS_NO_NASTY_STRING
+#else                             // TEST_STD_VER >= 20
 #  define CONVERT_TO_CSTRING(CharT, STR) MAKE_CSTRING(CharT, STR)
-#endif                            // TEST_HAS_NO_NASTY_STRING
+#endif // TEST_STD_VER >= 20
 
-#endif                            // TEST_SUPPORT_NASTY_STRING_H
+#endif // TEST_SUPPORT_NASTY_STRING_H

@frederick-vs-ja frederick-vs-ja merged commit 3e7be49 into llvm:main Apr 13, 2025
80 checks passed
@frederick-vs-ja frederick-vs-ja deleted the libcxx-test-nasty-string-20 branch April 13, 2025 03:23
var-const pushed a commit to ldionne/llvm-project that referenced this pull request Apr 17, 2025
It seems that we can only rely on C++20 features and make `nasty_string`
also tested for MSVC STL.
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. test-suite
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants