From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 347CA3858D33; Fri, 5 May 2023 09:50:14 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 347CA3858D33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1683280214; bh=1T6HogjmC9FaQEOZ9XjvJwXCphqd5EeBZ/4taY4nhWg=; h=From:To:Subject:Date:From; b=rZGMcaNf8po++3T5yhd9iMGkQYXXvXDYkjrSOn4WUhOlBpk3AU498jUTGTuD7YsdD wWD1M47vx49UAO+T/MSKKPKTAXBQU/MOKlQ5bzPfKNYXtUwMky6DA/Le/sJ8kP/DDx ILU46S57AFQUbqfTY/zwE8XlQQe0qezBsoouHVp0= From: "carlosgalvezp at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: carlosgalvezp at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D109745 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=3Dc++14 -O1: #include #include template class Foo { public: constexpr Foo() : has_value_{true} {} Foo(Foo const& other) { if (other.hasValue()) { static_cast(new (&value_) T(other.value())); has_value_ =3D 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() =3D default; constexpr Stateful(Stateful const& other) : state_{State::copy_constructed} { other.state_ =3D State::copied_from; } constexpr State state() const { return state_; } private: mutable State state_{State::initialized}; }; int main() { constexpr Foo x{}; const Foo y{x}; assert(State::copied_from =3D=3D 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 t= est passes. Do we have UB in our code, or is this an actual regression in GCC? Thanks!=