public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "delleyves at gmx dot ch" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/104785] New: Generated defaulted constexpr operator== produces wrong code
Date: Fri, 04 Mar 2022 15:29:37 +0000	[thread overview]
Message-ID: <bug-104785-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 104785
           Summary: Generated defaulted constexpr operator== produces
                    wrong code
           Product: gcc
           Version: 11.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: delleyves at gmx dot ch
  Target Milestone: ---

It seems that the code generated for a defaulted constexpr `operator==` for a
struct that inherits a field whose type contains another such `operator==` does
not take that field into account. Removing constexpr on the top-most
`operator==` restores correct behaviour. Therefore, this is likely related to
#102490, although that one deals with bitfields. The behaviour seems to be
present on all versions of GCC supporting C++20's defaulted equality operators,
I did test 10.1 through 11.2 on godbolt. 

In the following example (godbolt link: https://gcc.godbolt.org/z/7nE67fKxc),
the result is wrong only for the `D` type:

#include <iostream>
struct A
{
    int a;

    [[nodiscard]] constexpr bool operator==(const A&) const = default;
};

struct B
{
    A a;
    int b;

    [[nodiscard]] constexpr bool operator==(const B&) const = default;
};

struct C : B
{
    int c;

    [[nodiscard]] bool operator==(const C&) const = default;
};

struct D : B
{
    int d;

    [[nodiscard]] constexpr bool operator==(const D&) const = default;
};


std::ostream &operator<<(std::ostream &os, const A &a) {
    os << "A("<<a.a<<")";
    return os;
}
std::ostream &operator<<(std::ostream &os, const B &b) {
    os << "B("<<b.a<<", "<<b.b<<")";
    return os;
}
std::ostream &operator<<(std::ostream &os, const C &c) {
    os << "C("<<c.a<<", "<<c.b<<", "<<c.c<<")";
    return os;
}
std::ostream &operator<<(std::ostream &os, const D &d) {
    os << "D("<<d.a<<", "<<d.b<<", "<<d.d<<")";
    return os;
}


int main()
{
    A a1{1}, a2{2};
    B b1{a1,2}, b2{a2,2};
    C c1{b1,3}, c2{b2,3};
    D d1{b1,3}, d2{b2,3};

#define CHECK(x,y) std::cout << x << " == " << y << "? " << ((x==y) ? "true" :
"false") << std::endl;
    CHECK(a1,a2);
    CHECK(b1,b2);
    CHECK(c1,c2);
    CHECK(d1,d2);
}

             reply	other threads:[~2022-03-04 15:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04 15:29 delleyves at gmx dot ch [this message]
2022-03-05  4:43 ` [Bug c++/104785] " pinskia at gcc dot gnu.org
2022-03-05  4:45 ` pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-104785-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).