public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
@ 2020-08-30 13:09 milasudril at gmail dot com
  2020-08-30 13:15 ` [Bug libstdc++/96851] " milasudril at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: milasudril at gmail dot com @ 2020-08-30 13:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

            Bug ID: 96851
           Summary: operator< on std::array<T, N> does not work in
                    constexpr, for sizeof(T) == 1, and N > 1
           Product: gcc
           Version: 10.1.0
               URL: https://gcc.godbolt.org/z/vjvYE5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: milasudril at gmail dot com
  Target Milestone: ---

It appears that `operator<` on `std::array<T, N>` does not work in constexpr
context, for `sizeof(T) == 1`, and `N > 1`. I tried different `T`and `N`
combinations, and it appears that these work.

MWE:

#include <array>
#include <cstddef>
#include <cstdint>

template<class T, size_t N>
constexpr bool test(std::array<T, N> const& a, std::array<T, N> const& b)
{
    return a < b;
}

using Type = int8_t;
constexpr auto Count = 2;

// Below does not compile
constexpr auto value = test(std::array<Type, Count>{}, std::array<Type,
Count>{});

The problem exists in gcc 10.1 and trunk.

/opt/compiler-explorer/gcc-trunk-20200830/include/c++/11.0.0/array:262:32:
error: '__builtin_memcmp(((std::array<signed char,
2>::const_pointer)(&<anonymous>.std::array<signed char, 2>::_M_elems)),
((std::array<signed char, 2>::const_pointer)(&<anonymous>.std::array<signed
char, 2>::_M_elems)), 2)' is not a constant expression

  262 |         return __builtin_memcmp(__a.data(), __b.data(), _Nm) <=> 0;


Godbolt url: https://gcc.godbolt.org/z/vjvYE5

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
@ 2020-08-30 13:15 ` milasudril at gmail dot com
  2020-09-02  8:54 ` redi at gcc dot gnu.org
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: milasudril at gmail dot com @ 2020-08-30 13:15 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #1 from milasudril at gmail dot com ---
Apparently, std::lexicographical_compare works

https://gcc.godbolt.org/z/E1ETh1

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
  2020-08-30 13:15 ` [Bug libstdc++/96851] " milasudril at gmail dot com
@ 2020-09-02  8:54 ` redi at gcc dot gnu.org
  2020-09-02  9:08 ` redi at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-02  8:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
   Last reconfirmed|                            |2020-09-02
     Ever confirmed|0                           |1
   Target Milestone|---                         |10.3
           Assignee|unassigned at gcc dot gnu.org      |redi at gcc dot gnu.org

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
  2020-08-30 13:15 ` [Bug libstdc++/96851] " milasudril at gmail dot com
  2020-09-02  8:54 ` redi at gcc dot gnu.org
@ 2020-09-02  9:08 ` redi at gcc dot gnu.org
  2020-09-02 14:51 ` cvs-commit at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-02  9:08 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The instructions at https://gcc.gnu.org/bugs are clear about providing the
commands used to compile, please remember to state that you used -std=c++20 (a
godbolt URL is not sufficient).

The branch using memcmp shouldn't be used during constant evaluation, but it
also shouldn't be used for signed chars, so I'll fix that too.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (2 preceding siblings ...)
  2020-09-02  9:08 ` redi at gcc dot gnu.org
@ 2020-09-02 14:51 ` cvs-commit at gcc dot gnu.org
  2020-09-02 15:17 ` milasudril at gmail dot com
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-02 14:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jonathan Wakely <redi@gcc.gnu.org>:

https://gcc.gnu.org/g:2f983fa69005b603ea1758a013b4134d5b0f24a8

commit r11-2981-g2f983fa69005b603ea1758a013b4134d5b0f24a8
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Sep 2 15:17:24 2020 +0100

    libstdc++: Fix three-way comparison for std::array [PR 96851]

    The spaceship operator for std::array uses memcmp when the
    __is_byte<value_type> trait is true, but memcmp isn't usable in
    constexpr contexts. Also, memcmp should only be used for unsigned byte
    types, because it gives the wrong answer for signed chars with negative
    values.

    We can simply check std::is_constant_evaluated() so that we don't use
    memcmp during constant evaluation.

    To fix the problem of using memcmp for inappropriate types, this patch
    adds new __is_memcmp_ordered and __is_memcmp_ordered_with traits. These
    say whether using memcmp will give the right answer for ordering
    operations such as lexicographical_compare and three-way comparisons.
    The new traits can be used in several places, and can also be used to
    implement my suggestion in PR 93059 comment 37 to use memcmp for
    unsigned integers larger than one byte on big endian targets.

    libstdc++-v3/ChangeLog:

            PR libstdc++/96851
            * include/bits/cpp_type_traits.h (__is_memcmp_ordered):
            New trait that says if memcmp can be used for ordering.
            (__is_memcmp_ordered_with): Likewise, for two types.
            * include/bits/deque.tcc (__lex_cmp_dit): Use new traits
            instead of __is_byte and __numeric_traits.
            (__lexicographical_compare_aux1): Likewise.
            * include/bits/ranges_algo.h (__lexicographical_compare_fn):
            Likewise.
            * include/bits/stl_algobase.h (__lexicographical_compare_aux1)
            (__is_byte_iter): Likewise.
            * include/std/array (operator<=>): Likewise. Only use memcmp
            when std::is_constant_evaluated() is false.
            * testsuite/23_containers/array/comparison_operators/96851.cc:
            New test.
            * testsuite/23_containers/array/tuple_interface/get_neg.cc:
            Adjust dg-error line numbers.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (3 preceding siblings ...)
  2020-09-02 14:51 ` cvs-commit at gcc dot gnu.org
