<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/120200>120200</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[clang] [coroutine] Possible over optimization of co_return
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
NewSigma
</td>
</tr>
</table>
<pre>
Hi, clang community
I want to discuss an unusual usage of coroutine and CRTP. The following code gives the expected result using MSVC, but a unpredictable result using GCC and clang. Intert a printf into final_suspend helps clang give the expected result.
A similar discussion is on [GCC Bugzilla](https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118074).
https://godbolt.org/z/5rGjKPhW1
clang version: 16.0.0 ~ trunk
Build option: -std=c++20 -O2
```
#include <cstdio>
#include <utility>
#include <coroutine>
template<class T>
struct CRCoro { // Curiously Recurring Coroutine
struct RValueWrapper {
T* p;
operator T&&() const noexcept { return std::move(*p); }
};
using promise_type = T;
T& getDerived() noexcept { return *static_cast<T*>(this); }
auto get_return_object() noexcept { return RValueWrapper(&getDerived()); }
std::suspend_never initial_suspend() noexcept { return {}; }
std::suspend_never final_suspend() noexcept {
//printf("final_suspend\n");
return {};
}
void return_value(T&& x) noexcept { getDerived() = std::move(x); }
void unhandled_exception() {}
};
struct A : public CRCoro<A> {
int a;
};
A func() {
A aa{};
aa.a = 5;
co_return std::move(aa);
}
int main() {
printf("%d\n", func().a);
return 0;
}
```
Expected result(MSVC): 5
Possible result of clang: -387938488
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyEVVuPozYU_jXOy9EgYyCBhzwQstmuqrar2dHuY2SMA94aG_mSuTz0t1cGZkIy022EFISPv_N950qtFa3ifIuyHcr2K-pdp832T_74TbQ9XdW6ed7-JhCpgEmqWmC6770S7hnhEuHyCzxS5cBpaIRl3lqgCrzy1lMJ3tKWgz4B00Z7JxQHqhqo7h--RvDQcThpKfW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