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

            Bug ID: 101777
           Summary: Copying array of non-trivial type during constant
                    evaluation is incorrect
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: davidfromonline at gmail dot com
  Target Milestone: ---

The following well-formed translation unit

```
struct value {
        constexpr value():
                m_value(new int())
        {
        }

        constexpr value(value const &):
                m_value(new int())
        {
        }

        value(value &&) = delete;
        void operator=(value const &) = delete;
        void operator=(value &&) = delete;

        constexpr ~value() {
                delete m_value;
        }

private:
        int * m_value;
};

struct array {
        value m[1];
};

constexpr bool test() {
        auto a = array();
        auto b = a;
        return true;
}

static_assert(test());
```

Fails with the message

```
<source>:34:19: error: non-constant condition for static assertion
   34 | static_assert(test());
      |               ~~~~^~
<source>:34:19:   in 'constexpr' expansion of 'test()'
<source>:32:1:   in 'constexpr' expansion of '(& b)->array::~array()'
<source>:24:8:   in 'constexpr' expansion of '<anonymous>->value::~value()'
<source>:17:24: error: deallocation of already deallocated storage
   17 |                 delete m_value;
      |                        ^~~~~~~
Compiler returned: 1
```

Compiler explorer: https://godbolt.org/z/bcWf3sr4f

I suspect this is another case where some part of the constant evaluation
assumes an operation is trivial even though it is not (assuming copying array
members is trivial would cause this issue).
  • [Bug c++/101777] New: Copyin... davidfromonline at gmail dot com via Gcc-bugs

Reply via email to