<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 - Clang segfaults when compiling code with std::variant"
   href="https://bugs.llvm.org/show_bug.cgi?id=51127">51127</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang segfaults when compiling code with std::variant
          </td>
        </tr>

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

        <tr>
          <th>Version</th>
          <td>12.0
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>Frontend
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>cerg2010cerg2010@mail.ru
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=25032" name="attach_25032" title="stacktrace output">attachment 25032</a> <a href="attachment.cgi?id=25032&action=edit" title="stacktrace output">[details]</a></span>
stacktrace output

Consider this code snippet:

#include <variant>

struct Foo
{
    operator std::variant<int, float>()
    {
        return 0;
    }
};

constexpr void foo()
{
  std::variant<int, float, Foo> var1 = 0;
  std::variant<int, float> var2;
  var2 = std::visit([](auto arg) -> decltype(var2) { return arg; }, var1);
}

int main(int argc, char** argv)
{
    return 0;
}

Latest Clang release version (12.0.1 atm), as well as trunk and older versions
up to 3.9.0 which has -std=c++1z and variant header (tested on godbolt.org)
segfault when compiling this. GCC handles this code fine. Also it crashes with
both -stdlib=libc++ and -stdlib=libstdc++.

One particular thing which is interesting here is that if you replace return 0
in conversion function with return {}, the trunk version will compile this, but
12.0.1 will not. I've also had other variants of this code when adding a
newline at the end of file and removing it will determine the result. The
example provided here is the most stable to reproduce.

I've tried this mostly on godbolt.org, but it also successfully reproduces on
my local machine with Arch Linux (with clang version 12.0.1)</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>