From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 812E0385C6F4 for ; Wed, 19 Jul 2023 22:43:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 812E0385C6F4 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1689806601; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=Ah0tyGVmCEt6GPaJLYUJtbYYyHJIi7kNEIyT4Y4JDiA=; b=hlj8ngzx6snhabVbIDV3CwxuJT1IxJIIxYCHbTfTAjyTstZDODZDpZxB6upb0/ExEUA19J tmP+r9gycUsGbZoYHDOkaFvKSuVNQ+V035Fq2u4vHIZiw4oZ6Ta2quzZ4on5Xm3FZl9zU1 zUTP86DqHMrNX4Kl0kBT4Vc9fngkk+s= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-86-rgyX0FakNc-ZK27QvFBjWQ-1; Wed, 19 Jul 2023 18:43:19 -0400 X-MC-Unique: rgyX0FakNc-ZK27QvFBjWQ-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.rdu2.redhat.com [10.11.54.3]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 774198F1840; Wed, 19 Jul 2023 22:43:19 +0000 (UTC) Received: from localhost (unknown [10.42.28.140]) by smtp.corp.redhat.com (Postfix) with ESMTP id 3ACC31121314; Wed, 19 Jul 2023 22:43:19 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Check for std::ratio in arithmetic and comparisons [PR110593] Date: Wed, 19 Jul 2023 23:43:05 +0100 Message-ID: <20230719224318.2599563-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.3 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- The standard says that it should be ill-formed to use std::ratio_equal etc. with types which are not specializations of std::ratio. This implements that requirement. We don't need to add assertions to every one of the class templates, because many of them are implemented in terms of other ones. For example, ratio_divide and ratio_subtract can rely on the assertions in ratio_multiply and ratio_add respectively. libstdc++-v3/ChangeLog: PR libstdc++/110593 * include/bits/chrono.h (duration): Improve static assert messages. (__is_ratio): Move to ... * include/std/ratio (__is_ratio): ... here. (__is_ratio_v): New variable template and partial specialization. (__are_both_ratios): New function template. (__ratio_multiply, ratio_equal, ratio_less, __ratio_add): Add static assertion. * testsuite/20_util/ratio/requirements/type_constraints.cc: New test. * testsuite/20_util/duration/requirements/typedefs_neg1.cc: Adjust expected error. * testsuite/20_util/duration/requirements/typedefs_neg2.cc: Likewise. --- libstdc++-v3/include/bits/chrono.h | 19 ++----- libstdc++-v3/include/std/ratio | 53 +++++++++++++++++-- .../duration/requirements/typedefs_neg1.cc | 2 +- .../duration/requirements/typedefs_neg2.cc | 2 +- .../ratio/requirements/type_constraints.cc | 34 ++++++++++++ 5 files changed, 87 insertions(+), 23 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/ratio/requirements/type_constraints.cc diff --git a/libstdc++-v3/include/bits/chrono.h b/libstdc++-v3/include/bits/chrono.h index 81b92d724f7..e9490767c56 100644 --- a/libstdc++-v3/include/bits/chrono.h +++ b/libstdc++-v3/include/bits/chrono.h @@ -505,26 +505,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { return numeric_limits<_Rep>::lowest(); } }; - /// @cond undocumented - - template - struct __is_ratio - : std::false_type - { }; - - template - struct __is_ratio> - : std::true_type - { }; - - /// @endcond - template class duration { - static_assert(!__is_duration<_Rep>::value, "rep cannot be a duration"); + static_assert(!__is_duration<_Rep>::value, + "rep cannot be a std::chrono::duration"); static_assert(__is_ratio<_Period>::value, - "period must be a specialization of ratio"); + "period must be a specialization of std::ratio"); static_assert(_Period::num > 0, "period must be positive"); template diff --git a/libstdc++-v3/include/std/ratio b/libstdc++-v3/include/std/ratio index 183f1aeda11..1d285bf916f 100644 --- a/libstdc++-v3/include/std/ratio +++ b/libstdc++-v3/include/std/ratio @@ -289,9 +289,43 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// @cond undocumented + template + struct __is_ratio + : std::false_type + { }; + + template + struct __is_ratio> + : std::true_type + { }; + +#if __cpp_variable_templates + template + constexpr bool __is_ratio_v = false; + template + constexpr bool __is_ratio_v> = true; +#endif + + template + constexpr bool + __are_both_ratios() noexcept + { +#if __cpp_variable_templates && __cpp_if_constexpr + if constexpr (__is_ratio_v<_R1>) + if constexpr (__is_ratio_v<_R2>) + return true; + return false; +#else + return __and_<__is_ratio<_R1>, __is_ratio<_R2>>::value; +#endif + } + template struct __ratio_multiply { + static_assert(std::__are_both_ratios<_R1, _R2>(), + "both template arguments must be a std::ratio"); + private: static const intmax_t __gcd1 = __static_gcd<_R1::num, _R2::den>::value; @@ -356,7 +390,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct ratio_equal : integral_constant - { }; + { + static_assert(std::__are_both_ratios<_R1, _R2>(), + "both template arguments must be a std::ratio"); + }; /// ratio_not_equal template @@ -402,7 +439,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct ratio_less : __ratio_less_impl<_R1, _R2>::type - { }; + { + static_assert(std::__are_both_ratios<_R1, _R2>(), + "both template arguments must be a std::ratio"); + }; /// ratio_less_equal template @@ -430,13 +470,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template inline constexpr bool ratio_less_v = ratio_less<_R1, _R2>::value; template - inline constexpr bool ratio_less_equal_v = - ratio_less_equal<_R1, _R2>::value; + inline constexpr bool ratio_less_equal_v + = ratio_less_equal<_R1, _R2>::value; template inline constexpr bool ratio_greater_v = ratio_greater<_R1, _R2>::value; template inline constexpr bool ratio_greater_equal_v - = ratio_greater_equal<_R1, _R2>::value; + = ratio_greater_equal<_R1, _R2>::value; #endif // C++17 /// @cond undocumented @@ -513,6 +553,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template struct __ratio_add { + static_assert(std::__are_both_ratios<_R1, _R2>(), + "both template arguments must be a std::ratio"); + typedef typename __ratio_add_impl<_R1, _R2>::type type; static constexpr intmax_t num = type::num; static constexpr intmax_t den = type::den; diff --git a/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg1.cc b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg1.cc index 49c9fe77168..1a1188b2a2f 100644 --- a/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg1.cc +++ b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg1.cc @@ -29,4 +29,4 @@ void test01() test_type d; // { dg-error "required from here" } } -// { dg-error "rep cannot be a duration" "" { target *-*-* } 0 } +// { dg-error "rep cannot be a std::chrono::duration" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc index 23c5d479b1f..cbdb7af0c65 100644 --- a/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc +++ b/libstdc++-v3/testsuite/20_util/duration/requirements/typedefs_neg2.cc @@ -30,7 +30,7 @@ void test01() test_type d; // { dg-error "required from here" } } -// { dg-error "must be a specialization of ratio" "" { target *-*-* } 0 } +// { dg-error "must be a specialization of std::ratio" "" { target *-*-* } 0 } // { dg-prune-output "'num' is not a member of 'int'" } // { dg-prune-output "'den' is not a member of 'int'" } // { dg-prune-output "'int' is not a class, struct, or union type" } diff --git a/libstdc++-v3/testsuite/20_util/ratio/requirements/type_constraints.cc b/libstdc++-v3/testsuite/20_util/ratio/requirements/type_constraints.cc new file mode 100644 index 00000000000..ebf09bc8f07 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/ratio/requirements/type_constraints.cc @@ -0,0 +1,34 @@ +// { dg-do compile { target c++11 } } + +// C++11 20.10.1 [ratio.general] +// if the template argument types R1 and R2 are not specializations of the +// ratio template, the program is ill-formed. + +#include + +using namespace std; + +// A type that looks like std::ratio but isn't. +template struct Ratty { static constexpr int num = 1, den = 1; }; + +using T1 = ratio_add, Ratty<1>>::type; // { dg-error "here" } +using T2 = ratio_subtract, Ratty<3>>::type; // { dg-error "here" } +using T3 = ratio_multiply, Ratty<3>>::type; // { dg-error "here" } +using T4 = ratio_divide, Ratty<4>>::type; // { dg-error "here" } +using T5 = ratio_equal, Ratty<5>>::type; // { dg-error "here" } +using T6 = ratio_not_equal, Ratty<6>>::type; // { dg-error "here" } +using T7 = ratio_less, Ratty<7>>::type; // { dg-error "here" } +using T8 = ratio_less_equal, Ratty<8>>::type; // { dg-error "here" } +using T9 = ratio_greater, Ratty<9>>::type; // { dg-error "here" } +using T10 = ratio_greater_equal, Ratty<10>>::type; // { dg-error "here" } + +#if __cplusplus >= 201703L +bool B11 = ratio_equal_v, Ratty<11>>; // { dg-error "here" "" { target c++17 } } +bool B12 = ratio_not_equal_v, Ratty<12>>; // { dg-error "here" "" { target c++17 } } +bool B13 = ratio_less_v, Ratty<13>>; // { dg-error "here" "" { target c++17 } } +bool B14 = ratio_less_equal_v, Ratty<14>>; // { dg-error "here" "" { target c++17 } } +bool B15 = ratio_greater_v, Ratty<15>>; // { dg-error "here" "" { target c++17 } } +bool B16 = ratio_greater_equal_v, Ratty<16>>; // { dg-error "here" "" { target c++17 } } +#endif + +// { dg-error "static assertion failed" "" { target *-*-* } 0 } -- 2.41.0