From 8ec4999766bedd3f3195f8c5eebe84ed9320bfe1 Mon Sep 17 00:00:00 2001 From: Louis Dionne Date: Mon, 4 Apr 2022 13:35:41 -0400 Subject: [PATCH] [libc++] Tidy up tests for deduction guides and other compile-time failing properties in std::string Instead of using `.fail.cpp` tests, use `.verify.cpp` to check for the exact reason of the failure. In the case of deduction guides, use SFINAE based tests instead since that is our preferred way of testing those. Finally, ensure that we actually run the test in `iter_alloc_deduction.pass.cpp`, since we were not running anything before. Differential Revision: https://reviews.llvm.org/D123055 --- ...fail.cpp => allocator_mismatch.verify.cpp} | 7 +-- .../strings/basic.string/char.bad.fail.cpp | 54 ------------------ .../strings/basic.string/char.bad.verify.cpp | 51 +++++++++++++++++ .../string.cons/iter_alloc_deduction.fail.cpp | 55 ------------------- .../string.cons/iter_alloc_deduction.pass.cpp | 32 +++++++++-- .../string.cons/string_view.compile.fail.cpp | 24 -------- .../string.cons/string_view.pass.cpp | 14 +++-- .../string_view_deduction.fail.cpp | 40 -------------- .../string_view_deduction.pass.cpp | 16 ++++-- .../string_view_size_size_deduction.fail.cpp | 46 ---------------- .../string_view_size_size_deduction.pass.cpp | 16 ++++-- ...le.fail.cpp => traits_mismatch.verify.cpp} | 7 +-- 12 files changed, 112 insertions(+), 250 deletions(-) rename libcxx/test/std/strings/basic.string/{allocator_mismatch.compile.fail.cpp => allocator_mismatch.verify.cpp} (75%) delete mode 100644 libcxx/test/std/strings/basic.string/char.bad.fail.cpp create mode 100644 libcxx/test/std/strings/basic.string/char.bad.verify.cpp delete mode 100644 libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.fail.cpp delete mode 100644 libcxx/test/std/strings/basic.string/string.cons/string_view.compile.fail.cpp delete mode 100644 libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.fail.cpp delete mode 100644 libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.fail.cpp rename libcxx/test/std/strings/basic.string/{traits_mismatch.compile.fail.cpp => traits_mismatch.verify.cpp} (77%) diff --git a/libcxx/test/std/strings/basic.string/allocator_mismatch.compile.fail.cpp b/libcxx/test/std/strings/basic.string/allocator_mismatch.verify.cpp similarity index 75% rename from libcxx/test/std/strings/basic.string/allocator_mismatch.compile.fail.cpp rename to libcxx/test/std/strings/basic.string/allocator_mismatch.verify.cpp index 1d016991f0c6..2c59164051c7 100644 --- a/libcxx/test/std/strings/basic.string/allocator_mismatch.compile.fail.cpp +++ b/libcxx/test/std/strings/basic.string/allocator_mismatch.verify.cpp @@ -11,9 +11,4 @@ #include -int main(int, char**) -{ - std::basic_string, std::allocator > s; - - return 0; -} +std::basic_string, std::allocator > s; // expected-error@*:* {{Allocator::value_type must be same type as value_type}} diff --git a/libcxx/test/std/strings/basic.string/char.bad.fail.cpp b/libcxx/test/std/strings/basic.string/char.bad.fail.cpp deleted file mode 100644 index bace91c3222b..000000000000 --- a/libcxx/test/std/strings/basic.string/char.bad.fail.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -// -// ... manipulating sequences of any non-array trivial standard-layout types. - -#include -#include "test_traits.h" - -struct NotTrivial { - NotTrivial() : value(3) {} - int value; -}; - -struct NotStandardLayout { -public: - NotStandardLayout() : one(1), two(2) {} - int sum() const { return one + two; } // silences "unused field 'two' warning" - int one; -private: - int two; -}; - -int main(int, char**) -{ - { -// array - typedef char C[3]; - static_assert(std::is_array::value, ""); - std::basic_string > s; -// expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must not be an array"}} - } - - { -// not trivial - static_assert(!std::is_trivial::value, ""); - std::basic_string > s; -// expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must be trivial"}} - } - - { -// not standard layout - static_assert(!std::is_standard_layout::value, ""); - std::basic_string > s; -// expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must be standard-layout"}} - } - - return 0; -} diff --git a/libcxx/test/std/strings/basic.string/char.bad.verify.cpp b/libcxx/test/std/strings/basic.string/char.bad.verify.cpp new file mode 100644 index 000000000000..0eab73a6de47 --- /dev/null +++ b/libcxx/test/std/strings/basic.string/char.bad.verify.cpp @@ -0,0 +1,51 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// +// ... manipulating sequences of any non-array trivial standard-layout types. + +#include +#include "test_traits.h" + +struct NotTrivial { + NotTrivial() : value(3) {} + int value; +}; + +struct NotStandardLayout { +public: + NotStandardLayout() : one(1), two(2) {} + int sum() const { return one + two; } // silences "unused field 'two' warning" + int one; +private: + int two; +}; + +void f() { + { + // array + typedef char C[3]; + static_assert(std::is_array::value, ""); + std::basic_string > s; + // expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must not be an array"}} + } + + { + // not trivial + static_assert(!std::is_trivial::value, ""); + std::basic_string > s; + // expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must be trivial"}} + } + + { + // not standard layout + static_assert(!std::is_standard_layout::value, ""); + std::basic_string > s; + // expected-error-re@string:* {{static_assert failed{{.*}} "Character type of basic_string must be standard-layout"}} + } +} diff --git a/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.fail.cpp b/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.fail.cpp deleted file mode 100644 index 4ab4189d3e4c..000000000000 --- a/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.fail.cpp +++ /dev/null @@ -1,55 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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, c++11, c++14 - -// template::value_type>> -// basic_string(InputIterator, InputIterator, Allocator = Allocator()) -// -> basic_string::value_type, -// char_traits::value_type>, -// Allocator>; -// -// The deduction guide shall not participate in overload resolution if InputIterator -// is a type that does not qualify as an input iterator, or if Allocator is a type -// that does not qualify as an allocator. - - -#include -#include -#include -#include - -#include "test_macros.h" - -class NotAnIterator {}; - -template -struct NotAnAllocator { typedef T value_type; }; - -int main(int, char**) -{ - { // Not an iterator at all - std::basic_string s1{NotAnIterator{}, NotAnIterator{}, std::allocator{}}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}} - } - { // Not an input iterator - std::basic_string s0; - std::basic_string s1{std::back_insert_iterator(s0), // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}} - std::back_insert_iterator(s0), - std::allocator{}}; - } - { // Not an allocator - const wchar_t* s = L"12345678901234"; - (void)s; - std::basic_string s1{s, s+10, NotAnAllocator{}}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}} - } - - - return 0; -} diff --git a/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp index 74d98a976bb6..d483af689abf 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/iter_alloc_deduction.pass.cpp @@ -9,10 +9,6 @@ // // UNSUPPORTED: c++03, c++11, c++14 -// template -// basic_string(InputIterator begin, InputIterator end, -// const Allocator& a = Allocator()); - // template::value_type>> // basic_string(InputIterator, InputIterator, Allocator = Allocator()) @@ -24,15 +20,35 @@ // is a type that does not qualify as an input iterator, or if Allocator is a type // that does not qualify as an allocator. -#include -#include #include #include +#include +#include +#include #include "test_macros.h" #include "test_allocator.h" #include "min_allocator.h" +class NotAnIterator {}; +using NotAnInputIterator = std::back_insert_iterator>; + +template +struct NotAnAllocator { typedef T value_type; }; + +template +struct CanDeduce : std::false_type { }; + +template +struct CanDeduce(), std::declval(), std::declval()} +)> : std::true_type { }; + +static_assert( CanDeduce>::value); +static_assert(!CanDeduce>::value); +static_assert(!CanDeduce>::value); +static_assert(!CanDeduce>::value); + bool test() { { const char* s = "12345678901234"; @@ -90,6 +106,10 @@ bool test() { int main(int, char**) { + test(); +#if TEST_STD_VER > 17 + // static_assert(test()); +#endif return 0; } diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view.compile.fail.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view.compile.fail.cpp deleted file mode 100644 index 61d5b3db4e94..000000000000 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view.compile.fail.cpp +++ /dev/null @@ -1,24 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -// - -// explicit basic_string(basic_string_view sv, const Allocator& a = Allocator()); - -#include -#include - -void foo ( const string &s ) {} - -int main(int, char**) -{ - std::string_view sv = "ABCDE"; - foo(sv); // requires implicit conversion from string_view to string - - return 0; -} diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view.pass.cpp index d730f674f89f..4621ba7acae1 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/string_view.pass.cpp @@ -10,15 +10,19 @@ // explicit basic_string(basic_string_view sv, const Allocator& a = Allocator()); -#include -#include -#include #include #include +#include +#include +#include +#include -#include "test_macros.h" -#include "test_allocator.h" #include "min_allocator.h" +#include "test_allocator.h" +#include "test_macros.h" + +static_assert(!std::is_convertible::value, ""); +static_assert(!std::is_convertible::value, ""); template TEST_CONSTEXPR_CXX20 void diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.fail.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.fail.cpp deleted file mode 100644 index 40211cf96ea2..000000000000 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.fail.cpp +++ /dev/null @@ -1,40 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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, c++11, c++14 - -// template -// basic_string(InputIterator begin, InputIterator end, -// const Allocator& a = Allocator()); - -// template -// > -// basic_string(basic_string_view, const Allocator& = Allocator()) -// -> basic_string; -// -// The deduction guide shall not participate in overload resolution if Allocator -// is a type that does not qualify as an allocator. - -#include -#include -#include -#include -#include - -int main(int, char**) -{ - { - std::string_view sv = "12345678901234"; - std::basic_string s1{sv, 23}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}} - } - - return 0; -} diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp index 1dff75c9c9a4..cdb045d9f137 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/string_view_deduction.pass.cpp @@ -9,10 +9,6 @@ // // UNSUPPORTED: c++03, c++11, c++14 -// template -// basic_string(InputIterator begin, InputIterator end, -// const Allocator& a = Allocator()); - // template @@ -35,6 +31,18 @@ #include "test_allocator.h" #include "min_allocator.h" +template +struct CanDeduce : std::false_type { }; + +template +struct CanDeduce(), std::declval()} +)> : std::true_type { }; + +struct NotAnAllocator { }; +static_assert( CanDeduce>::value); +static_assert(!CanDeduce::value); + bool test() { { std::string_view sv = "12345678901234"; diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.fail.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.fail.cpp deleted file mode 100644 index 7e087fe12094..000000000000 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.fail.cpp +++ /dev/null @@ -1,46 +0,0 @@ -//===----------------------------------------------------------------------===// -// -// 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, c++11, c++14 - -// template -// basic_string(InputIterator begin, InputIterator end, -// const Allocator& a = Allocator()); - -// template -// > -// basic_string(basic_string_view, -// typename see below::size_type, -// typename see below::size_type, -// const Allocator& = Allocator()) -// -> basic_string; -// -// A size_type parameter type in a basic_string deduction guide refers to the size_type -// member type of the type deduced by the deduction guide. -// -// The deduction guide shall not participate in overload resolution if Allocator -// is a type that does not qualify as an allocator. - -#include -#include -#include -#include -#include - -int main(int, char**) -{ - { - std::string_view sv = "12345678901234"; - std::basic_string s1{sv, 0, 4, 23}; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'basic_string'}} - } - - return 0; -} diff --git a/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.pass.cpp b/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.pass.cpp index e36fa2c3b2b0..f0dfec2d3448 100644 --- a/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.pass.cpp +++ b/libcxx/test/std/strings/basic.string/string.cons/string_view_size_size_deduction.pass.cpp @@ -9,10 +9,6 @@ // // UNSUPPORTED: c++03, c++11, c++14 -// template -// basic_string(InputIterator begin, InputIterator end, -// const Allocator& a = Allocator()); - // template @@ -39,6 +35,18 @@ #include "test_allocator.h" #include "min_allocator.h" +template +struct CanDeduce : std::false_type { }; + +template +struct CanDeduce(), std::declval(), std::declval(), std::declval()} +)> : std::true_type { }; + +struct NotAnAllocator { }; +static_assert( CanDeduce>::value); +static_assert(!CanDeduce::value); + bool test() { { std::string_view sv = "12345678901234"; diff --git a/libcxx/test/std/strings/basic.string/traits_mismatch.compile.fail.cpp b/libcxx/test/std/strings/basic.string/traits_mismatch.verify.cpp similarity index 77% rename from libcxx/test/std/strings/basic.string/traits_mismatch.compile.fail.cpp rename to libcxx/test/std/strings/basic.string/traits_mismatch.verify.cpp index 47524d2c34d9..6730e2fb5d90 100644 --- a/libcxx/test/std/strings/basic.string/traits_mismatch.compile.fail.cpp +++ b/libcxx/test/std/strings/basic.string/traits_mismatch.verify.cpp @@ -11,9 +11,4 @@ #include -int main(int, char**) -{ - std::basic_string> s; - - return 0; -} +std::basic_string > s; // expected-error@*:* {{traits_type::char_type must be the same type as CharT}}