[llvm-bugs] [Bug 44958] New: Struct with a mutable member can't be used in constant expression even when the member isn't accessed

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Feb 18 13:15:16 PST 2020


https://bugs.llvm.org/show_bug.cgi?id=44958

            Bug ID: 44958
           Summary: Struct with a mutable member can't be used in constant
                    expression even when the member isn't accessed
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: ldionne at apple.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

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: https://godbolt.org/z/LPrhxh

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;

   
https://github.com/llvm/llvm-project/blob/47282b1b4/clang/lib/AST/ExprConstant.cpp#L3061

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 <richard-llvm at metafoo.co.uk>
    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

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200218/fbb30c70/attachment.html>


More information about the llvm-bugs mailing list