public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "carlosgalvezp at gmail dot com" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member Date: Fri, 05 May 2023 09:50:13 +0000 [thread overview] Message-ID: <bug-109745-4@http.gcc.gnu.org/bugzilla/> (raw) https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109745 Bug ID: 109745 Summary: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: carlosgalvezp at gmail dot com Target Milestone: --- Hi! We are bumping our GCC installation from 6910cad55ffc330dc9767d2c8e0b66ccfa4134af to 07c52d1eec9671af92b7ce977b469f13a87887ad and one of our unit tests fails. I have managed to reduce the code to the following minimal example, compiled with -std=c++14 -O1: #include <cassert> #include <new> template <class T> class Foo { public: constexpr Foo() : has_value_{true} {} Foo(Foo const& other) { if (other.hasValue()) { static_cast<void>(new (&value_) T(other.value())); has_value_ = true; } } constexpr bool hasValue() const { return has_value_; } constexpr T const& value() const { return value_; } private: T value_{}; bool has_value_{false}; }; enum class State { initialized, copy_constructed, copied_from, }; class Stateful { public: constexpr Stateful() = default; constexpr Stateful(Stateful const& other) : state_{State::copy_constructed} { other.state_ = State::copied_from; } constexpr State state() const { return state_; } private: mutable State state_{State::initialized}; }; int main() { constexpr Foo<Stateful> x{}; const Foo<Stateful> y{x}; assert(State::copied_from == x.value().state()); } Godbolt: https://godbolt.org/z/oTd8M9P91 The problem seems to also appear between GCC 12.2 and 13.1. The test runs fine on Clang trunk. One observation is that if I make "x" 'const' instead of 'constexpr', the test passes. Do we have UB in our code, or is this an actual regression in GCC? Thanks!
next reply other threads:[~2023-05-05 9:50 UTC|newest] Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top 2023-05-05 9:50 carlosgalvezp at gmail dot com [this message] 2023-05-05 9:52 ` [Bug c++/109745] " carlosgalvezp at gmail dot com 2023-05-05 11:56 ` albin at yahoo dot com 2023-05-05 15:04 ` [Bug c++/109745] [13/14 Regression] " mpolacek at gcc dot gnu.org 2023-05-11 15:01 ` ppalka at gcc dot gnu.org 2023-05-11 20:31 ` cvs-commit at gcc dot gnu.org 2023-05-11 20:32 ` [Bug c++/109745] [13 " ppalka at gcc dot gnu.org 2023-05-12 15:08 ` cvs-commit at gcc dot gnu.org 2023-05-12 15:22 ` ppalka at gcc dot gnu.org 2023-05-12 21:17 ` carlosgalvezp at gmail dot com
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-109745-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: linkBe 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).