Commit Graph

68 Commits

Author SHA1 Message Date
Patrick Palka
620db4ca60 libstdc++: Reduce the size of a subrange with empty sentinel type
libstdc++-v3/ChangeLog:

	* include/bits/ranges_util.h (subrange::_M_end): Give it
	[[no_unique_address]].
	* testsuite/std/ranges/subrange/sizeof.cc: New test.
2020-09-28 11:55:04 -04:00
Patrick Palka
623443357e libstdc++: Reduce the size of an unbounded iota_view
libstdc++-v3/ChangeLog:

	* include/std/ranges (iota_view::_M_bound): Give it
	[[no_unique_address]].
	* testsuite/std/ranges/iota/iota_view.cc: Check that an
	unbounded iota_view has minimal size.
2020-09-28 11:54:57 -04:00
Patrick Palka
42907ca9a4 libstdc++: Specialize ranges::__detail::__box for semiregular types
The class template semiregular-box<T> defined in [range.semi.wrap] is
used by a number of views to accomodate non-semiregular subobjects
while ensuring that the overall view remains semiregular.  It provides
a stand-in default constructor, copy assignment operator and move
assignment operator whenever the underlying type lacks them.  The
wrapper derives from std::optional<T> to support default construction
when T is not default constructible.

It would be nice for this wrapper to essentially be a no-op when the
underlying type is already semiregular, but this is currently not the
case due to its use of std::optional<T>, which incurs space overhead
compared to storing just T.

To that end, this patch specializes the semiregular wrapper for
semiregular T.  Compared to the primary template, this specialization
uses less space, and it allows [[no_unique_address]] to optimize away
wrapped data members whose underlying type is empty and semiregular
(e.g. a non-capturing lambda).  This patch also applies
[[no_unique_address]] to the five data members that use the wrapper.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::__boxable): Split out the
	associated constraints of __box into here.
	(__detail::__box): Use the __boxable concept.  Define a leaner
	partial specialization for semiregular types.
	(single_view::_M_value): Give it [[no_unique_address]].
	(filter_view::_M_pred): Likewise.
	(transform_view::_M_fun): Likewise.
	(take_while_view::_M_pred): Likewise.
	(drop_while_view::_M_pred):: Likewise.
	* testsuite/std/ranges/adaptors/detail/semiregular_box.cc: New
	test.
2020-09-24 12:58:39 -04:00
Jonathan Wakely
aecea4158f libstdc++: Fix constraints for drop_view::begin() const [LWG 3482]
libstdc++-v3/ChangeLog:

	* include/std/ranges (drop_view::begin()): Adjust constraints
	to match the correct condition for O(1) ranges::next (LWG 3482).
	* testsuite/std/ranges/adaptors/drop.cc: Check that iterator is
	cached for non-sized_range.
2020-09-21 23:43:25 +01:00
Jonathan Wakely
2ec58cfcea libstdc++: Relax constraints on transform_view and elements_view iterators
libstdc++-v3/ChangeLog:

	* include/std/ranges (transform_view, elements_view): Relax
	constraints on operator- for iterators, as per LWG 3483.
	* testsuite/std/ranges/adaptors/elements.cc: Check that we
	can take the difference of two iterators from a non-random
	access range.
	* testsuite/std/ranges/adaptors/transform.cc: Likewise.
2020-09-21 14:30:38 +01:00
Patrick Palka
71e9716137 libstdc++: Fix arithmetic bug in chrono::year_month::operator+
This fixes the months-based addition for year_month when the
year_month's month component is 0.

libstdc++-v3/ChangeLog:

	* include/std/chrono (year_month::operator+): Properly handle a
	month value of 0 by casting the month value to int before
	subtracting 1 from it so that the difference is sign-extended in
	the subsequent addition.
	* testsuite/std/time/year_month/1.cc: Test adding months to a
	year_month whose month component is below or above the
	normalized range of [1,12].
2020-08-27 14:11:24 -04:00
Patrick Palka
7b743c67f0 libstdc++: Fix operator overload ambiguity with calendar types
We currently don't enforce a constraint on some of the calendar types'
addition/subtraction operator overloads that take a 'months' arguments:

  Constraints: If the argument supplied by the caller for the months
  parameter is convertible to years, its implicit conversion sequence to
  years is worse than its implicit conversion sequence to months.

This constraint is relevant when adding/subtracting a duration to/from,
say, a year_month where the given duration is convertible to both
'months' and to 'years' (as in the new testcases below).  The correct
behavior here in light of this constraint is to perform the operation
through the (more efficient) 'years'-based overload, but we currently
emit an ambiguous overload error.

This patch templatizes the 'months'-based addition/subtraction operator
overloads so that in the event of an implicit-conversion tie, we select
the non-template 'years'-based overload.  This is the same approach
that the date library takes for enforcing this constraint.

libstdc++-v3/ChangeLog:

	* include/std/chrono
	(__detail::__months_years_conversion_disambiguator): Define.
	(year_month::operator+=): Templatize the 'months'-based overload
	so that the 'years'-based overload is selected in case of
	equally-ranked implicit conversion sequences to both 'months'
	and 'years' from the supplied argument.
	(year_month::operator-=): Likewise.
	(year_month::operator+): Likewise.
	(year_month::operator-): Likewise.
	(year_month_day::operator+=): Likewise.
	(year_month_day::operator-=): Likewise.
	(year_month_day::operator+): Likewise.
	(year_month_day::operator-): Likewise.
	(year_month_day_last::operator+=): Likewise.
	(year_month_day_last::operator-=): Likewise.
	(year_month_day_last::operator+): Likewise
	(year_month_day_last::operator-): Likewise.
	(year_month_day_weekday::operator+=): Likewise
	(year_month_day_weekday::operator-=): Likewise.
	(year_month_day_weekday::operator+): Likewise.
	(year_month_day_weekday::operator-): Likewise.
	(year_month_day_weekday_last::operator+=): Likewise
	(year_month_day_weekday_last::operator-=): Likewise.
	(year_month_day_weekday_last::operator+): Likewise.
	(year_month_day_weekday_last::operator-): Likewise.
	(testsuite/std/time/year_month/2.cc): New test.
	(testsuite/std/time/year_month_day/2.cc): New test.
	(testsuite/std/time/year_month_day_last/2.cc): New test.
	(testsuite/std/time/year_month_weekday/2.cc): New test.
	(testsuite/std/time/year_month_weekday_last/2.cc): New test.
