<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - Struct with a mutable member can't be used in constant expression even when the member isn't accessed"
   href="https://bugs.llvm.org/show_bug.cgi?id=44958">44958</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Struct with a mutable member can't be used in constant expression even when the member isn't accessed
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>C++2a
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>ldionne@apple.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The following code doesn't compile because it claims that a read from the
mutable member `i` is performed when calling `.size()`, but this seems wrong:

    $ cat <<EOF | clang++ -xc++ - -std=c++2a -fsyntax-only
    template <typename T, long N>
    struct array {
        T data_[N];
        constexpr long size() const { return N; }
    };
    struct T { mutable int i; };
    constexpr array<T, 1> a = {T{3}};
    static_assert(a.size());
    EOF

Also, GCC compiles this fine. Godbolt link: <a href="https://godbolt.org/z/LPrhxh">https://godbolt.org/z/LPrhxh</a>

The error is:

    <stdin>:8:15: error: static_assert expression is not an integral constant
expression
    static_assert(a.size());
                  ^~~~~~~~
    <stdin>:8:17: note: member call on mutable member 'i' is not allowed in a
constant expression
    static_assert(a.size());
                    ^
    <stdin>:6:24: note: declared here
    struct T { mutable int i; };
                           ^
    1 error generated.


We seem to be hitting this diagnostic specifically;

   
<a href="https://github.com/llvm/llvm-project/blob/47282b1b4/clang/lib/AST/ExprConstant.cpp#L3061">https://github.com/llvm/llvm-project/blob/47282b1b4/clang/lib/AST/ExprConstant.cpp#L3061</a>

However, this seems wrong because we're not doing a lvalue-to-rvalue conversion
anywhere AFAICT. This failure seems to have started with:

    commit 2b4fa5348ee157b6b1a1af44d0137ca8c7a71573
    Author: Richard Smith <<a href="mailto:richard-llvm@metafoo.co.uk">richard-llvm@metafoo.co.uk</a>>
    Date:   Sun Sep 29 05:08:46 2019 +0000

        For P0784R7: compute whether a variable has constant destruction if it
        has a constexpr destructor.

        For constexpr variables, reject if the variable does not have constant
        destruction. In all cases, do not emit runtime calls to the destructor
        for variables with constant destruction.

        llvm-svn: 373159</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>