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

            Bug ID: 125714
           Summary: [C++20] ignored pure attribute on constexpr destructor
                    breaks constant-evaluation deallocation tracking
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: qurong at ios dot ac.cn
  Target Milestone: ---

For this program:

struct T {
  T *l = nullptr;
  T *r = nullptr;

  __attribute__((pure)) constexpr ~T() {
    if (l) {
      l->r = nullptr;
      delete l;
    }
    if (r) {
      r->l = nullptr;
      delete r;
    }
  }

  constexpr T *right() {
    if (!r) {
      r = new T;
      r->l = this;
    }
    return r;
  }
};

constexpr int f() {
  T *t = new T;
  t = t->right();
  delete t;
  return 1;
}

static_assert(f() == 1);

This causes an error in gcc, but clang warns and ignores it. 
Compiler Explorer link: https://godbolt.org/z/Ed8fTqYrG

GCC 15.2.0 output:
warning: 'pure' attribute on function returning 'void'
error: non-constant condition for static assertion
error: ... allocated storage has not been deallocated


Clang 22.1.0 output:
warning: 'pure' attribute on function returning 'void'; attribute ignored

Reply via email to