2020-08-27 14:09:52 -04:00
Patrick Palka
3ae0cd94ab libstdc++: Implement remaining piece of LWG 3448
Almost all of the proposed resolution for LWG 3448 is already
implemented; the only part left is to adjust the return type of
transform_view::sentinel::operator-.

libstdc++-v3/ChangeLog:

	PR libstdc++/95322
	* include/std/ranges (transform_view::sentinel::__distance_from):
	Give this a deduced return type.
	(transform_view::sentinel::operator-): Adjust the return type so
	that it's based on the constness of the iterator rather than
	that of the sentinel.
	* testsuite/std/ranges/adaptors/95322.cc: Refer to LWG 3488.
2020-08-26 21:53:04 -04:00
Patrick Palka
4be16d1c1c libstdc++: elements_view's sentinel and iterator not comparable [LWG 3406]
This implements the proposed resolution for LWG 3406, and adds a
testcase for the example from P1994R1.

libstdc++-v3/ChangeLog:

	* include/std/ranges (elements_view::begin): Adjust constraints.
	(elements_view::end): Likewise.
	(elements_view::_Sentinel::operator==): Templatize to take both
	_Iterator<true> and _Iterator<false>.
	(elements_view::_Sentinel::operator-): Likewise.
	* testsuite/std/ranges/adaptors/elements.cc: Add testcase for
	the example from P1994R1.
	* testsuite/std/ranges/adaptors/lwg3406.cc: New test.
2020-08-26 21:52:58 -04:00
Patrick Palka
0c5df67ffc libstdc++: Fix typo in chrono::year_month_weekday::operator==
libstdc++-v3/ChangeLog:

	* include/std/chrono (year_month_weekday::operator==): Compare
	weekday_indexed instead of weekday.
	* testsuite/std/time/year_month_weekday/1.cc: Augment testcase.
2020-08-26 12:58:37 -04:00
Patrick Palka
03d5044b31 libstdc++: Add more C++20 additions to <chrono>
This patch adds the C++20 calendar types and their methods as defined in
[time.cal] (modulo the parsing/printing support).  This patch also
implements [time.hms] and [time.12], and a few more bits of
[time.clock].  The remaining C++20 additions to <chrono> from P0355 and
P1466 depend on [time.zone] and <format>, so they will come later, as
will more optimized versions of some of the algorithms added here.

The non-member operator overloads for the calendar types are defined as
namespace-scope functions in the standard, but here we instead define
these operator overloads as hidden friends.  This simplifies the
implementation somewhat and lets us reap the benefits of hidden friends
for these overloads.

The bulk of this work is based on a patch from Ed Smith-Rowland, which can
be found at the Git branch users/redi/heads/calendar.

Co-authored-by: Ed Smith-Rowland <3dw4rd@verizon.net>
Co-authored-by: Jonathan Wakely <jwakely@redhat.com>

libstdc++-v3/ChangeLog:

	* include/std/chrono (time_point::operator++)
	(time_point::operator--): Define.
	(utc_clock, tai_clock, gps_clock): Forward declare.
	(utc_time, utc_seconds, tai_time, tai_seconds, gps_time)
	(gps_seconds): Define.
	(is_clock<utc_clock>, is_clock<tai_clock>, is_clock<gps_clock>)
	(is_clock_v<utc_clock>, is_clock_v<tai_clock>)
	(is_clock_v<gps_clock>): Define these specializations.
	(leap_second_info): Define.
	(day, month, year, weekday, weekday_indexed)
	(weekday_last, month_day, month_day_last, month_weekday)
	(month_weekday_last, year_month, year_month_day)
	(year_month_day_last, year_month_weekday, year_month_weekday_last):
	Declare and later define.
	(last_spec, last, __detail::__days_per_month)
	(__detail::__days_per_month, __detail::__last_day): Define.
	(January, February, March, April, May, June, July, August)
	(September, October, November, December, Sunday, Monday, Tuesday)
	(Wednesday, Thursday, Friday, Saturday): Define.
	(weekday::operator[]): Define out-of-line.
	(year_month_day::_S_from_days, year_month_day::M_days_since_epoch):
	Likewise.
	(year_month_day::year_month_day, year_month_day::ok): Likewise.
	(__detail::__pow10, hh_mm_ss): Define.
	(literals::chrono_literals::operator""d)
	(literals::chrono_literals::operator""y): Define.
	(is_am, is_pm, make12, make24): Define.
	* testsuite/20_util/time_point/4.cc: New test.
	* testsuite/std/time/day/1.cc: New test.
	* testsuite/std/time/hh_mm_ss/1.cc: New test.
	* testsuite/std/time/is_am/1.cc: New test.
	* testsuite/std/time/is_pm/1.cc: New test.
	* testsuite/std/time/make12/1.cc: New test.
	* testsuite/std/time/make24/1.cc: New test.
	* testsuite/std/time/month/1.cc: New test.
	* testsuite/std/time/month_day/1.cc: New test.
	* testsuite/std/time/month_day_last/1.cc: New test.
	* testsuite/std/time/month_weekday/1.cc: New test.
	* testsuite/std/time/month_weekday_last/1.cc: New test.
	* testsuite/std/time/weekday/1.cc: New test.
	* testsuite/std/time/weekday_indexed/1.cc: New test.
	* testsuite/std/time/weekday_last/1.cc: New test.
	* testsuite/std/time/year/1.cc: New test.
	* testsuite/std/time/year_month/1.cc: New test.
	* testsuite/std/time/year_month_day/1.cc: New test.
	* testsuite/std/time/year_month_day_last/1.cc: New test.
	* testsuite/std/time/year_month_weekday/1.cc: New test.
	* testsuite/std/time/year_month_weekday_last/1.cc: New test.
