<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 - Incomplete implementation of the P1330R0 proposal / [expr.const]p5 and [class.union]p6 (N4892)"
   href="https://bugs.llvm.org/show_bug.cgi?id=51260">51260</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Incomplete implementation of the P1330R0 proposal / [expr.const]p5 and [class.union]p6 (N4892)
          </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>Linux
          </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++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>matthewjbarichello@gmail.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25087" name="attach_25087" title="Patch to fix the issue in 'releases/12.x'">attachment 25087</a> <a href="attachment.cgi?id=25087&action=edit" title="Patch to fix the issue in 'releases/12.x'">[details]</a></span>
Patch to fix the issue in 'releases/12.x'

All of the references and quotes in this report are concerned with the changes
in P1330R0 and the current working standard document, N4892:
 - P1330R0: <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1330r0.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p1330r0.pdf</a>
 - N4892:   <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/n4892.pdf">http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2021/n4892.pdf</a>

Affected versions:
 - trunk
 - 12.0.1
 - 12.0.0
 - 11.0.1
 - 11.0.0
 - 10.0.1
 - 10.0.0
 - 9.0.1
 - 9.0.0

Driver cmdline:
Note: For versions not supporting -std=c++20, -std=c++2a was used.
 clang++ -std=c++20

Consider the following examples:
 [Example A:
  struct A {};

  constexpr auto test() {
   union U {
    int i;
    A a;
   } u{.i = 0};
   u.a = {};
   return 0;
  }

  int main() {
   constexpr auto r = test();
   (void)r;
  }

 [Example B:
  struct A {
   int i;

   constexpr A() noexcept : i(0) {}
   constexpr A(int i) noexcept : i(i) {}

   constexpr A& operator=(A const& other) noexcept {
    i = other.i;
    return *this;
   }
  };

  constexpr auto test() {
   union U {
    int i;
    A a;
   } u{.i = 0};
   u.a = {123};
   return u.a.i;
  }

  int main() {
   constexpr auto r = test();
   (void)r;
  }

 According to [expr.constant]p5

  An expression E is a core constant expression unless the evaluation of E,
following the rules of the abstract machine (6.9.1), would evaluate one of the
following:
   ... an invocation of an implicitly-defined copy/move constructor or
copy/move assignment operator for a union whose active member (if any) is
mutable, unless the lifetime of the union object began within the evaluation of
E ...

 , and [class.union]p6

  When the left operand of an assignment operator involves a member access
expression (7.6.1.5) that nominates a union member, it may begin the lifetime
of that union member ...

 , the lifetime of `u.a` ("Example A":8) should start at the invocation of
`A`'s implicitly defined copy-assignment operator, however, under all listed
clang versions this is not the case as compilation yields the following error:

  "Example A":13:17: error: constexpr variable 'r' must be initialized by a
constant expression
  constexpr auto r = test();
                 ^   ~~~~~~
  "Example A":8:6: note: member call on member 'a' of union with active member
'i' is not allowed in a constant expression
  u.a = {};
      ^
  "Example A":13:21: note: in call to 'test()'
  constexpr auto r = test();
                     ^
  1 error generated.

 Likewise, the lifetime of `u.a` ("Example B":18) should begin at the
invocation of `A`'s user-defined constexpr copy-assignment operator, however
compilation yields the same error.

Addressing the issue:
 I plan to formally submit a properly tested patch to trunk and backport it to
all affected major clang releases. Until then, I've attached a patch to address
this bug, based on the `release/12.x` tag:

  commit fed41342a82f5a3a9201819a82bf7a48313e296b (grafted, HEAD ->
release/12.x, tag: llvmorg-12.0.1-rc4, tag: llvmorg-12.0.1,
origin/release/12.x)
  Author: Tom Stellard <<a href="mailto:tstellar@redhat.com">tstellar@redhat.com</a>>
  Date:   Mon Jun 28 09:23:38 2021 -0700

      Revert "Revert "[Coverage] Fix branch coverage merging in
FunctionCoverageSummary::get() for instantiation""

      This reverts commit 33d312b2d731507327252fd597bac1b738870330.

      The original patch was correct, so we need to restore it in the
      release branch.

Notes:
 - In testing both examples against other major compilers, such as MSVC and
GCC, it would seem that they compile and behave as expected.</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>