public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object
@ 2020-10-14 19:14 ldalessandro at gmail dot com
  2020-10-14 19:17 ` [Bug c++/97427] " mpolacek at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: ldalessandro at gmail dot com @ 2020-10-14 19:14 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 97427
           Summary: constexpr destructor for const object incorrectly
                    rejected as modifying const object
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ldalessandro at gmail dot com
  Target Milestone: ---

The destructor below is rejected (see https://godbolt.org/z/M8YxTY). Workaround
for now is to mark n as mutable.

```
struct Foo {
    int n = 1;
    constexpr ~Foo() {
        n = 0;
    }
};

constexpr bool foo() {
    const Foo b;
    return true;
}

static_assert(foo());
```

The write to n is flagged as below.


```
<source>:13:18: error: non-constant condition for static assertion
   13 | static_assert(foo());
      |               ~~~^~
<source>:13:18:   in 'constexpr' expansion of 'foo()'
<source>:9:15:   in 'constexpr' expansion of '((Foo*)(& b))->Foo::~Foo()'
<source>:4:11: error: modifying a const object '((Foo*)this)->Foo::n' is not
allowed in a constant expression
    4 |         n = 0;
```

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

* [Bug c++/97427] constexpr destructor for const object incorrectly rejected as modifying const object
  2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
@ 2020-10-14 19:17 ` mpolacek at gcc dot gnu.org
  2020-11-21 20:33 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-10-14 19:17 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |rejects-valid
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-10-14
                 CC|                            |mpolacek at gcc dot gnu.org
             Status|UNCONFIRMED                 |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Hmm, this is probably mine.

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

* [Bug c++/97427] constexpr destructor for const object incorrectly rejected as modifying const object
  2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
  2020-10-14 19:17 ` [Bug c++/97427] " mpolacek at gcc dot gnu.org
@ 2020-11-21 20:33 ` cvs-commit at gcc dot gnu.org
  2020-11-21 20:34 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-11-21 20:33 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:caf17f3afa83623c0f538f6c91c7699c4fdd5674

commit r11-5228-gcaf17f3afa83623c0f538f6c91c7699c4fdd5674
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Nov 19 17:56:39 2020 -0500

    c++: Fix wrong error with constexpr destructor [PR97427]

    When I implemented the code to detect modifying const objects in
    constexpr contexts, we couldn't have constexpr destructors, so I didn't
    consider them.  But now we can and that caused a bogus error in this
    testcase: [class.dtor]p5 says that "const and volatile semantics are not
    applied on an object under destruction.  They stop being in effect when
    the destructor for the most derived object starts." so we have to clear
    the TREE_READONLY flag we set on the object after the constructors have
    been called to mark it as no-longer-under-construction.  In the ~Foo
    call it's now an object under destruction, so don't report those errors.

    gcc/cp/ChangeLog:

            PR c++/97427
            * constexpr.c (cxx_set_object_constness): New function.
            (cxx_eval_call_expression): Set new_obj for destructors too.
            Call cxx_set_object_constness to set/unset TREE_READONLY of
            the object under construction/destruction.

    gcc/testsuite/ChangeLog:

            PR c++/97427
            * g++.dg/cpp2a/constexpr-dtor10.C: New test.

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

* [Bug c++/97427] constexpr destructor for const object incorrectly rejected as modifying const object
  2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
  2020-10-14 19:17 ` [Bug c++/97427] " mpolacek at gcc dot gnu.org
  2020-11-21 20:33 ` cvs-commit at gcc dot gnu.org
@ 2020-11-21 20:34 ` mpolacek at gcc dot gnu.org
  2021-01-05 21:29 ` cvs-commit at gcc dot gnu.org
  2021-01-05 21:34 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-11-21 20:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed on trunk.  I plan to backport to 10.

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

* [Bug c++/97427] constexpr destructor for const object incorrectly rejected as modifying const object
  2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
                   ` (2 preceding siblings ...)
  2020-11-21 20:34 ` mpolacek at gcc dot gnu.org
@ 2021-01-05 21:29 ` cvs-commit at gcc dot gnu.org
  2021-01-05 21:34 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-01-05 21:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:4a8a0d38b128d36a0cf1ac6a7879307b937418ee

commit r10-9204-g4a8a0d38b128d36a0cf1ac6a7879307b937418ee
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Jan 5 16:27:30 2021 -0500

    c++: Fix wrong error with constexpr destructor [PR97427]

    When I implemented the code to detect modifying const objects in
    constexpr contexts, we couldn't have constexpr destructors, so I didn't
    consider them.  But now we can and that caused a bogus error in this
    testcase: [class.dtor]p5 says that "const and volatile semantics are not
    applied on an object under destruction.  They stop being in effect when
    the destructor for the most derived object starts." so we have to clear
    the TREE_READONLY flag we set on the object after the constructors have
    been called to mark it as no-longer-under-construction.  In the ~Foo
    call it's now an object under destruction, so don't report those errors.

    gcc/cp/ChangeLog:

            PR c++/97427
            * constexpr.c (cxx_set_object_constness): New function.
            (cxx_eval_call_expression): Set new_obj for destructors too.
            Call cxx_set_object_constness to set/unset TREE_READONLY of
            the object under construction/destruction.

    gcc/testsuite/ChangeLog:

            PR c++/97427
            * g++.dg/cpp2a/constexpr-dtor10.C: New test.

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

* [Bug c++/97427] constexpr destructor for const object incorrectly rejected as modifying const object
  2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
                   ` (3 preceding siblings ...)
  2021-01-05 21:29 ` cvs-commit at gcc dot gnu.org
@ 2021-01-05 21:34 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2021-01-05 21:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2021-01-05 21:34 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-14 19:14 [Bug c++/97427] New: constexpr destructor for const object incorrectly rejected as modifying const object ldalessandro at gmail dot com
2020-10-14 19:17 ` [Bug c++/97427] " mpolacek at gcc dot gnu.org
2020-11-21 20:33 ` cvs-commit at gcc dot gnu.org
2020-11-21 20:34 ` mpolacek at gcc dot gnu.org
2021-01-05 21:29 ` cvs-commit at gcc dot gnu.org
2021-01-05 21:34 ` mpolacek 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).