2020-08-25 10:23:59 -04:00
Jonathan Wakely
ef275d1f20 libstdc++: Add deduction guide for std::ranges::join_view [LWG 3474]
This implements the proposed resolution for LWG 3474.

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view): Add deduction guide (LWG 3474).
	* testsuite/std/ranges/adaptors/join_lwg3474.cc: New test.
2020-08-24 16:18:32 +01:00
Jonathan Wakely
a0e6f05d26 libstdc++: Fix iota_view::size() to avoid overflow
This avoids the overflow that occurs when negating the most negative
value of an integral type.

Also prevent returning signed int when the values have lower rank and
promote to int.

libstdc++-v3/ChangeLog:

	* include/std/ranges (ranges::iota_view::size()): Perform all
	calculations in the right unsigned types.
	* testsuite/std/ranges/iota/size.cc: New test.
2020-08-24 16:17:04 +01:00
Jonathan Wakely
5e9ad288eb libstdc++: Make incrementable<__int128> satisfied in strict mode
This adds specializations of std::incrementable_traits so that 128-bit
integers are always considered incrementable (and therefore usable with
std::ranges::iota_view) even when they don't satisfy std::integral.

libstdc++-v3/ChangeLog:

	* include/bits/iterator_concepts.h [__STRICT_ANSI__]
	(incrementable_traits<__int128>): Define specialization.
	(incrementable_traits<unsigned __int128>): Likewise.
	* testsuite/std/ranges/iota/96042.cc: Test iota_view with
	__int128.
2020-08-20 19:42:02 +01:00
Jonathan Wakely
e6e01618e8 libstdc++: Make make-unsigned-like-t<__int128> work [PR 96042]
As well as ensuring that numeric_limits<__int128> is defined, we need to
ensure that make-unsigned-like-t and to-unsigned-like work correctly for
128-bit integers in strict mode. This ensures that a subrange created
from an iota_view's iterator and sentinel can represent its size.

Co-authored-by: Patrick Palka  <ppalka@redhat.com>

libstdc++-v3/ChangeLog:

2020-08-19  Jonathan Wakely  <jwakely@redhat.com>
	    Patrick Palka  <ppalka@redhat.com>

	PR libstdc++/96042
	* include/bits/range_access.h (__detail::__to_unsigned_like):
	Do not use make_unsigned_t<T> in the return type, as it can
	result in an error before the integral<T> constraint is checked.
	[__STRICT_ANSI__]: Add overloads for 128-bit integer types.
	(__detail::__make_unsigned_like_t): Define as the return type
	of __to_unsigned_like.
	* testsuite/std/ranges/subrange/96042.cc: New test.
2020-08-19 20:36:10 +01:00
Jonathan Wakely
386fd16c55 libstdc++: Make __int128 meet integer-class requirements [PR 96042]
Because __int128 can be used as the difference type for iota_view, we
need to ensure that it meets the requirements of an integer-class type.
The requirements in [iterator.concept.winc] p10 include numeric_limits
being specialized and giving meaningful answers. Currently we only
specialize numeric_limits for non-standard integer types in non-strict
modes.  However, nothing prevents us from defining an explicit
specialization for any implementation-defined type, so it doesn't matter
whether std::is_integral<__int128> is true or not.

This patch ensures that the numeric_limits specializations for signed
and unsigned __int128 are defined whenever __int128 is available. It
also makes the __numeric_traits and __int_limits helpers work for
__int128, via a new __gnu_cxx::__is_integer_nonstrict trait.

