<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 - Miscompilation: clang generates a weak symbol for a generic lambda that depends on a lambda in a static function"
   href="https://bugs.llvm.org/show_bug.cgi?id=44368">44368</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Miscompilation: clang generates a weak symbol for a generic lambda that depends on a lambda in a static function
          </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>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>-New Bugs
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>blastrock@free.fr
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>htmldeveloper@gmail.com, 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=22961" name="attach_22961" title="complete project that shows the bug">attachment 22961</a> <a href="attachment.cgi?id=22961&action=edit" title="complete project that shows the bug">[details]</a></span>
complete project that shows the bug

Here is a code snippet that shows the bug:

    template <typename T> auto async() {
      return [](auto func) {
        [func] { func(); }();
      };
    }
    static void f() {
      async<int>()([] { });
    }
    void f1() { f(); }

Compile with:

    clang++ -c ll.cpp

Run objdump -t ll.o -C, and you'll see the following symbol:

    0000000000000000  w    F
.text._ZZZ5asyncIiEDavENKUlT_E_clIZL1fvE3$_0EEDaS0_ENKUlvE_clEv   
000000000000001b
async<int>()::{lambda(auto:1)#1}::operator()<f()::$_0>(f()::$_0)
const::{lambda()#1}::operator()() const

This is the call operator of the inner lambda on line 3 and this symbol is
*weak*, even though it depends on the func argument, which is a lambda from a
static function. I think this symbol should be *local* as a *weak* symbol might
be overridden by another translation unit.

The consequence is that if there are two static functions that have the same
name in two different translation units and that they both define a lambda,
calling async with that lambda will generate only one symbol in the final
binary and that usually leads to a crash in a more complex example.

I am attaching a more complete project which shows the miscompilation issue.
The program should print 1 and 2, but it prints 1 and 1 for the reason
described above. Renaming the static function in one of the translation units
fixes the issue.

GCC 9 compiles the program correctly, produces *local* symbols for that call
operator and the program prints 1 and 2.</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>