public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
@ 2023-05-05  9:50 carlosgalvezp at gmail dot com
  2023-05-05  9:52 ` [Bug c++/109745] " carlosgalvezp at gmail dot com
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: carlosgalvezp at gmail dot com @ 2023-05-05  9:50 UTC (permalink / raw)
  To: gcc-bugs

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!

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

* [Bug c++/109745] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
@ 2023-05-05  9:52 ` carlosgalvezp at gmail dot com
  2023-05-05 11:56 ` albin at yahoo dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: carlosgalvezp at gmail dot com @ 2023-05-05  9:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Carlos Galvez <carlosgalvezp at gmail dot com> ---
In case it wasn't clear: the test passes also on O0 - it only fails when
increasing from O1 all the way to O3.

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

* [Bug c++/109745] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
  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
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: albin at yahoo dot com @ 2023-05-05 11:56 UTC (permalink / raw)
  To: gcc-bugs

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

albin <albin at yahoo dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |albin at yahoo dot com

--- Comment #2 from albin <albin at yahoo dot com> ---
Simpler example:

#include <cassert>

template <typename T>
struct Foo
{
    T val;
};

class Bar {
   public:
    constexpr Bar() = default;
    constexpr Bar(Bar const& other) { other.val_ = 42; }
    constexpr int val() const { return val_; }
 private:
    mutable int val_{};
};

int main()
{
    constexpr Foo<Bar> x{};
    Foo<Bar> y{x};
    assert(x.val.val() == 42);
}

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

* [Bug c++/109745] [13/14 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
  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 ` mpolacek at gcc dot gnu.org
  2023-05-11 15:01 ` ppalka at gcc dot gnu.org
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-05-05 15:04 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
            Summary|Incorrect code generated    |[13/14 Regression]
                   |with -O1 when having a      |Incorrect code generated
                   |constexpr object modifying  |with -O1 when having a
                   |a mutable member            |constexpr object modifying
                   |                            |a mutable member
                 CC|                            |mpolacek at gcc dot gnu.org,
                   |                            |ppalka at gcc dot gnu.org
   Last reconfirmed|                            |2023-05-05
           Priority|P3                          |P2
   Target Milestone|---                         |13.2
             Status|UNCONFIRMED                 |NEW

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Looks like it started with r13-2701:

commit 7107ea6fb933f1e928593c7e92edfd64ccf0df63
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Sep 16 11:10:43 2022 -0400

    c++: 'mutable' member within constexpr [PR92505]

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

* [Bug c++/109745] [13/14 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (2 preceding siblings ...)
  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
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-05-11 15:01 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |ppalka at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

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

* [Bug c++/109745] [13/14 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (3 preceding siblings ...)
  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
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-11 20:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:02777f20be4f40160f1b4ed34fa59ba75245b5b7

commit r14-742-g02777f20be4f40160f1b4ed34fa59ba75245b5b7
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu May 11 16:31:33 2023 -0400

    c++: 'mutable' subobject of constexpr variable [PR109745]

    r13-2701-g7107ea6fb933f1 made us correctly accept during constexpr
    evaluation 'mutable' member accesses of objects constructed during
    that evaluation, while continuing to reject such accesses for constexpr
    objects constructed outside of that evaluation, by considering the
    CONSTRUCTOR_MUTABLE_POISON flag during cxx_eval_component_reference.

    However, this flag is set only for the outermost CONSTRUCTOR of a
    constexpr variable initializer, so if we're accessing a 'mutable' member
    of a nested CONSTRUCTOR, the flag won't be set and we won't reject the
    access.  This can lead to us accepting invalid code, as in the first
    testcase, or even wrong code generation due to our speculative constexpr
    evaluation, as in the second and third testcase.

    This patch fixes this by setting CONSTRUCTOR_MUTABLE_POISON recursively
    rather than only on the outermost CONSTRUCTOR.

            PR c++/109745

    gcc/cp/ChangeLog:

            * typeck2.cc (poison_mutable_constructors): Define.
            (store_init_value): Use it instead of setting
            CONSTRUCTOR_MUTABLE_POISON directly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/constexpr-mutable4.C: New test.
            * g++.dg/cpp0x/constexpr-mutable5.C: New test.
            * g++.dg/cpp1y/constexpr-mutable2.C: New test.

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

* [Bug c++/109745] [13 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (4 preceding siblings ...)
  2023-05-11 20:31 ` cvs-commit at gcc dot gnu.org