libstdc++-v3/ChangeLog:

	PR libstdc++/96042
	* include/ext/numeric_traits.h (__is_integer_nonstrict): New
	trait which is true for 128-bit integers even in strict modes.
	(__numeric_traits_integer, __numeric_traits): Use
	__is_integer_nonstrict instead of __is_integer.
	* include/std/limits [__STRICT_ANSI__ && __SIZEOF_INT128__]
	(numeric_limits<__int128>, (numeric_limits<unsigned __int128>):
	Define.
	* testsuite/std/ranges/iota/96042.cc: New test.
2020-08-19 16:49:07 +01:00
Patrick Palka
e6c76f0d33 libstdc++: integer-class types as per [iterator.concept.winc]
This implements signed and unsigned integer-class types, whose width is
one bit larger than the widest supported signed and unsigned integral
type respectively.  In our case this is either __int128 and unsigned
__int128, or long long and unsigned long long.

Internally, the two integer-class types are represented as a largest
supported unsigned integral type plus one extra bit.  The signed
integer-class type is represented in two's complement form with the
extra bit acting as the sign bit.

libstdc++-v3/ChangeLog:

	* include/Makefile.am (bits_headers): Add new header
	<bits/max_size_type.h>.
	* include/Makefile.in: Regenerate.
	* include/bits/iterator_concepts.h
	(ranges::__detail::__max_diff_type): Remove definition, replace
	with forward declaration of class __max_diff_type.
	(__detail::__max_size_type): Remove definition, replace with
	forward declaration of class __max_size_type.
	(__detail::__is_unsigned_int128, __is_signed_int128)
	(__is_int128): New concepts.
	(__detail::__is_integer_like): Accept __int128 and unsigned
	__int128.
	(__detail::__is_signed_integer_like): Accept __int128.
	* include/bits/max_size_type.h: New header.
	* include/bits/range_access.h: Include <bits/max_size_type.h>.
	(__detail::__to_unsigned_like): Two new overloads.
	* testsuite/std/ranges/iota/difference_type.cc: New test.
	* testsuite/std/ranges/iota/max_size_type.cc: New test.
2020-08-19 09:12:55 -04:00
Jonathan Wakely
dc8c00966e libstdc++: Avoid using __float128 in strict modes
libstdc++-v3/ChangeLog:

	* testsuite/26_numerics/numbers/float128.cc: Check
	__STRICT_ANSI__ before using __float128.
	* testsuite/std/concepts/concepts.lang/concept.arithmetic/floating_point.cc:
	Likewise.
2020-07-31 19:58:03 +01:00
Jonathan Wakely
9e67b4356e libstdc++: cv bool can't be an integer-like type (LWG 3467)
libstdc++-v3/ChangeLog:

	* include/bits/iterator_concepts.h (__detail::__cv_bool): New
	helper concept.
	(__detail::__integral_nonbool): Likewise.
	(__detail::__is_integer_like): Use __integral_nonbool.
	* testsuite/std/ranges/access/lwg3467.cc: New test.
2020-07-30 13:09:18 +01:00
Jonathan Wakely
6c2582c040 libstdc++: Fix view adaptors for mixed-const sentinels and iterators (PR 95322)
The bug report is that transform_view's sentinel<false> cannot be
compared to its iterator<true>.  The comparison is supposed to use
operator==(iterator<Const>, sentinel<Const>) after converting
sentinel<false> to sentinel<true>. However, the operator== is a hidden
friend so is not a candidate when comparing iterator<true> with
sentinel<false>. The required conversion would only happen if we'd found
the operator, but we can't find the operator until after the conversion
happens.

A new LWG issue has been reported, but not yet assigned a number.  The
solution suggested by Casey Carter is to make the hidden friends of the
sentinel types work with iterators of any const-ness, so that no
conversions are required.

Patrick Palka observed that join_view has a similar problem and a
similar fix is used for its sentinel.

	PR libstdc++/95322
	* include/std/ranges (transform_view::_Sentinel): Allow hidden
	friends to work with _Iterator<true> and _Iterator<false>.
	(join_view::_Sentinel): Likewise.
	* testsuite/std/ranges/adaptors/95322.cc: New test.
2020-05-27 22:08:15 +01:00
Patrick Palka
3bf5e7657b libstdc++: Fix common_iterator::operator-> [PR95322]
This patch fixes the definition of common_iterator::operator-> when the
underlying iterator's operator* returns a non-reference.

The first problem is that the class __detail::_Common_iter_proxy is used
unqualified.  Fixing that revealed another problem: the class's template
friend declaration of common_iterator doesn't match up with the
definition of common_iterator, because the friend declaration isn't
constrained.

If we try to make the friend declaration match up by adding constraints,
we run into frontend bug PR93467.  So we currently can't correctly
express this friend relation between __detail::_Common_iter_proxy and
common_iterator.

As a workaround to this frontend bug, this patch moves the definition of
_Common_iter_proxy into the class template of common_iterator so that we
could instead express the friend relation via the injected-class-name.

(This bug was found when attempting to use views::common to work around
the compile failure with the testcase in PR95322.)

libstdc++-v3/ChangeLog:

	PR libstdc++/95322
	* include/bits/stl_iterator.h (__detail::_Common_iter_proxy):
	Remove and instead define it ...
	(common_iterator::_Proxy): ... here.
	(common_iterator::operator->): Use it.
	* testsuite/24_iterators/common_iterator/2.cc: New test.
	* testsuite/std/ranges/adaptors/95322.cc: New test.
2020-05-26 16:17:34 -04:00
Patrick Palka
a57aa11191 libstdc++: Compile PR93978 testcase with -Wall
Now that the frontend issue PR c++/94038 is thoroughly fixed, the
testcase for PR93978 no longer fails to compile with -O -Wall, so add
-Wall to the testcase's compile flags to help ensure we don't regress
here.

libstdc++-v3/ChangeLog:

	PR libstdc++/93978
	* testsuite/std/ranges/adaptors/93978.cc: Add -Wall to
	dg-additional-options.  Avoid unused-but-set-variable warning.
2020-05-23 15:25:40 -04:00
Patrick Palka
19667c82e4 libstdc++: Fix subrange::advance and subrange::prev (LWG 3433)
This implements the proposed resolution of LWG 3433, which fixes
subrange::advance when called with a negative argument.

libstdc++-v3/ChangeLog:

	LWG 3433 subrange::advance(n) has UB when n < 0
	* include/std/ranges (subrange::prev): Fix typo.
	(subrange::advance): Handle a negative argument as per the proposed
	resolution of LWG 3433.
	* testsuite/std/ranges/subrange/lwg3433.cc: New test.
2020-04-28 16:43:23 -04:00
Jonathan Wakely
c9313582d8 libstdc++: Update __cpp_lib_concepts value
* include/std/functional (__cpp_lib_concepts): Update macro value to
	indicate P1964R2 support.
	* include/std/version (__cpp_lib_concepts): Likewise.
	* testsuite/std/concepts/1.cc: Adjust expected value.
	* testsuite/std/concepts/2.cc: Likewise.
2020-04-22 22:54:34 +01:00
Jonathan Wakely
b8a28a06ea libstdc++: Define __cpp_lib_ranges macro for C++20
Define the feature test macro now that ranges support is complete.

This also changes the preprocessor checks for the __cpp_concepts macro
so that library components depending on concepts are only enabled when
C++20 concepts are supported, and not just for the Concepts TS (which
uses different syntax in places).

	* include/bits/range_cmp.h (__cpp_lib_ranges): Define.
	* include/bits/stl_iterator.h: Check value of __cpp_concepts so that
	C++20 concepts are required.
	* include/bits/stl_iterator_base_types.h: Likewise.
	* include/std/concepts: Likewise.
	* include/std/version: Likewise.
	* testsuite/std/ranges/headers/ranges/synopsis.cc: Check feature test
	macro.
2020-03-27 23:26:03 +00:00
Jonathan Wakely
16948c54b7 libstdc++: Add some C++20 additions to <chrono>
* include/std/chrono (chrono::days, chrono::weeks, chrono::years)
	(chrono::months, chrono::sys_days, chrono::local_t)
	(chrono::local_time, chrono::local_seconds, chrono::local_days):
	Define for C++20.
	(chrono::time_point): Add missing static assert.
	* testsuite/20_util/time_point/requirements/duration_neg.cc: New test.
	* testsuite/std/time/clock/file/overview.cc: New test.
	* testsuite/std/time/clock/file/members.cc: New test.
	* testsuite/std/time/syn_c++20.cc: New test.
2020-03-26 14:00:12 +00:00
Jonathan Wakely
bf1fc37bb4 libstdc++: Define and use chrono::is_clock for C++20
For C++20 the wait_until members of mutexes and condition variables are
required to be ill-formed if given a clock that doesn't meet the
requirements for a clock type. To implement that requirement this patch
adds static assertions using the chrono::is_clock trait, and defines
that trait.

To avoid expensive checks for the common cases, the trait (and
associated variable template) are explicitly specialized for the
standard clock types.

This also moves the filesystem::__file_clock type from <filesystem> to
<chrono>, so that chrono::file_clock and chrono::file_time can be
defined in <chrono> as required.

	* include/bits/fs_fwd.h (filesystem::__file_clock): Move to ...
	* include/std/chrono (filesystem::__file_clock): Here.
	(filesystem::__file_clock::from_sys, filesystem::__file_clock::to_sys):
	Define public member functions for C++20.
	(is_clock, is_clock_v): Define traits for C++20.
	* include/std/condition_variable (condition_variable::wait_until): Add
	check for valid clock.
	* include/std/future (_State_baseV2::wait_until): Likewise.
	* include/std/mutex (__timed_mutex_impl::_M_try_lock_until): Likewise.
	* include/std/shared_mutex (shared_timed_mutex::try_lock_shared_until):
	Likewise.
	* include/std/thread (this_thread::sleep_until): Likewise.
	* testsuite/30_threads/condition_variable/members/2.cc: Qualify
	slow_clock with new namespace.
	* testsuite/30_threads/condition_variable/members/clock_neg.cc: New
	test.
	* testsuite/30_threads/condition_variable_any/members/clock_neg.cc:
	New test.
	* testsuite/30_threads/future/members/clock_neg.cc: New test.
	* testsuite/30_threads/recursive_timed_mutex/try_lock_until/3.cc:
	Qualify slow_clock with new namespace.
	* testsuite/30_threads/recursive_timed_mutex/try_lock_until/
	clock_neg.cc: New test.
	* testsuite/30_threads/shared_future/members/clock_neg.cc: New
	test.
	* testsuite/30_threads/shared_lock/locking/clock_neg.cc: New test.
	* testsuite/30_threads/shared_timed_mutex/try_lock_until/clock_neg.cc:
	New test.
	* testsuite/30_threads/timed_mutex/try_lock_until/3.cc: Qualify
	slow_clock with new namespace.
	* testsuite/30_threads/timed_mutex/try_lock_until/4.cc: Likewise.
	* testsuite/30_threads/timed_mutex/try_lock_until/clock_neg.cc: New
	test.
	* testsuite/30_threads/unique_lock/locking/clock_neg.cc: New test.
	* testsuite/std/time/traits/is_clock.cc: New test.
	* testsuite/util/slow_clock.h (slow_clock): Move to __gnu_test
	namespace.
2020-03-25 22:07:02 +00:00
Patrick Palka
4512b7d851 libstdc++: Add a test that takes the split_view of a non-forward range
This adds a tests that verifies taking the split_view of a non-forward range
works correctly.  Doing so revealed a typo in one of _OuterIter's constructors.

It also revealed that the default constructor of
__gnu_test::test_range::iterator misbehaves, because by delegating to
Iter<T>(nullptr, nullptr) we perform a null-pointer deref at runtime in
input_iterator_wrapper's constructor due to the ITERATOR_VERIFY check therein.
Instead of delegating to this constructor it seems we can just inherit the
protected default constructor, which does not contain this ITERATOR_VERIFY
check.

libstdc++-v3/ChangeLog:

	* include/std/ranges (split_view::_OuterIter::_OuterIter): Typo fix,
	'address' -> 'std::__addressof'.
	* testsuite/std/ranges/adaptors/split.cc: Test taking the split_view of
	a non-forward input_range.
	* testsuite/util/testsuite_iterators.h (output_iterator_wrapper): Make
	default constructor protected instead of deleted, like with
	input_iterator_wrapper.
	(test_range::iterator): Add comment explaining that this type is used
	only when the underlying wrapper is input_iterator_wrapper or
	output_iterator_wrapper.  Remove delegating defaulted constructor so
	that the inherited default constructor is used instead.
2020-03-11 14:56:52 -04:00
Patrick Palka
8f0d8cd852 libstdc++: LWG 3286 ranges::size is not required to be valid after ...
... a call to ranges::begin on an input range.

This implements LWG 3286.  The new wording for the single-argument constructor
for subrange is implemented by splitting the constructor into two delegating
constructors, one constrained by _S_store_size and the other by !_S_store_size.

Tested on x86_64-pc-linux-gnu, both added tests fail before the patch and pass
with the patch.

libstdc++-v3/ChangeLog:

	LWG 3286 ranges::size is not required to be valid after a call to
	ranges::begin on an input range
	* include/std/ranges (subrange::subrange): Split single-argument
	constructor into two, one constrained by _S_store_size and another by
	!_S_store_size.
	(take_view::begin): Call size() before calling ranges::begin(_M_base).
	* testsuite/std/ranges/adaptors/lwg3286.cc: New test.
	* testsuite/std/ranges/subrange/lwg3286.cc: New test.
2020-03-11 00:40:33 -04:00
Jonathan Wakely
cf0c3a4573 libstdc++: Fix noexcept guarantees for ranges::split_view
Also introduce the _M_i_current() accessors to solve the problem of
access to the private member of _OuterIter from the iter_move and
iter_swap overloads (which are only friends of _InnerIter not
_OuterIter).

	* include/std/ranges (transform_view::_Iterator::__iter_move): Remove.
	(transform_view::_Iterator::operator*): Add noexcept-specifier.
	(transform_view::_Iterator::iter_move): Inline __iter_move body here.
	(split_view::_OuterIter::__current): Add noexcept.
	(split_view::_InnerIter::__iter_swap): Remove.
	(split_view::_InnerIter::__iter_move): Remove.
	(split_view::_InnerIter::_M_i_current): New accessors.
	(split_view::_InnerIter::__at_end): Use _M_i_current().
	(split_view::_InnerIter::operator*): Likewise.
	(split_view::_InnerIter::operator++): Likewise.
	(iter_move(const _InnerIter&)): Likewise.
	(iter_swap(const _InnerIter&, const _InnerIter&)): Likewise.
	* testsuite/std/ranges/adaptors/split.cc: Check noexcept-specifier
	for iter_move and iter_swap on split_view's inner iterator.
2020-03-10 17:45:45 +00:00
Patrick Palka
6aa2ca21a4 libstdc++: Add missing friend declaration to join_view::_Sentinel
The converting constructor of join_view::_Sentinel<true> needs to be able to
access the private members of join_view::_Sentinel<false>.

libstdc++-v3/ChangeLog:

	* include/std/ranges (join_view::_Sentinel<_Const>): Befriend
	join_view::_Sentinel<!_Const>.
	* testsuite/std/ranges/adaptors/join.cc: Augment test.
2020-03-06 09:23:10 -05:00
Patrick Palka
6d082cd901 libstdc++: Give ranges::empty() a concrete return type (PR 93978)
This works around PR 93978 by avoiding having to instantiate the body of
ranges::empty() when checking the constraints of view_interface::operator
bool().  When ranges::empty() has an auto return type, then we must instantiate
its body in order to determine whether the requires expression {
ranges::empty(_M_derived()); } is well-formed.  But this means instantiating
view_interface::empty() and hence view_interface::_M_derived(), all before we've
yet deduced the return type of join_view::end().  (The reason
view_interface::operator bool() is needed in join_view::end() in the first place
is because in this function we perform direct initialization of
join_view::_Sentinel from a join_view, and so we try to find a conversion
sequence from the latter to the former that goes through this conversion
operator.)

Giving ranges::empty() a concrete return type of bool should be safe according
to [range.prim.empty]/4 which says "whenever ranges::empty(E) is a valid
expression, it has type bool."

This fixes the test case in PR 93978 when compiling without -Wall, but with -Wall
the test case still fails due to the issue described in PR c++/94038, I think.
I still don't quite understand why the test case doesn't fail without -O.

libstdc++-v3/ChangeLog:

	PR libstdc++/93978
	* include/bits/range_access.h (__cust_access::_Empty::operator()):
	Declare return type to be bool instead of auto.
	* testsuite/std/ranges/adaptors/93978.cc: New test.
2020-03-06 09:22:54 -05:00
Patrick Palka
a153501578 libstdc++: Memoize {drop,drop_while,filter,reverse}_view::begin
This patch adds memoization to these four views so that their begin() has the
required amortized constant time complexity.

The cache is enabled only for forward_ranges and above because we need the
underlying iterator to be copyable and multi-pass in order for the cache to be
usable.  In the general case we represent the cached result of begin() as a bare
iterator.  This takes advantage of the fact that value-initialized forward
iterators can be compared to as per N3644, so we can use a value-initialized
iterator to denote the "empty" state of the cache.

As a special case, when the underlying range models random_access_range and when
it's profitable size-wise, then we cache the offset of the iterator from the
beginning of the range instead of caching the iterator itself.

Additionally, in drop_view and reverse_view we disable the cache when the
underlying range models random_access_range, because in these cases recomputing
begin() takes O(1) time anyway.

libstdc++-v3/ChangeLog:

	* include/std/ranges (__detail::_CachedPosition): New struct.
	(views::filter_view::_S_needs_cached_begin): New member variable.
	(views::filter_view::_M_cached_begin): New member variable.
	(views::filter_view::begin): Use _M_cached_begin to cache its
	result.
	(views::drop_view::_S_needs_cached_begin): New static member variable.
	(views::drop_view::_M_cached_begin): New member variable.
	(views::drop_view::begin): Use _M_cached_begin to cache its result
	when _S_needs_cached_begin.
	(views::drop_while_view::_M_cached_begin): New member variable.
	(views::drop_while_view::begin): Use _M_cached_begin to cache its
	result.
	(views::reverse_view::_S_needs_cached_begin): New static member
	variable.
	(views::reverse_view::_M_cached_begin): New member variable.
	(views::reverse_view::begin): Use _M_cached_begin to cache its result
	when _S_needs_cached_begin.
	* testsuite/std/ranges/adaptors/drop.cc: Augment test to check that
	drop_view::begin caches its result.
	* testsuite/std/ranges/adaptors/drop_while.cc: Augment test to check
	that drop_while_view::begin caches its result.
	* testsuite/std/ranges/adaptors/filter.cc: Augment test to check that
	filter_view::begin caches its result.
	* testsuite/std/ranges/adaptors/reverse.cc: Augment test to check that
	reverse_view::begin caches its result.
2020-02-28 09:33:03 -05:00
Patrick Palka
ba49e9eb18 libstdc++: Add missing friend declarations in some range adaptors
Some of the range adaptors have distinct constant and non-constant
iterator/sentinel types, along with converting constructors that can convert a
non-constant iterator/sentinel to a constant iterator/sentinel.  This patch adds
the missing appropriate friend declarations in order to make these converting
constructors well formed.

Strictly speaking it seems the friendship relations don't need to go both ways
-- we could get away with declaring e.g. friend _Iterator<false>; instead of
friend _Iterator<!_Const>; but both reference implementations seem to use the
latter symmetric form anyway.

libstdc++-v3/ChangeLog:

	* include/std/ranges (transform_view::_Iterator<_Const>): Befriend
	_Iterator<!_Const>.
	(transform_view::_Sentinel<_Const>): Befriend _Sentinel<!_Const>.
	(take_view::_Sentinel<_Const>): Likewise.
	(take_while_view::_Sentinel<_Const>): Likewise.
	(split_view::_OuterIter<_Const>): Befriend _OuterIter<!_Const>.
	* testsuite/std/ranges/adaptors/split.cc: Augment test.
	* testsuite/std/ranges/adaptors/take.cc: Augment test.
	* testsuite/std/ranges/adaptors/take_while.cc: Augment test.
	* testsuite/std/ranges/adaptors/transform.cc: Augment test.
2020-02-27 12:47:17 -05:00
Patrick Palka
8ce13842b5 libstdc++: Fix use of inaccessible private member in split_view (PR93936)
We are calling _OuterIter::__current from _InnerIter::operator==, but the former
is private within this non-member friend.  Fix this by calling
_OuterIter::operator== instead, which does the right thing here.

libstdc++-v3/ChangeLog:

	PR libstdc++/93936
	* include/std/ranges (split_view::_InnerIter::operator==): Compare
	the operands' _M_i rather than their _M_i.current().
	* testsuite/std/ranges/adaptors/split.cc: Augment test.
2020-02-26 10:24:00 -05:00
Patrick Palka
76a8c0f65e libstdc++: LWG 3397 basic_istream_view::iterator should not provide iterator_category
libstdc++-v3/ChangeLog:

	LWG 3397 basic_istream_view::iterator should not provide
	iterator_category
	* include/std/ranges (basic_istream_view:_Iterator::iterator_category):
	Rename to ...
	(basic_istream_view:_Iterator::iterator_concept): ... this.
	* testsuite/std/ranges/istream_view.cc: Augment test.
2020-02-25 13:04:33 -05:00
Patrick Palka
ec15da7c33 libstdc++: LWG 3325 Constrain return type of transformation function for transform_view
libstdc++-v3/ChangeLog:

	LWG 3325 Constrain return type of transformation function for
	transform_view
	* include/std/ranges (transform_view): Constrain the return type of the
	transformation function as per LWG 3325.
	* testsuite/std/ranges/adaptors/lwg3325_neg.cc: New test.
2020-02-25 13:04:32 -05:00
Patrick Palka
55c4b3f486 libstdc++: LWG 3313 join_view::iterator::operator-- is incorrectly constrained
libstdc++-v3/ChangeLog:

	LWG 3313 join_view::_Iterator::operator-- is incorrectly constrained
	* include/std/ranges (join_view::_Iterator::operator--): Require that
	range_reference_t<_Base> models common_range.
	* testsuite/std/ranges/adaptors/lwg3313_neg.cc: New test.
2020-02-25 13:04:32 -05:00
Patrick Palka
510bd1c178 libstdc++: LWG 3301 transform_view::iterator has incorrect iterator_category
libstdc++-v3/ChangeLog:

	LWG 3301 transform_view::_Iterator has incorrect iterator_category
	* include/std/ranges (transform_view::_Iterator::_S_iter_cat): Adjust
	determination of iterator_category as per LWG 3301.
	* testsuite/std/ranges/adaptors/transform.cc: Augment test.
2020-02-25 13:04:32 -05:00
Patrick Palka
7f0f108309 libstdc++: LWG 3292 iota_view is under-constrained
libstdc++-v3/ChangeLog:

	LWG 3292 iota_view is under-constrained
	* include/std/ranges (iota_view): Require that _Winc models semiregular
	  as per LWG 3292.
	* testsuite/std/ranges/iota/lwg3292_neg.cc: New test.
2020-02-25 12:39:51 -05:00
Jonathan Wakely
3841739c29 libstdc++: enable_view has false positives (LWG 3326)
* include/std/ranges (__deep_const_range, __enable_view_impl): Remove.
	(ranges::enable_view): Simplify (LWG 3326).
	* include/bits/range_access.h (ranges::enable_view): Declare.
	* include/bits/regex.h (__enable_view_impl): Remove partial
	specialization.
	* include/bits/stl_multiset.h (__enable_view_impl): Likewise.
	* include/bits/stl_set.h (__enable_view_impl): Likewise.
	* include/bits/unordered_set.h (__enable_view_impl): Likewise.
	* include/debug/multiset.h (__enable_view_impl): Likewise.
	* include/debug/set.h (__enable_view_impl): Likewise.
	* include/debug/unordered_set (__enable_view_impl): Likewise.
	* include/experimental/string_view (ranges::enable_view): Define
	partial specialization.
	* include/std/span (ranges::enable_view): Likewise.
	* include/std/string_view (ranges::enable_view): Likewise.
	* testsuite/std/ranges/view.cc: Check satisfaction of updated concept.
2020-02-24 12:15:05 +00:00
Patrick Palka
6e63438a0d libstdc++: Fix capturing of lvalue references in_RangeAdaptor::operator()
This fixes a dangling-reference issue with views::split and other multi-argument
adaptors that may take its extra arguments by reference.

When creating the _RangeAdaptorClosure in _RangeAdaptor::operator(), we
currently capture all provided arguments by value.  When we then use the
_RangeAdaptorClosure and call it with a range, as in e.g.

    v = views::split(p)(range),

we forward the range and the captures to the underlying adaptor routine.  But
then when the temporary _RangeAdaptorClosure goes out of scope, the by-value
captures get destroyed and the references to these captures in the resulting view
become dangling.

This patch fixes this problem by capturing lvalue references by reference in
_RangeAdaptorClosure::operator(), and then forwarding the captures appropriately
to the underlying adaptor routine.

libstdc++-v3/ChangeLog:

	* include/std/ranges (views::__adaptor::__maybe_refwrap): New utility
	function.
	(views::__adaptor::_RangeAdaptor::operator()): Add comments.  Use
	__maybe_refwrap to capture lvalue references by reference, and then use
	unwrap_reference_t to forward the by-reference captures as references.
	* testsuite/std/ranges/adaptors/split.cc: Augment test.
	* testsuite/std/ranges/adaptors/split_neg.cc: New test.
2020-02-20 14:06:23 -05:00
Patrick Palka
5586e5060f libstdc++: Forward second argument of views::iota using the correct type
We are forwarding the second argument of views::iota using the wrong type,
causing compile errors when calling views::iota with a value and bound of
different types, like in the test case below.

libstdc++-v3/ChangeLog:

	* include/std/ranges (iota_view): Forward declare _Sentinel.
	(iota_view::_Iterator): Befriend _Sentinel.
	(iota_view::_Sentinel::_M_equal): New member function.
	(iota_view::_Sentinel::operator==): Use it.
	(views::_Iota::operator()): Forward __f using the correct type.
	* testsuite/std/ranges/access/ssize.cc (test06): Don't call views::iota
	with integers of different signedness, to appease iota_view's deduction
	guide.
	* testsuite/std/ranges/iota/iota_view.cc: Augment test.
2020-02-20 13:54:43 -05:00
Jonathan Wakely
e817c23f68 libstdc++: Issues with range access CPOs (P2091R0)
This changes how arrays of unknown bound and/or incomplete element type
are handled.

	* include/bits/range_access.h (ranges::begin): Reject array of
	incomplete type.
	(ranges::end, ranges::size): Require arrays to be bounded.
	(ranges::data): Require lvalue or borrowed_range.
	(ranges::iterator_t): Remove constraint.
	* testsuite/std/ranges/access/begin.cc: Do not check array of
	incomplete type.
	* testsuite/std/ranges/access/begin_neg.cc: New test.
	* testsuite/std/ranges/access/end_neg.cc: Adjust expected error.
	* testsuite/std/ranges/access/size_neg.cc: Adjust expected error.
	* testsuite/std/ranges/access/ssize.cc: Do not check array of
	incomplete type.
2020-02-20 13:22:29 +00:00
Patrick Palka
38c7b74d2e libstdc++: Add missing call to unused subroutine in split_view test
libstdc++-v3/ChangeLog:

	* testsuite/std/ranges/adaptors/split.cc (test03): Don't include the
	null terminator of the underlying string as part of the test_range.
	(main): Call test03.
2020-02-19 17:15:19 -05:00
Jonathan Wakely
77f5310f02 libstdc++: subrange converting constructor should disallow slicing (LWG 3282)
* include/std/ranges (__detail::__convertible_to_non_slicing): New
	helper concept.
	(__detail::__pair_like_convertible_to): Remove.
	(__detail::__pair_like_convertible_from): Add requirements for
	non-slicing conversions.
	(subrange): Constrain constructors with __convertible_to_non_slicing.
	Remove constructors from pair-like types. Add new deduction guide.
	* testsuite/std/ranges/subrange/lwg3282_neg.cc: New test.
2020-02-19 21:21:06 +00:00
Jonathan Wakely
aca60ecff3 libstdc++: Add ranges_size_t and rename all_view (LWG 3335)
* include/bits/range_access.h (range_size_t): Define alias template.
	* include/std/ranges (all_view): Rename to views::all_t (LWG 3335).
	* testsuite/std/ranges/adaptors/filter.cc: Adjust to new name.
2020-02-19 12:46:57 +00:00
Jonathan Wakely
15411a6453 libstdc++: "safe" in several library names is misleading (LWG 3379)
* include/bits/range_access.h (enable_safe_range): Rename to
	enable_borrowed_range.
	(__detail::__maybe_safe_range): Rename to __maybe_borrowed_range.
	(safe_range): Rename to borrowed_range.
	* include/bits/ranges_algo.h: Adjust to use new names.
	* include/bits/ranges_algobase.h: Likewise.
	* include/bits/ranges_uninitialized.h: Likewise.
	* include/std/ranges: Likewise.
	(safe_iterator_t): Rename to borrowed_iterator_t.
	(safe_subrange_t): Rename to borrowed_subrange_t.
	* include/std/span: Adjust to use new names.
	* include/std/string_view: Likewise.
	* include/experimental/string_view: Likewise.
	* testsuite/std/ranges/access/begin.cc: Likewise.
	* testsuite/std/ranges/access/cbegin.cc: Likewise.
	* testsuite/std/ranges/access/cdata.cc: Likewise.
	* testsuite/std/ranges/access/cend.cc: Likewise.
	* testsuite/std/ranges/access/crbegin.cc: Likewise.
	* testsuite/std/ranges/access/crend.cc: Likewise.
	* testsuite/std/ranges/access/data.cc: Likewise.
	* testsuite/std/ranges/access/end.cc: Likewise.
	* testsuite/std/ranges/access/rbegin.cc: Likewise.
	* testsuite/std/ranges/access/rend.cc: Likewise.
	* testsuite/std/ranges/safe_range.cc: Likewise.
	* testsuite/std/ranges/safe_range_types.cc: Likewise.
	* testsuite/util/testsuite_iterators.h: Likewise.
2020-02-19 12:46:57 +00:00
Jonathan Wakely
fa89adaa97 libstdc++: tuple_element_t is also wrong for const subrange (LWG 3398)
* include/std/ranges (tuple_element<0, const subrange<I, S, K>>)
	(tuple_element<1, const subrange<I, S, K>>): Add partial
	specializations (LWG 3398).
	* testsuite/std/ranges/subrange/tuple_like.cc: New test.
2020-02-19 12:46:57 +00:00
Patrick Palka
242b4fb7f4 libstdc++: P1983R0 Wording for GB301, US296, US292, US291, and US283
Among other changes, P1983R0 resolves LWG 3278 in a different way, so this patch
also reverts the already-applied wording of LWG 3278.

The wording for US291 (the join_view::begin hunk) also required adding the
friend _Iterator<!_Const> to join_view::_Iterator.  This friend is needed so
that _Iterator's converting constructor can access the private members of an
_Iterator of the opposite constness.

The wording for US283 has already been applied it seems.

libstdc++-v3/ChangeLog:

	P1983R0 Wording for GB301, US296, US292, US291, and US283
	* include/std/ranges (filter_view::pred): New member function.
	(join_view::_Iterator::_Iterator): Remove now-redundant comment since
	P1983R0 fixes the highlighted issue in the same way.
	(join_view::_Iterator<_Const>): Add friend
	join_view::_Iterator<!_Const>.
	(join_view::_M_inner): Remove mutable specifier, effectively reverting
	the proposed wording changes of P3278.
	(join_view::begin): Refine the condition for when to return a const
	iterator.
	(split_view::_OuterIter::_OuterIter): Adjust constraints.
	* testsuite/std/ranges/adaptors/filter.cc: Test that filter_view::pred
	exists and works.
2020-02-18 23:21:55 -05:00