public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
@ 2021-10-22 15:46 friedkeenan at protonmail dot com
  2021-10-22 17:42 ` [Bug c++/102899] " jakub at gcc dot gnu.org
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: friedkeenan at protonmail dot com @ 2021-10-22 15:46 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102899
           Summary: Performing `delete this` within destructor in constant
                    evaluation results in infinitely recursive error
                    message
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: friedkeenan at protonmail dot com
  Target Milestone: ---

See the following code:

struct Test {
    constexpr Test() = default;

    constexpr ~Test() {
        delete this;
    }
};

consteval bool test() {
    const auto obj = Test{};

    return true;
}

static_assert(test());

Godbolt link: https://godbolt.org/z/fv3dGWsY1

This bug still occurs when `obj` is heap-allocated and destroyed with
`std::destroy_at`.

This issue does not occur if the `delete this` statement is relegated to
another method, including the constructor; it only occurs when `delete this` is
placed within the destructor.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
@ 2021-10-22 17:42 ` jakub at gcc dot gnu.org
  2021-10-22 17:45 ` friedkeenan at protonmail dot com
                   ` (7 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-22 17:42 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Why do you think this is a compiler bug?
If you delete this, you invoke the destructor and then the operator delete, so
when you do this in the destructor, it means you do infinite recursion.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
  2021-10-22 17:42 ` [Bug c++/102899] " jakub at gcc dot gnu.org
@ 2021-10-22 17:45 ` friedkeenan at protonmail dot com
  2021-10-22 17:46 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: friedkeenan at protonmail dot com @ 2021-10-22 17:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from friedkeenan at protonmail dot com ---
(In reply to Jakub Jelinek from comment #1)
> Why do you think this is a compiler bug?
> If you delete this, you invoke the destructor and then the operator delete,
> so when you do this in the destructor, it means you do infinite recursion.

Because this message is really unhelpful, and Clang handles it as I would
expect, where it errors on the second deallocation of `this`, and that's it.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
  2021-10-22 17:42 ` [Bug c++/102899] " jakub at gcc dot gnu.org
  2021-10-22 17:45 ` friedkeenan at protonmail dot com
@ 2021-10-22 17:46 ` jakub at gcc dot gnu.org
  2021-10-22 17:59 ` friedkeenan at protonmail dot com
                   ` (5 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-22 17:46 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Just compile your testcase with additional
-Dconstexpr= -Dconsteval= -D'static_assert(x)='
to see how it is compiled, there is that infinite recursion with all compilers
I've tried.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (2 preceding siblings ...)
  2021-10-22 17:46 ` jakub at gcc dot gnu.org
@ 2021-10-22 17:59 ` friedkeenan at protonmail dot com
  2021-10-22 18:14 ` jakub at gcc dot gnu.org
                   ` (4 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: friedkeenan at protonmail dot com @ 2021-10-22 17:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from friedkeenan at protonmail dot com ---
(In reply to Jakub Jelinek from comment #3)
> Just compile your testcase with additional
> -Dconstexpr= -Dconsteval= -D'static_assert(x)='
> to see how it is compiled, there is that infinite recursion with all
> compilers I've tried.

The difference being that that's runtime which does not have the same heap
safety guarantees as constant evaluation. The recursive error, while
technically compliant with the standard afaik, is not that great from a
figuring out what the issue is perspective.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (3 preceding siblings ...)
  2021-10-22 17:59 ` friedkeenan at protonmail dot com
@ 2021-10-22 18:14 ` jakub at gcc dot gnu.org
  2021-10-22 18:30 ` friedkeenan at protonmail dot com
                   ` (3 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-10-22 18:14 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
What I see is clang performing checks at a place it shouldn't.
It can be better seen on
struct Test {
    constexpr Test() = default;

    constexpr ~Test() {
        delete this;
    }
};

consteval bool test() {
    auto obj = new Test();
    delete obj;
    return true;
}

static_assert(test());
where it reports
destruction of object that is already being destroyed
which definitely isn't something that happens in the testcase.
The object is only destroyed after the destructor finishes, which is never in
this case.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (4 preceding siblings ...)
  2021-10-22 18:14 ` jakub at gcc dot gnu.org
@ 2021-10-22 18:30 ` friedkeenan at protonmail dot com
  2021-10-22 21:36 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  8 siblings, 0 replies; 10+ messages in thread
From: friedkeenan at protonmail dot com @ 2021-10-22 18:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from friedkeenan at protonmail dot com ---
Well it says "being destroyed" not "already destroyed", because the object is
currently being destroyed

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (5 preceding siblings ...)
  2021-10-22 18:30 ` friedkeenan at protonmail dot com
@ 2021-10-22 21:36 ` redi at gcc dot gnu.org
  2021-10-22 21:39 ` redi at gcc dot gnu.org
  2022-01-17 10:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-22 21:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #5)
> What I see is clang performing checks at a place it shouldn't.
> It can be better seen on
> struct Test {
>     constexpr Test() = default;
> 
>     constexpr ~Test() {
>         delete this;
>     }
> };
> 
> consteval bool test() {
>     auto obj = new Test();
>     delete obj;
>     return true;
> }
> 
> static_assert(test());
> where it reports
> destruction of object that is already being destroyed
> which definitely isn't something that happens in the testcase.
> The object is only destroyed after the destructor finishes, which is never
> in this case.

I think clang is right to reject this, because the constexpr destructor can
never be called with arguments that make it a constant expression, so it's
ill-formed; no diagnostic required ([dcl.constexpr]/6).

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (6 preceding siblings ...)
  2021-10-22 21:36 ` redi at gcc dot gnu.org
@ 2021-10-22 21:39 ` redi at gcc dot gnu.org
  2022-01-17 10:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: redi at gcc dot gnu.org @ 2021-10-22 21:39 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #5)
> The object is only destroyed after the destructor finishes, which is never
> in this case.

But it's still wrong to re-enter the destructor after it starts, even if it
hasn't finished yet.

And [basic.life] p1 says the lifetime ends when the destructor call *starts*.
So the delete expression calls a destructor on an object after its lifetime
ends.

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

* [Bug c++/102899] Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message
  2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
                   ` (7 preceding siblings ...)
  2021-10-22 21:39 ` redi at gcc dot gnu.org
@ 2022-01-17 10:31 ` pinskia at gcc dot gnu.org
  8 siblings, 0 replies; 10+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-01-17 10:31 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2022-01-17
             Status|UNCONFIRMED                 |NEW

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

end of thread, other threads:[~2022-01-17 10:31 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-22 15:46 [Bug c++/102899] New: Performing `delete this` within destructor in constant evaluation results in infinitely recursive error message friedkeenan at protonmail dot com
2021-10-22 17:42 ` [Bug c++/102899] " jakub at gcc dot gnu.org
2021-10-22 17:45 ` friedkeenan at protonmail dot com
2021-10-22 17:46 ` jakub at gcc dot gnu.org
2021-10-22 17:59 ` friedkeenan at protonmail dot com
2021-10-22 18:14 ` jakub at gcc dot gnu.org
2021-10-22 18:30 ` friedkeenan at protonmail dot com
2021-10-22 21:36 ` redi at gcc dot gnu.org
2021-10-22 21:39 ` redi at gcc dot gnu.org
2022-01-17 10:31 ` pinskia 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).