public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/104348] New: Incorrect sorting in constexpr constructor
@ 2022-02-02 17:14 asorenji at gmail dot com
  2022-02-02 17:29 ` [Bug c++/104348] " redi at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: asorenji at gmail dot com @ 2022-02-02 17:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 104348
           Summary: Incorrect sorting in constexpr constructor
           Product: gcc
           Version: 10.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: asorenji at gmail dot com
  Target Milestone: ---

I have simple test code, that just sorting array of string_view wrapped in
another class. This array contain list of "1","2","3","4","5","6","7","8"
values and remains unchanged if code executed in runtime. Well, it's already
sorted from beginning. But if constexpr used and code executed it compile time,
I get 17256348 instead of 12345678. For very strangle reason, bug appear in
constexpr variable constructor, but disappear in constinit variable
constructor.

g++ --version - g++ (Debian 10.2.1-6) 10.2.1 20210110
Compile options - g++ -std=c++20 -Wall -Wextra main.cpp -o test
Code:

#include<string_view>
#include<algorithm>
#include<iostream>

class Test
{
public:
    //for some reason, bug don't appear if std::string_view not wrapped in
another class
    class Value
    {
    public:
        constexpr Value(const char*str):data(str){}
        constexpr bool operator<(const Value&value)const{return
data<value.data;}

        std::string_view data;
    };

    constexpr Test()
    {
        //no bug if std::sort used
        //std::sort(array,array+8);
        std::make_heap(array,array+8);
        std::sort_heap(array,array+8);
    }

    Value array[8]={"1","2","3","4","5","6","7","8"};
};

int main()
{
    //product 12345678
    //Test test;

    //also word well
    //static constinit Test test;

    //Bug. Product 17256348 instead of 12345678
    static constexpr Test test;

    for(auto&value:test.array)
        std::cout<<value.data;
    return 0;
}

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

* [Bug c++/104348] Incorrect sorting in constexpr constructor
  2022-02-02 17:14 [Bug c++/104348] New: Incorrect sorting in constexpr constructor asorenji at gmail dot com
@ 2022-02-02 17:29 ` redi at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: redi at gcc dot gnu.org @ 2022-02-02 17:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |DUPLICATE
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This is already fixed on the gcc-10 branch and in 11.1.0

Reduced to compile-time test:

#include<string_view>
#include<algorithm>

class Test
{
public:
    //for some reason, bug don't appear if std::string_view not wrapped in
another class
    class Value
    {
    public:
        constexpr Value(const char*str):data(str){}
        constexpr bool operator<(const Value&value)const{return
data<value.data;}

        std::string_view data;
    };

    constexpr Test()
    {
        //no bug if std::sort used
        //std::sort(array,array+8);
        std::make_heap(array,array+8);
        std::sort_heap(array,array+8);
    }

    Value array[8]={"1","2","3","4","5","6","7","8"};
};

    //Bug. Product 17256348 instead of 12345678
    static constexpr Test test;

    static_assert( test.array[1].data[0] == '2' );


Fixed by r11-8056:

PR c++/99859
* constexpr.c (addr_of_non_const_var): New function.
(cxx_bind_parameters_in_call): Set *non_constant_args to true
even if cp_walk_tree on arg with addr_of_non_const_var callback
returns true.

That was already backported as r10-9722

*** This bug has been marked as a duplicate of bug 99859 ***

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

end of thread, other threads:[~2022-02-02 17:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-02 17:14 [Bug c++/104348] New: Incorrect sorting in constexpr constructor asorenji at gmail dot com
2022-02-02 17:29 ` [Bug c++/104348] " 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).