@ 2023-05-11 20:32 ` ppalka at gcc dot gnu.org
  2023-05-12 15:08 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-05-11 20:32 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|[13/14 Regression]          |[13 Regression] Incorrect
                   |Incorrect code generated    |code generated with -O1
                   |with -O1 when having a      |when having a constexpr
                   |constexpr object modifying  |object modifying a mutable
                   |a mutable member            |member
           Keywords|                            |accepts-invalid

--- Comment #5 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed on trunk so far.

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

* [Bug c++/109745] [13 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (5 preceding siblings ...)
  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
  8 siblings, 0 replies; 10+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-12 15:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-13 branch has been updated by Patrick Palka
<ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:7f962e7003a51cc752ed79cbc7fe55ca66981c7a

commit r13-7326-g7f962e7003a51cc752ed79cbc7fe55ca66981c7a
Author: Patrick Palka <ppalka@redhat.com>
Date:   Thu May 11 16:31:33 2023 -0400

    c++: 'mutable' subobject of constexpr variable [PR109745]

    r13-2701-g7107ea6fb933f1 made us correctly accept during constexpr
    evaluation 'mutable' member accesses of objects constructed during
    that evaluation, while continuing to reject such accesses for constexpr
    objects constructed outside of that evaluation, by considering the
    CONSTRUCTOR_MUTABLE_POISON flag during cxx_eval_component_reference.

    However, this flag is set only for the outermost CONSTRUCTOR of a
    constexpr variable initializer, so if we're accessing a 'mutable' member
    of a nested CONSTRUCTOR, the flag won't be set and we won't reject the
    access.  This can lead to us accepting invalid code, as in the first
    testcase, or even wrong code generation due to our speculative constexpr
    evaluation, as in the second and third testcase.

    This patch fixes this by setting CONSTRUCTOR_MUTABLE_POISON recursively
    rather than only on the outermost CONSTRUCTOR.

            PR c++/109745

    gcc/cp/ChangeLog:

            * typeck2.cc (poison_mutable_constructors): Define.
            (store_init_value): Use it instead of setting
            CONSTRUCTOR_MUTABLE_POISON directly.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/constexpr-mutable4.C: New test.
            * g++.dg/cpp0x/constexpr-mutable5.C: New test.
            * g++.dg/cpp1y/constexpr-mutable2.C: New test.

    (cherry picked from commit 02777f20be4f40160f1b4ed34fa59ba75245b5b7)

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

* [Bug c++/109745] [13 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (6 preceding siblings ...)
  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
  8 siblings, 0 replies; 10+ messages in thread
From: ppalka at gcc dot gnu.org @ 2023-05-12 15:22 UTC (permalink / raw)
  To: gcc-bugs

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

Patrick Palka <ppalka at gcc dot gnu.org> changed:

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

--- Comment #7 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 13.2, thanks for the bug report and the nice reproducer!

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

* [Bug c++/109745] [13 Regression] Incorrect code generated with -O1 when having a constexpr object modifying a mutable member
  2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
                   ` (7 preceding siblings ...)
  2023-05-12 15:22 ` ppalka at gcc dot gnu.org
@ 2023-05-12 21:17 ` carlosgalvezp at gmail dot com
  8 siblings, 0 replies; 10+ messages in thread
From: carlosgalvezp at gmail dot com @ 2023-05-12 21:17 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Carlos Galvez <carlosgalvezp at gmail dot com> ---
Thanks a lot for the quick fix!

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

end of thread, other threads:[~2023-05-12 21:17 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-05  9:50 [Bug c++/109745] New: Incorrect code generated with -O1 when having a constexpr object modifying a mutable member carlosgalvezp at gmail dot com
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

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).