public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110714] New: constexpr lifetime error: base class this pointer
@ 2023-07-18 11:39 pkeir at outlook dot com
  2023-07-18 12:46 ` [Bug c++/110714] " pinskia at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: pkeir at outlook dot com @ 2023-07-18 11:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 110714
           Summary: constexpr lifetime error: base class this pointer
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pkeir at outlook dot com
  Target Milestone: ---

Compiling the C++20 MRE code below fails:

struct Base
{
  constexpr virtual ~Base() {}
  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;
  delete pf->get_this();
  return true;
}

constexpr bool b = test();

...with the following error message:

2$ /opt/gcc-latest/bin/g++ -std=c++20 -c ce_base_alloc2.cpp 
ce_base_alloc2.cpp:17:24:   in ‘constexpr’ expansion of ‘test()’
ce_base_alloc2.cpp:13:23:   in ‘constexpr’ expansion of
‘pf->Derived::<anonymous>.Base::get_this()->Base::~Base()’
ce_base_alloc2.cpp:8:8: error: deallocation of storage that was not previously
allocated
    8 | struct Derived : public Base {};
      |        ^~~~~~~

I have tried with GCC trunk (14.0.0) and also version 12.2.0.

I suspect that the this pointer in the base class is not tracking the constexpr
dynamic allocation. Clang and MSVC both compile successfully. Clang requires
the virtual destructor.

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
@ 2023-07-18 12:46 ` pinskia at gcc dot gnu.org
  2023-07-18 12:58 ` pkeir at outlook dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-07-18 12:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-07-18

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this works on the trunk:
```
struct Base
{
  constexpr virtual ~Base() {}
//  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;
  Base* t = pf;
  delete t;
  return true;
}

constexpr bool b = test();
```

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
  2023-07-18 12:46 ` [Bug c++/110714] " pinskia at gcc dot gnu.org
@ 2023-07-18 12:58 ` pkeir at outlook dot com
  2023-07-18 13:16 ` pkeir at outlook dot com
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pkeir at outlook dot com @ 2023-07-18 12:58 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Paul Keir <pkeir at outlook dot com> ---
I know. `delete pf` also works. The issue seems to be with the use of the this
pointer within the member function. This is just the MRE - I've come across
this issue twice now in our code base.

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
  2023-07-18 12:46 ` [Bug c++/110714] " pinskia at gcc dot gnu.org
  2023-07-18 12:58 ` pkeir at outlook dot com
@ 2023-07-18 13:16 ` pkeir at outlook dot com
  2024-03-22  0:53 ` redi at gcc dot gnu.org
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pkeir at outlook dot com @ 2023-07-18 13:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Paul Keir <pkeir at outlook dot com> ---
Actually, there's no need here to delete through the base pointer; so this is
perhaps simpler:

struct Base
{
  constexpr Base* get_this() { return this; }
  int x;
};

struct Derived : public Base {};

constexpr bool test()
{
  Derived* pf = new Derived;

  delete static_cast<Derived*>(pf->get_this());

  return true;
}

constexpr bool b = test();

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
                   ` (2 preceding siblings ...)
  2023-07-18 13:16 ` pkeir at outlook dot com
@ 2024-03-22  0:53 ` redi at gcc dot gnu.org
  2024-03-22  0:54 ` redi at gcc dot gnu.org
  2024-03-25 21:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-22  0:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|2023-07-18 00:00:00         |2024-3-22

--- Comment #4 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I encountered this using std::pointer_traits<P>::pointer_to in constexpr code
like so:

#include <memory>

struct B { constexpr virtual ~B() = default; };
struct D : B { };

consteval void f()
{
  delete std::pointer_traits<B*>::pointer_to(*new D());
}

int main()
{
  f();
}


Which can be simplified to:

struct B { constexpr virtual ~B() = default; };
struct D : B { };

consteval void f()
{
  delete &(B&)*new D();
}

int main()
{
  f();
}

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
                   ` (3 preceding siblings ...)
  2024-03-22  0:53 ` redi at gcc dot gnu.org
@ 2024-03-22  0:54 ` redi at gcc dot gnu.org
  2024-03-25 21:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: redi at gcc dot gnu.org @ 2024-03-22  0:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
This works with Clang and EDG.

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

* [Bug c++/110714] constexpr lifetime error: base class this pointer
  2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
                   ` (4 preceding siblings ...)
  2024-03-22  0:54 ` redi at gcc dot gnu.org
@ 2024-03-25 21:13 ` ppalka at gcc dot gnu.org
  5 siblings, 0 replies; 7+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-03-25 21:13 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
For the original testcase, evaluation of pf->get_this(); yields &heap [0]
instead of  the expected &heap, which we don't recognize as a valid constexpr
heap pointer thus leading to the error.

This heap[0] term gets built from cxx_fold_indirect_ref which folds '*(struct
Derived *) &heap' into 'heap[0]' (where 'heap' has type Derived[1]), which
doesn't seem like a sound transformation?  I'd expect it to get folded to
simply 'heap'.

Then again this intermediate (Derived *) cast seems unsound in the first place
since we're casting something of type Derived[1]* to Derived*.  I don't know
what to make of that.

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

end of thread, other threads:[~2024-03-25 21:13 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-18 11:39 [Bug c++/110714] New: constexpr lifetime error: base class this pointer pkeir at outlook dot com
2023-07-18 12:46 ` [Bug c++/110714] " pinskia at gcc dot gnu.org
2023-07-18 12:58 ` pkeir at outlook dot com
2023-07-18 13:16 ` pkeir at outlook dot com
2024-03-22  0:53 ` redi at gcc dot gnu.org
2024-03-22  0:54 ` redi at gcc dot gnu.org
2024-03-25 21:13 ` ppalka 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).