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

            Bug ID: 124117
           Summary: constexpr evaluator fails to deallocate copy of
                    pointer
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

```
#include <memory>
#include <cassert>

struct NodeBase
{
  int data;
  constexpr NodeBase* base_ptr() { return this; }
};

struct Node : NodeBase {};

consteval bool deallocate_copy() {
  std::allocator<Node> alloc;

  Node* node_ptr = alloc.allocate(1);
  new (node_ptr) Node;
  NodeBase* base_ptr = node_ptr->base_ptr();
  Node* node_ptr_copy = static_cast<Node*>(base_ptr);

  assert(node_ptr == node_ptr_copy); // passes
  alloc.deallocate(node_ptr_copy, 1); // fails

  return true;
}

static_assert(deallocate_copy());
```

Despite `node_ptr` and `node_ptr_copy` comparing equal,
deallocating `node_ptr_copy` fails, while
deallocating `node_ptr` succeeds

Clang accepts this code

Godbolt of this example:
https://godbolt.org/z/8T7xhrbEe

This example is rather sensitive
Removing the `data` member variable makes it pass
`NodeBase* base_ptr = node_ptr;` also passes

I believe this issue is relevant to libstdc++,
to constexpr-ifying `std::{set,map}`

Reply via email to