Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libcxx][ranges] implements dangling, borrowed_iterator_t, borrowed_s…
…ubrange_t * Implements part of P0896 'The One Ranges Proposal' * Implements http://wg21.link/range.dangling Reviewed By: zoecarver Differential Revision: https://reviews.llvm.org/D105205
- Loading branch information
Showing
10 changed files
with
200 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// -*- C++ -*- | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___RANGES_DANGLING_H | ||
#define _LIBCPP___RANGES_DANGLING_H | ||
|
||
#include <__config> | ||
#include <__ranges/access.h> | ||
#include <__ranges/concepts.h> | ||
#include <type_traits> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
#pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_PUSH_MACROS | ||
#include <__undef_macros> | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
#if !defined(_LIBCPP_HAS_NO_RANGES) | ||
|
||
namespace ranges { | ||
struct dangling { | ||
dangling() = default; | ||
_LIBCPP_HIDE_FROM_ABI constexpr dangling(auto&&...) noexcept {} | ||
}; | ||
|
||
template <range _Rp> | ||
using borrowed_iterator_t = _If<borrowed_range<_Rp>, iterator_t<_Rp>, dangling>; | ||
|
||
// borrowed_subrange_t defined in <__ranges/subrange.h> | ||
} // namespace ranges | ||
|
||
#endif // !_LIBCPP_HAS_NO_RANGES | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
_LIBCPP_POP_MACROS | ||
|
||
#endif // _LIBCPP___RANGES_DANGLING_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
libcxx/test/libcxx/diagnostics/detail.headers/ranges/dangling.module.verify.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// -*- C++ -*- | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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: modules-build | ||
|
||
// WARNING: This test was generated by 'generate_private_header_tests.py' | ||
// and should not be edited manually. | ||
|
||
// expected-error@*:* {{use of private header from outside its module: '__ranges/dangling.h'}} | ||
#include <__ranges/dangling.h> |
36 changes: 36 additions & 0 deletions
36
libcxx/test/std/ranges/range.utility/range.dangling/borrowed_iterator.compile.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++17 | ||
// UNSUPPORTED: libcpp-no-concepts | ||
// UNSUPPORTED: gcc-10 | ||
|
||
// std::ranges::borrowed_iterator_t; | ||
|
||
#include <ranges> | ||
|
||
#include <concepts> | ||
#include <span> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::string>, std::ranges::dangling>); | ||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::string&&>, std::ranges::dangling>); | ||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::vector<int> >, std::ranges::dangling>); | ||
|
||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::string&>, std::string::iterator>); | ||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::string_view>, std::string_view::iterator>); | ||
static_assert(std::same_as<std::ranges::borrowed_iterator_t<std::span<int> >, std::span<int>::iterator>); | ||
|
||
template <class T> | ||
constexpr bool has_borrowed_iterator = requires { | ||
typename std::ranges::borrowed_iterator_t<T>; | ||
}; | ||
|
||
static_assert(!has_borrowed_iterator<int>); |
44 changes: 44 additions & 0 deletions
44
libcxx/test/std/ranges/range.utility/range.dangling/borrowed_subrange.compile.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++17 | ||
// UNSUPPORTED: libcpp-no-concepts | ||
// UNSUPPORTED: gcc-10 | ||
|
||
// std::ranges::borrowed_subrange_t; | ||
|
||
#include <ranges> | ||
|
||
#include <concepts> | ||
#include <span> | ||
#include <string> | ||
#include <string_view> | ||
#include <vector> | ||
|
||
static_assert(std::same_as<std::ranges::borrowed_subrange_t<std::string>, std::ranges::dangling>); | ||
static_assert(std::same_as<std::ranges::borrowed_subrange_t<std::string&&>, std::ranges::dangling>); | ||
static_assert(std::same_as<std::ranges::borrowed_subrange_t<std::vector<int> >, std::ranges::dangling>); | ||
|
||
static_assert( | ||
std::same_as<std::ranges::borrowed_subrange_t<std::string&>, std::ranges::subrange<std::string::iterator> >); | ||
|
||
static_assert( | ||
std::same_as<std::ranges::borrowed_subrange_t<std::span<int> >, std::ranges::subrange<std::span<int>::iterator> >); | ||
|
||
static_assert(std::same_as<std::ranges::borrowed_subrange_t<std::string_view>, | ||
std::ranges::subrange<std::string_view::iterator> >); | ||
|
||
template <class T> | ||
constexpr bool has_type = requires { | ||
typename std::ranges::borrowed_subrange_t<T>; | ||
}; | ||
|
||
static_assert(!has_type<int>); | ||
|
||
struct S {}; | ||
static_assert(!has_type<S>); |
40 changes: 40 additions & 0 deletions
40
libcxx/test/std/ranges/range.utility/range.dangling/dangling.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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, c++17 | ||
// UNSUPPORTED: libcpp-no-concepts | ||
// UNSUPPORTED: gcc-10 | ||
|
||
// std::ranges::dangling; | ||
|
||
#include <ranges> | ||
|
||
#include <concepts> | ||
#include <type_traits> | ||
|
||
static_assert(std::is_empty_v<std::ranges::dangling>); | ||
|
||
template<int> struct S { }; | ||
static_assert(std::is_nothrow_constructible_v<std::ranges::dangling>); | ||
static_assert(std::is_nothrow_constructible_v<std::ranges::dangling, S<0>>); | ||
static_assert(std::is_nothrow_constructible_v<std::ranges::dangling, S<0>, S<1>>); | ||
static_assert(std::is_nothrow_constructible_v<std::ranges::dangling, S<0>, S<1>, S<2>>); | ||
|
||
constexpr bool test_dangling() { | ||
[[maybe_unused]] auto a = std::ranges::dangling(); | ||
[[maybe_unused]] auto b = std::ranges::dangling(S<0>()); | ||
[[maybe_unused]] auto c = std::ranges::dangling(S<0>(), S<1>()); | ||
[[maybe_unused]] auto d = std::ranges::dangling(S<0>(), S<1>(), S<2>()); | ||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
static_assert(test_dangling()); | ||
test_dangling(); | ||
return 0; | ||
} |