<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60422>60422</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Bug when returning object with union from function
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
poljak181
</td>
</tr>
</table>
<pre>
Hi, consider the following example:
```cpp
#include <iostream>
struct simGEOM_BOX {
float x;
float y;
float z;
};
struct simGEOM_CYLINDER {
float halfHeight = 0.f;
float radius = 0.f;
};
enum class simDISTURBER_GEOM_TYPE {
BOX = 1,
CYLINDER = 2,
UNDEFINED = 100500,
};
union GEOM {
simGEOM_BOX box;
simGEOM_CYLINDER cylinder;
GEOM()
: box(simGEOM_BOX{})
{}
explicit GEOM(const simGEOM_BOX& box)
: box(box)
{}
explicit GEOM(const simGEOM_CYLINDER& cylinder)
: cylinder(cylinder)
{}
static GEOM MakeCylinder(float halfHeight, float radius)
{
return GEOM(simGEOM_CYLINDER{ halfHeight, radius });
}
};
struct GEOM_HOLDER {
GEOM_HOLDER() = default;
static GEOM_HOLDER MakeCylinder();
GEOM geom = {};
simDISTURBER_GEOM_TYPE type = simDISTURBER_GEOM_TYPE::UNDEFINED;
private:
GEOM_HOLDER(const GEOM& geom, simDISTURBER_GEOM_TYPE type)
: geom(geom)
, type(type)
{}
};
//#pragma clang optimize off
GEOM_HOLDER GEOM_HOLDER::MakeCylinder()
{
auto res = GEOM_HOLDER{ GEOM::MakeCylinder(1.3, 2.2), simDISTURBER_GEOM_TYPE::CYLINDER };
std::cerr << "type before: " << static_cast<int>(res.type) << "\n";
return res;
}
//#pragma clang optimize on
GEOM_HOLDER geomHolder;
int main()
{
geomHolder = GEOM_HOLDER::MakeCylinder();
std::cerr << "type after: " << static_cast<int>(geomHolder.type) << "\n";
return 0;
}
```
Program compiled with x86-64 clang 13.0.1 (or with any other compiler) with comman line
`-std=c++17 -O3 -Wall -Wextra -fsanitize=undefined,address` gives result:
```
type before: 2
type after: 2
```
Program compiled with x86-64 clang 14 or higher with the same command line gives result:
```
type before: 2
type after: 0
```
If I disable optimization for `GEOM_HOLDER GEOM_HOLDER::MakeCylinder()` result looks like expected.
[you can check it here](https://godbolt.org/z/ExhEj4vqj)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl2PmzoT_jXOzSgR2EDIRS42IXl3pbZb9W3V06vKAQPeNTbHNvv1648MJJCQrXqqI0WbjYd5nplnPjA1hheSsTUKNyhMZrSxpdLrWokH-ujH_uygstf1LUd4C6mShmdMgy0Z5EoI9cxlAeyFVrVgiNwgL0HeDYq87pPWdX-CCZepaDIGiGy5MlYzWiGy683tX2N1k1owvPrf7v7jz839X4CWm84GuVDUwgsixwPoj16nR2-nI7RMhv-vkWx_fLj7lOy-jJiOKCUV-S3jRWkBkQS8RT5l0jTjjZnYp7RMNhWkghrjqJO7_3_99mWz-_KzDeLrj8-7swDa1EkCPsLb0-EQKkkAD5Zvn5Ld_u7TLulcPC_0vJN1GkkjuZLgeM8ox6of1EjniVLpq-AyY_oCF6AFRThGeHU6AgBEblpEHI84HPUyGZ7sf5_jsZda8JTbI7Brv7P-QDjqoN8hPLP9AcUxZ8dzSntKNpjia09dJTaWWp52dfhIH9l2wLhsPzd543a7xB5Fo5lttDwmM0ljubmAPfZvV4pRew8BvzdBLfLt_Yfz4Rmddp3QNmXGctoIO-2YkQpHrAsxxnGNOKBgqmqxe3XJWStfGzD7WrPW47rdbS9ycxqmC9Ja8ydqhxXXd_uQatc4nfBRG53T9xehXOmjzivuvs6teNs7xZe-Z901KRbC-_ZDak2LiroVJAtQteUVf2Og8rx7blyBcWKtKFdK0tMNotPGKtCs24VjhOWmU-Uakr8gLjO8wA7zXb0639GqHpXb2Kwzp0xr93JBZAsI47baB5Yr7YrmTo7Grud-ptRY9y6S1r2GcKyZWfTajmBQuJXu68TXT5hm5mzb_5bYclyZseKu4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