From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1888) id CBAE7386F019; Tue, 22 Sep 2020 03:22:55 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CBAE7386F019 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1600744975; bh=xC7LBDvStlPHRuaWPr5hmSA+joNQpWaXZYFhbv6hb6Y=; h=From:To:Subject:Date:From; b=KZ4Tw5MF7Erx5+JQT/pf46KLgl2dMor282yLk/BYZUumVRR2WCBgCbRUDQI1eYdA0 g87Vt9FtmXAzjTmeBqc6drRpnrwbGWfHP7/QtIWKYrCjX4zukLavD1FaTFWknacgrk l+u/geyzxu5p0pgxBvArqJwFnfNZ5Z5mqlrKjCbc= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Patrick Palka To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r11-3346] libstdc++: Remove overzealous static_asserts from std::span X-Act-Checkin: gcc X-Git-Author: Patrick Palka X-Git-Refname: refs/heads/master X-Git-Oldrev: 813ad9c4dd5a779f12ad2abf710c6e75a3117ef0 X-Git-Newrev: 37edf28c24b7bd198c27d266af9aefad417635fd Message-Id: <20200922032255.CBAE7386F019@sourceware.org> Date: Tue, 22 Sep 2020 03:22:55 +0000 (GMT) X-BeenThere: libstdc++-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libstdc++-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 22 Sep 2020 03:22:55 -0000 https://gcc.gnu.org/g:37edf28c24b7bd198c27d266af9aefad417635fd commit r11-3346-g37edf28c24b7bd198c27d266af9aefad417635fd Author: Patrick Palka Date: Mon Sep 21 20:53:17 2020 -0400 libstdc++: Remove overzealous static_asserts from std::span For a span with statically empty extent, we currently model the preconditions of front(), back(), and operator[] as if they are mandates, by using a static_assert to verify that extent != 0. This causes us to reject valid programs that would instantiate these member functions and at runtime never call them. Since they are already followed by more general runtime asserts, this patch just removes these static_asserts altogether, libstdc++-v3/ChangeLog: * include/std/span (span::front): Remove static_assert. (span::back): Likewise. (span::operator[]): Likewise. * testsuite/23_containers/span/back_neg.cc: Rewrite to verify that we check the preconditions of back() only when it's called. * testsuite/23_containers/span/front_neg.cc: Likewise for front(). * testsuite/23_containers/span/index_op_neg.cc: Likewise for operator[]. Diff: --- libstdc++-v3/include/std/span | 3 --- libstdc++-v3/testsuite/23_containers/span/back_neg.cc | 14 ++++++++++---- libstdc++-v3/testsuite/23_containers/span/front_neg.cc | 14 ++++++++++---- libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc | 14 ++++++++++---- 4 files changed, 30 insertions(+), 15 deletions(-) diff --git a/libstdc++-v3/include/std/span b/libstdc++-v3/include/std/span index f658adb04cf..1cdc0589ddb 100644 --- a/libstdc++-v3/include/std/span +++ b/libstdc++-v3/include/std/span @@ -264,7 +264,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr reference front() const noexcept { - static_assert(extent != 0); __glibcxx_assert(!empty()); return *this->_M_ptr; } @@ -272,7 +271,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr reference back() const noexcept { - static_assert(extent != 0); __glibcxx_assert(!empty()); return *(this->_M_ptr + (size() - 1)); } @@ -280,7 +278,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION constexpr reference operator[](size_type __idx) const noexcept { - static_assert(extent != 0); __glibcxx_assert(__idx < size()); return *(this->_M_ptr + __idx); } diff --git a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc index c451ed10df8..f777edfa20c 100644 --- a/libstdc++-v3/testsuite/23_containers/span/back_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/back_neg.cc @@ -20,10 +20,16 @@ #include -void -test01() +constexpr bool +test01(bool b) { std::span s; - s.back(); // { dg-error "here" } + if (b || !s.empty()) + s.back(); + return true; } -// { dg-error "static assertion failed" "" { target *-*-* } 0 } + +static_assert(test01(false)); +static_assert(test01(true)); // { dg-error "non-constant" } +// { dg-error "assert" "" { target *-*-* } 0 } +// { dg-prune-output "in 'constexpr' expansion" } diff --git a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc index 38f87aa2cd5..14e5bc1e100 100644 --- a/libstdc++-v3/testsuite/23_containers/span/front_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/front_neg.cc @@ -20,10 +20,16 @@ #include -void -test01() +constexpr bool +test01(bool b) { std::span s; - s.front(); // { dg-error "here" } + if (b || !s.empty()) + s.front(); + return true; } -// { dg-error "static assertion failed" "" { target *-*-* } 0 } + +static_assert(test01(false)); +static_assert(test01(true)); // { dg-error "non-constant" } +// { dg-error "assert" "" { target *-*-* } 0 } +// { dg-prune-output "in 'constexpr' expansion" } diff --git a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc index 1e8b2d8724e..6a3bb8834b4 100644 --- a/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc +++ b/libstdc++-v3/testsuite/23_containers/span/index_op_neg.cc @@ -20,10 +20,16 @@ #include -void -test01() +constexpr bool +test01(bool b) { std::span s; - s[99]; // { dg-error "here" } + if (b || !s.empty()) + s[99]; + return true; } -// { dg-error "static assertion failed" "" { target *-*-* } 0 } + +static_assert(test01(false)); +static_assert(test01(true)); // { dg-error "non-constant" } +// { dg-error "assert" "" { target *-*-* } 0 } +// { dg-prune-output "in 'constexpr' expansion" }