From: Jonathan Wakely Date: Thu, 31 Oct 2019 09:39:48 +0000 (+0000) Subject: Check precondition for std::ssize(const Container&) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=164abd6786e5ddab0c3a4a0a9a6cdc67f84b1a17;p=gcc.git Check precondition for std::ssize(const Container&) This precondition is the subject of a national body comment on the C++20 CD. This just adds a test to ensure we enforce the precondition. Also move existing 24_iterator/range_access*.cc tests to a dedicated directory for the [iterator.range] subclause. * testsuite/24_iterators/range_access*.cc: Move to ... * testsuite/24_iterators/range_access/range_access*.cc: ... here. * testsuite/24_iterators/range_access/range_access_cpp20_neg.cc: New test. From-SVN: r277658 --- diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index 059976e9f6f..78ea4476ebe 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +2019-10-31 Jonathan Wakely + + * testsuite/24_iterators/range_access*.cc: Move to ... + * testsuite/24_iterators/range_access/range_access*.cc: ... here. + * testsuite/24_iterators/range_access/range_access_cpp20_neg.cc: New + test. + 2019-10-30 Jonathan Wakely * include/std/concepts (std::ranges::swap): Use a single overload for diff --git a/libstdc++-v3/testsuite/24_iterators/range_access.cc b/libstdc++-v3/testsuite/24_iterators/range_access.cc deleted file mode 100644 index 8b1ac966503..00000000000 --- a/libstdc++-v3/testsuite/24_iterators/range_access.cc +++ /dev/null @@ -1,30 +0,0 @@ -// { dg-do compile { target c++11 } } - -// Copyright (C) 2010-2019 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING3. If not see -// . - -// C++ 2011 24.6.5, range access [iterator.range] - -#include - -void -test01() -{ - int arr[3] = {1, 2, 3}; - std::begin(arr); - std::end(arr); -} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access.cc new file mode 100644 index 00000000000..8b1ac966503 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access.cc @@ -0,0 +1,30 @@ +// { dg-do compile { target c++11 } } + +// Copyright (C) 2010-2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// C++ 2011 24.6.5, range access [iterator.range] + +#include + +void +test01() +{ + int arr[3] = {1, 2, 3}; + std::begin(arr); + std::end(arr); +} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp14.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp14.cc new file mode 100644 index 00000000000..29a469d7d6b --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp14.cc @@ -0,0 +1,79 @@ +// { dg-do run { target c++14 } } + +// Copyright (C) 2015-2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// C++ 2014 24.7, range access [iterator.range] + +#include +#include +#include + +void +test01() +{ + int i[1]; + VERIFY(std::cbegin(i) == i); + VERIFY(std::cend(i) == i+1); + VERIFY(std::rbegin(i) == std::reverse_iterator(i+1)); + VERIFY(std::rend(i) == std::reverse_iterator(i)); + VERIFY(std::crbegin(i) == std::reverse_iterator(i+1)); + VERIFY(std::crend(i) == std::reverse_iterator(i)); +} + +void +test02() +{ + static int i[1]; + constexpr auto b __attribute__((unused)) = std::begin(i); + constexpr auto e __attribute__((unused)) = std::end(i); + constexpr auto cb __attribute__((unused)) = std::cbegin(i); + constexpr auto ce __attribute__((unused)) = std::cend(i); +} + +void +test03() +{ + std::initializer_list il{1}; + VERIFY(std::cbegin(il) == il.begin()); + VERIFY(std::cend(il) == il.end()); + VERIFY(std::rbegin(il) == std::reverse_iterator(il.end())); + VERIFY(std::rend(il) == std::reverse_iterator(il.begin())); + VERIFY(std::crbegin(il) == std::reverse_iterator(il.end())); + VERIFY(std::crend(il) == std::reverse_iterator(il.begin())); +} + +void +test04() +{ + std::vector v{1}; + VERIFY(std::cbegin(v) == v.cbegin()); + VERIFY(std::cend(v) == v.cend()); + VERIFY(std::rbegin(v) == v.rbegin()); + VERIFY(std::rend(v) == v.rend()); + VERIFY(std::crbegin(v) == v.crbegin()); + VERIFY(std::crend(v) == v.crend()); +} + +int +main() +{ + test01(); + test02(); + test03(); + test04(); +} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17.cc new file mode 100644 index 00000000000..9ba20090c77 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17.cc @@ -0,0 +1,57 @@ +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } + +// Copyright (C) 2017-2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// C++ 2017 27.7, range access [iterator.range] + +#include + +void +test01() +{ + using std::reverse_iterator; + static int i[1]; + static_assert(std::cbegin(i) == i); + static_assert(std::cend(i) == i+1); + static_assert(std::rbegin(i) == reverse_iterator(i+1)); + static_assert(std::rend(i) == reverse_iterator(i)); + static_assert(std::crbegin(i) == reverse_iterator(i+1)); + static_assert(std::crend(i) == reverse_iterator(i)); +} + +void +test02() +{ + static int i[] = { 1, 2 }; + static_assert(std::distance(std::begin(i), std::end(i)) == 2); + static_assert(std::distance(std::cbegin(i), std::cend(i)) == 2); +} + +void +test03() +{ + using std::reverse_iterator; + static constexpr std::initializer_list il{1}; + static_assert(std::cbegin(il) == il.begin()); + static_assert(std::cend(il) == il.end()); + static_assert(std::rbegin(il) == reverse_iterator(il.end())); + static_assert(std::rend(il) == reverse_iterator(il.begin())); + static_assert(std::crbegin(il) == reverse_iterator(il.end())); + static_assert(std::crend(il) == reverse_iterator(il.begin())); +} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17_neg.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17_neg.cc new file mode 100644 index 00000000000..6e407fd957a --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp17_neg.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2017-2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++17" } +// { dg-do compile { target c++17 } } + +#include +#include + +void +test01() +{ + struct A { bool empty() const { return true; } }; + A a; + std::empty(a); // { dg-warning "ignoring return value" } +} + +void +test02() +{ + int a[2]; + std::empty(a); // { dg-warning "ignoring return value" } +} + +void +test03() +{ + std::initializer_list a{}; + std::empty(a); // { dg-warning "ignoring return value" } +} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20.cc new file mode 100644 index 00000000000..567b0564507 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20.cc @@ -0,0 +1,67 @@ +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// N4830 23.7, Range access [iterator.range] + +#include + +void +test01() +{ + static int i[1]; + constexpr auto s = std::ssize(i); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 1); +} + +void +test02() +{ + static int i[] = { 1, 2 }; + constexpr auto s = std::ssize(i); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 2); +} + +void +test03() +{ + struct Cont + { + constexpr unsigned short size() const { return 3; } + }; + constexpr Cont c; + constexpr auto s = std::ssize(c); + const std::ptrdiff_t* check_type = &s; + static_assert(s == 3); +} + +void +test04() +{ + struct Cont + { + constexpr unsigned long long size() const { return 4; } + }; + constexpr Cont c; + constexpr auto s = std::ssize(c); + const long long* check_type = &s; + static_assert(s == 4); +} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20_neg.cc b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20_neg.cc new file mode 100644 index 00000000000..8caa941e161 --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/range_access/range_access_cpp20_neg.cc @@ -0,0 +1,49 @@ +// Copyright (C) 2019 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-options "-std=gnu++2a" } +// { dg-do compile { target c++2a } } + +#include + +void +test01() +{ + struct C { unsigned size() { return 0u; } }; + C c; + std::ssize(c); // { dg-error "no matching function" } +} +// { dg-error "discards qualifiers" "" { target *-*-* } 0 } + +void +test02() +{ + struct C { bool size() { return false; } }; + C c; + std::ssize(c); // { dg-error "no matching function" } +} +// { dg-error "incomplete type .*make_signed.*bool" "" { target *-*-* } 0 } + +void +test03() +{ + struct S { }; + struct C { S size() { return {}; } }; + C c; + std::ssize(c); // { dg-error "no matching function" } +} +// { dg-error "incomplete type .*make_signed.*S" "" { target *-*-* } 0 } diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc deleted file mode 100644 index 29a469d7d6b..00000000000 --- a/libstdc++-v3/testsuite/24_iterators/range_access_cpp14.cc +++ /dev/null @@ -1,79 +0,0 @@ -// { dg-do run { target c++14 } } - -// Copyright (C) 2015-2019 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING3. If not see -// . - -// C++ 2014 24.7, range access [iterator.range] - -#include -#include -#include - -void -test01() -{ - int i[1]; - VERIFY(std::cbegin(i) == i); - VERIFY(std::cend(i) == i+1); - VERIFY(std::rbegin(i) == std::reverse_iterator(i+1)); - VERIFY(std::rend(i) == std::reverse_iterator(i)); - VERIFY(std::crbegin(i) == std::reverse_iterator(i+1)); - VERIFY(std::crend(i) == std::reverse_iterator(i)); -} - -void -test02() -{ - static int i[1]; - constexpr auto b __attribute__((unused)) = std::begin(i); - constexpr auto e __attribute__((unused)) = std::end(i); - constexpr auto cb __attribute__((unused)) = std::cbegin(i); - constexpr auto ce __attribute__((unused)) = std::cend(i); -} - -void -test03() -{ - std::initializer_list il{1}; - VERIFY(std::cbegin(il) == il.begin()); - VERIFY(std::cend(il) == il.end()); - VERIFY(std::rbegin(il) == std::reverse_iterator(il.end())); - VERIFY(std::rend(il) == std::reverse_iterator(il.begin())); - VERIFY(std::crbegin(il) == std::reverse_iterator(il.end())); - VERIFY(std::crend(il) == std::reverse_iterator(il.begin())); -} - -void -test04() -{ - std::vector v{1}; - VERIFY(std::cbegin(v) == v.cbegin()); - VERIFY(std::cend(v) == v.cend()); - VERIFY(std::rbegin(v) == v.rbegin()); - VERIFY(std::rend(v) == v.rend()); - VERIFY(std::crbegin(v) == v.crbegin()); - VERIFY(std::crend(v) == v.crend()); -} - -int -main() -{ - test01(); - test02(); - test03(); - test04(); -} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp17.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp17.cc deleted file mode 100644 index 9ba20090c77..00000000000 --- a/libstdc++-v3/testsuite/24_iterators/range_access_cpp17.cc +++ /dev/null @@ -1,57 +0,0 @@ -// { dg-options "-std=gnu++17" } -// { dg-do compile { target c++17 } } - -// Copyright (C) 2017-2019 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING3. If not see -// . - -// C++ 2017 27.7, range access [iterator.range] - -#include - -void -test01() -{ - using std::reverse_iterator; - static int i[1]; - static_assert(std::cbegin(i) == i); - static_assert(std::cend(i) == i+1); - static_assert(std::rbegin(i) == reverse_iterator(i+1)); - static_assert(std::rend(i) == reverse_iterator(i)); - static_assert(std::crbegin(i) == reverse_iterator(i+1)); - static_assert(std::crend(i) == reverse_iterator(i)); -} - -void -test02() -{ - static int i[] = { 1, 2 }; - static_assert(std::distance(std::begin(i), std::end(i)) == 2); - static_assert(std::distance(std::cbegin(i), std::cend(i)) == 2); -} - -void -test03() -{ - using std::reverse_iterator; - static constexpr std::initializer_list il{1}; - static_assert(std::cbegin(il) == il.begin()); - static_assert(std::cend(il) == il.end()); - static_assert(std::rbegin(il) == reverse_iterator(il.end())); - static_assert(std::rend(il) == reverse_iterator(il.begin())); - static_assert(std::crbegin(il) == reverse_iterator(il.end())); - static_assert(std::crend(il) == reverse_iterator(il.begin())); -} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc deleted file mode 100644 index 6e407fd957a..00000000000 --- a/libstdc++-v3/testsuite/24_iterators/range_access_cpp17_neg.cc +++ /dev/null @@ -1,44 +0,0 @@ -// Copyright (C) 2017-2019 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING3. If not see -// . - -// { dg-options "-std=gnu++17" } -// { dg-do compile { target c++17 } } - -#include -#include - -void -test01() -{ - struct A { bool empty() const { return true; } }; - A a; - std::empty(a); // { dg-warning "ignoring return value" } -} - -void -test02() -{ - int a[2]; - std::empty(a); // { dg-warning "ignoring return value" } -} - -void -test03() -{ - std::initializer_list a{}; - std::empty(a); // { dg-warning "ignoring return value" } -} diff --git a/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc b/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc deleted file mode 100644 index 567b0564507..00000000000 --- a/libstdc++-v3/testsuite/24_iterators/range_access_cpp20.cc +++ /dev/null @@ -1,67 +0,0 @@ -// { dg-options "-std=gnu++2a" } -// { dg-do compile { target c++2a } } - -// Copyright (C) 2019 Free Software Foundation, Inc. -// -// This file is part of the GNU ISO C++ Library. This library is free -// software; you can redistribute it and/or modify it under the -// terms of the GNU General Public License as published by the -// Free Software Foundation; either version 3, or (at your option) -// any later version. - -// This library is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. - -// You should have received a copy of the GNU General Public License along -// with this library; see the file COPYING3. If not see -// . - -// N4830 23.7, Range access [iterator.range] - -#include - -void -test01() -{ - static int i[1]; - constexpr auto s = std::ssize(i); - const std::ptrdiff_t* check_type = &s; - static_assert(s == 1); -} - -void -test02() -{ - static int i[] = { 1, 2 }; - constexpr auto s = std::ssize(i); - const std::ptrdiff_t* check_type = &s; - static_assert(s == 2); -} - -void -test03() -{ - struct Cont - { - constexpr unsigned short size() const { return 3; } - }; - constexpr Cont c; - constexpr auto s = std::ssize(c); - const std::ptrdiff_t* check_type = &s; - static_assert(s == 3); -} - -void -test04() -{ - struct Cont - { - constexpr unsigned long long size() const { return 4; } - }; - constexpr Cont c; - constexpr auto s = std::ssize(c); - const long long* check_type = &s; - static_assert(s == 4); -}