@ 2020-09-02 15:17 ` milasudril at gmail dot com
  2020-09-02 15:25 ` redi at gcc dot gnu.org
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: milasudril at gmail dot com @ 2020-09-02 15:17 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #4 from milasudril at gmail dot com ---
Actually, I did not even try without the c++20 flag:

template< class T, std::size_t N >
bool operator<( const std::array<T,N>& lhs,
                const std::array<T,N>& rhs );
        (3)     (until C++20)

...

template< class T, std::size_t N >
constexpr /* see below */ operator<=>( const std::array<T,N>& lhs,
                                       const std::array<T,N>& rhs );
        (7)     (since C++20)

https://en.cppreference.com/w/cpp/container/array/operator_cmp

Thus I wouldn't expect it to work in C++17 or below. If it did, it could be
considered a bug.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (4 preceding siblings ...)
  2020-09-02 15:17 ` milasudril at gmail dot com
@ 2020-09-02 15:25 ` redi at gcc dot gnu.org
  2020-09-02 15:42 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-02 15:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to milasudril from comment #4)
> Actually, I did not even try without the c++20 flag:

That's irrelevant. The bug happens when using c++20, so the bug report should
include the options necessary to reproduce it. Whether you tried without c++20
is irrelevant, what matters is that you *did* use c++20 when encountering the
bug. So the bug report should say so.

> Thus I wouldn't expect it to work in C++17 or below. If it did, it could be
> considered a bug.

Yes, but that doesn't mean you don't need to say which options you used when
encountering the bug.

People trying to reproduce the bug should not need to guess how to reproduce
the bug. *I* know it needs C++20 and *you* know it needs C++20, but knowing the
details of each C++ feature should not be necessary for other people to triage
bug reports or try to reproduce them.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (5 preceding siblings ...)
  2020-09-02 15:25 ` redi at gcc dot gnu.org
@ 2020-09-02 15:42 ` cvs-commit at gcc dot gnu.org
  2020-09-02 15:46 ` redi at gcc dot gnu.org
  2021-07-12 11:14 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-09-02 15:42 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Jonathan Wakely
<redi@gcc.gnu.org>:

https://gcc.gnu.org/g:33c34c4c2466fd4fd050ed8e2d5996c35ebdeef6

commit r10-8702-g33c34c4c2466fd4fd050ed8e2d5996c35ebdeef6
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Sep 2 15:17:24 2020 +0100

    libstdc++: Fix three-way comparison for std::array [PR 96851]

    The spaceship operator for std::array uses memcmp when the
    __is_byte<value_type> trait is true, but memcmp isn't usable in
    constexpr contexts. Also, memcmp should only be used for unsigned byte
    types, because it gives the wrong answer for signed chars with negative
    values.

    We can simply check std::is_constant_evaluated() so that we don't use
    memcmp during constant evaluation.

    To fix the problem of using memcmp for inappropriate types, this patch
    adds new __is_memcmp_ordered and __is_memcmp_ordered_with traits. These
    say whether using memcmp will give the right answer for ordering
    operations such as lexicographical_compare and three-way comparisons.
    The new traits can be used in several places.

    Unlike the trunk commit this was backported from, this commit for the
    branch doesn't extend the memcmp optimisations to all unsigned integers
    on big endian targets. Only narrow character types and std::byte will
    use memcmp.

    libstdc++-v3/ChangeLog:

            PR libstdc++/96851
            * include/bits/cpp_type_traits.h (__is_memcmp_ordered):
            New trait that says if memcmp can be used for ordering.
            (__is_memcmp_ordered_with): Likewise, for two types.
            * include/bits/ranges_algo.h (__lexicographical_compare_fn):
            Use new traits instead of __is_byte and __numeric_traits.
            * include/bits/stl_algobase.h (__lexicographical_compare_aux1)
            (__is_byte_iter): Likewise.
            * include/std/array (operator<=>): Likewise. Only use memcmp
            when std::is_constant_evaluated() is false.
            * testsuite/23_containers/array/comparison_operators/96851.cc:
            New test.
            * testsuite/23_containers/array/tuple_interface/get_neg.cc:
            Adjust dg-error line numbers.

    (cherry picked from commit 2f983fa69005b603ea1758a013b4134d5b0f24a8)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (6 preceding siblings ...)
  2020-09-02 15:42 ` cvs-commit at gcc dot gnu.org
@ 2020-09-02 15:46 ` redi at gcc dot gnu.org
  2021-07-12 11:14 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2020-09-02 15:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
Fixed for GCC 10.3, thanks for the report.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [Bug libstdc++/96851] operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1
  2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
                   ` (7 preceding siblings ...)
  2020-09-02 15:46 ` redi at gcc dot gnu.org
@ 2021-07-12 11:14 ` redi at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-07-12 11:14 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96851

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |tobias.bruell at gmail dot com

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
*** Bug 101318 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2021-07-12 11:14 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-30 13:09 [Bug libstdc++/96851] New: operator< on std::array<T, N> does not work in constexpr, for sizeof(T) == 1, and N > 1 milasudril at gmail dot com
2020-08-30 13:15 ` [Bug libstdc++/96851] " milasudril at gmail dot com
2020-09-02  8:54 ` redi at gcc dot gnu.org
2020-09-02  9:08 ` redi at gcc dot gnu.org
2020-09-02 14:51 ` cvs-commit at gcc dot gnu.org
2020-09-02 15:17 ` milasudril at gmail dot com
2020-09-02 15:25 ` redi at gcc dot gnu.org
2020-09-02 15:42 ` cvs-commit at gcc dot gnu.org
2020-09-02 15:46 ` redi at gcc dot gnu.org
2021-07-12 11:14 ` redi at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).