<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 - std::bit_cast falls over, seemingly due to some invisible alignment requirements"
href="https://bugs.llvm.org/show_bug.cgi?id=51925">51925</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>std::bit_cast falls over, seemingly due to some invisible alignment requirements
</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>C++2a
</td>
</tr>
<tr>
<th>Assignee</th>
<td>unassignedclangbugs@nondot.org
</td>
</tr>
<tr>
<th>Reporter</th>
<td>krax.powah@gmail.com
</td>
</tr>
<tr>
<th>CC</th>
<td>blitzrakete@gmail.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, richard-llvm@metafoo.co.uk
</td>
</tr></table>
<p>
<div>
<pre>Clang Version: The current truck (14.0.0
(<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a>
60ab6861ed13e4f1e2729f8add6366a7be223d80)), but also present on 12.0.1 which
support std::bit_cast.
Repro case (<a href="https://godbolt.org/z/Y6e8T98M1">https://godbolt.org/z/Y6e8T98M1</a>):
```
#include <bit>
struct Item
{
float a = 0;
float b = 0;
char c = 0; // Removing this or changing it to type with a size of 4
}; // fixes it.
struct M
{
struct Data { char d[sizeof(Item)]; };
static constexpr Data build(Item item)
{
Data d {};
d = std::bit_cast<Data>(item);
return d;
}
};
int main(void)
{
constexpr auto data = M::build({}); // Only occurs if this is a
constexpr assignment
}
```
Result:
```
<source>:23:20: error: constexpr variable 'data' must be initialized by a
constant expression
constexpr auto data = M::build({}); // Only occurs if this is a
constexpr assignment
^ ~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/bit:77:14:
note: indeterminate value can only initialize an object of type 'unsigned char'
or 'std::byte'; 'char' is invalid
return __builtin_bit_cast(_To, __from);
^
<source>:16:13: note: in call to 'bit_cast(item)'
d = std::bit_cast<Data>(item);
^
<source>:23:27: note: in call to 'build({0.000000e+00, 0.000000e+00, 0})'
constexpr auto data = M::build({}); // Only occurs if this is a
constexpr assignment
^
1 error generated.
ASM generation compiler returned: 1
<source>:23:20: error: constexpr variable 'data' must be initialized by a
constant expression
constexpr auto data = M::build({}); // Only occurs if this is a
constexpr assignment
^ ~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/bit:77:14:
note: indeterminate value can only initialize an object of type 'unsigned char'
or 'std::byte'; 'char' is invalid
return __builtin_bit_cast(_To, __from);
^
<source>:16:13: note: in call to 'bit_cast(item)'
d = std::bit_cast<Data>(item);
^
<source>:23:27: note: in call to 'build({0.000000e+00, 0.000000e+00, 0})'
constexpr auto data = M::build({}); // Only occurs if this is a
constexpr assignment
```
I've made a similar bug report to GCC aswell, which can be found here:
<a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102401">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102401</a></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>