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

            Bug ID: 121880
           Summary: incorrect "deallocation of storage that was not
                    previously allocated" error message
           Product: gcc
           Version: 15.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: geza.herman at gmail dot com
  Target Milestone: ---

I have a program which I think should compile, but it doesn't:

---- 8< -----

#include <memory>

struct Base {
    void (*free_ptr)(Base *);

    consteval Base();
    consteval void free();
};

struct A: Base {
};

consteval void free_impl(Base *self_base) {
    std::allocator<A>().deallocate(static_cast<A *>(self_base), 1);
}

consteval Base::Base() : free_ptr(free_impl) {
}

consteval void Base::free() {
    free_ptr(this);
}

consteval void foo() {
    A *a = std::allocator<A>().allocate(1);
    a = new(a) A;
    a->free();
    // a->free_ptr(a);
}

int main() {
    foo();
}

---- 8< -----

The program allocates and deallocates in a consteval function.  GCC complains:

t.cpp: In function ‘int main()’:
t.cpp:32:8: error: call to consteval function ‘foo()’ is not a constant
expression
   32 |     foo();
      |     ~~~^~
In file included from /usr/include/c++/15/memory:67,
                 from t.cpp:1:
t.cpp:32:8:   in ‘constexpr’ expansion of ‘foo()’
t.cpp:27:12:   in ‘constexpr’ expansion of ‘a->A::Base.Base::free()’
t.cpp:21:13:   in ‘constexpr’ expansion of
‘((Base*)this)->Base::free_ptr(((Base*)this))’
t.cpp:14:35:   in ‘constexpr’ expansion of
‘std::allocator<A>().std::allocator<A>::deallocate(((A*)self_base), 1)’
/usr/include/c++/15/bits/allocator.h:212:30: error: deallocation of storage
that was not previously allocated
  212 |             ::operator delete(__p);

Interestingly, if instead of "a->free();", I use "a->free_ptr(a);", GCC
compiles the program.

This happens on GCC 16.0 as well.

Reply via email to