<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/81963>81963</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Warning for coroutine lambdas that have a closure
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          KayEss
      </td>
    </tr>
</table>

<pre>
    I'd like to propose a new warning that is issued for dangerous/invalid uses of closures together with coroutine lambdas. Because of the way that that coroutines work in C++ the lifetime of the closure and the lifetime of the coroutine are not coupled -- the closure will be destructed the first time that the coroutine is suspended making any access into the closure after that point UB. Many (most?) coroutine libraries will always suspend at the initial suspension point meaning that use of the closure will always be UB.

Anything that would normally be put in the closure needs to be passed in as an argument to the lambda to be safe for use after suspension.

If a coroutine lambda is used in a class's member function which accesses other members, the compiler will suggest to add `this` to the closure, when the correct solution is generally to pass it as a parameter.

I've been using coroutines with clang ever since they first landed in trunk and have made this mistake many times. ASAN does not generally catch this problem and debugging it can be difficult due to the UB. It's a very easy mistake to make and I expect many more will come across this foot-gun as more people start to use coroutines.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJxsVE2P8zYP_DXKhdjAUTZfBx92n-ddYPGivRRFz7RE2-rKkiFKcf3vC8nefODpJQFscjieGRKZTeeIanF4F4efG0yx96H-P87_Y940Xs_1p5AnDdZ8EUQPY_CjZwIERxNMGJxxHcQeIxgGw5xIQ-sDaHQdBZ9YyA_jrmiNhsTE4FtQ1nMKxBB9R7GnAJOJPSgffIrGEVgcGo28hXdSmJhyU-wJJpyXWeXnVs8w-fAFxsEPId-FfC_F1rQUzXBrXqcCOv3f72_jMRA4nwek0ZKGl5cngMlYCw2BJo4hqUgLXmsCRyiIK8dHTMPAiUdymjQM-JVlQzcDKkXMYFz0zyzbSGEBGr1xEf5838JvuUPI8-A5iv2HkJdH0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