<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 generates invalid code when lambdas call lambdas."
   href="http://llvm.org/bugs/show_bug.cgi?id=15512">15512</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>Clang generates invalid code when lambdas call lambdas.
          </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>Windows XP
          </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>C++11
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>ian.stewart@autodesk.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=10179" name="attach_10179" title="template workaround to avoid nested lambda bags.">attachment 10179</a> <a href="attachment.cgi?id=10179&action=edit" title="template workaround to avoid nested lambda bags.">[details]</a></span>
template workaround to avoid nested lambda bags.

I have created a simple program that crashes when running the resulting code
generated by Clang.

    int main( int argc, char *argv[])
    {
        auto first = [&]() { return argc; };
        auto second = [&]() { return first(); };
        return second();
    }

I have investigated a little, and what I have found is that Clang mistakenly
thinks that 'first' and 'second' should have the same implementation, only
generates code for one of them, yet calls it for both.

If I change the resulting type of 'first' to differ from 'second', it works:

    int main( int argc, char *argv[])
    {
        auto first = [&]() -> unsigned int { return argc; };
        auto second = [&]() -> int { return first(); };
        return second();
    }

As an ugly workaround, forcing a different return type using a templated
wrapper works (see attachment)

LLVM 3.3 @ SVN 176884
clang++.exe -Xclang -cxx-abi -Xclang microsoft -S -emit-llvm lambdafail.cpp -o
lambdafail.cpp.bc
lli.exe lambdafail.cpp.bc x y

expected result: 3


Reply from Richard Smith <<a href="mailto:richard@metafoo.co.uk">richard@metafoo.co.uk</a>> on the mailing list:

The implementation of the Microsoft ABI is incomplete. It's quite likely that
it does not correctly mangle lambdas yet.</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>