[llvm-bugs] [Bug 51925] New: std::bit_cast falls over, seemingly due to some invisible alignment requirements

via llvm-bugs llvm-bugs at lists.llvm.org
Tue Sep 21 01:10:41 PDT 2021


https://bugs.llvm.org/show_bug.cgi?id=51925

            Bug ID: 51925
           Summary: std::bit_cast falls over, seemingly due to some
                    invisible alignment requirements
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Linux
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++2a
          Assignee: unassignedclangbugs at nondot.org
          Reporter: krax.powah at gmail.com
                CC: blitzrakete at gmail.com, erik.pilkington at gmail.com,
                    llvm-bugs at lists.llvm.org, richard-llvm at metafoo.co.uk

Clang Version: The current truck (14.0.0
(https://github.com/llvm/llvm-project.git
60ab6861ed13e4f1e2729f8add6366a7be223d80)), but also present on 12.0.1 which
support std::bit_cast.


Repro case (https://godbolt.org/z/Y6e8T98M1):

```
    #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:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102401

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20210921/15973e31/attachment.html>


More information about the llvm-bugs mailing list