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

    <tr>
        <th>Summary</th>
        <td>
            regression: forced packing of non-POD leads to compatibility problems
        </td>
    </tr>

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

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

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

<pre>
    ```
#include <cstdio>
#define PACKED __attribute__((packed))

class Foo {
    int i;
public:
    int& get_ref() { return i; }
};

class Bar {
public:
    unsigned char c;
    Foo p;
    unsigned char c1;
    int c2;
    int get_ref() { return p.get_ref(); }
} PACKED;

int main()
{
  Bar b;
  printf("sizeof(b)=%zi\n", sizeof(b));
  return b.get_ref();
}
```

This code builds well on clang-15, and
prints the result "10", which is correct.
But on clang-16 we get this:
```
clang++-16 -Wall -Wpacked-non-pod -Wpacked -o p_clng16 pod.cpp
pod.cpp:13:9: warning: not packing field 'p' as it is non-POD for the purposes of layout [-Wpacked-non-pod]
    Foo p;
        ^
1 warning generated.
```
and the result is 16, which is not what we need.
So the work-around is to pack class `Foo`, lets
do that. Now we get:
```
$ clang++-16 -Wall -Wpacked-non-pod -Wpacked -o p_clng16 pod.cpp
$ ./p_clng16
sizeof(b)=10
```

So far, the work-around works as expected,
good. But suddenly we discover this:
```
g++ -Wall -Wpacked -o p_gcc pod.cpp
pod.cpp: In member function ‘int& Foo::get_ref()’:
pod.cpp:7:29: error: cannot bind packed field ‘((Foo*)this)->Foo::i’ to ‘int&’
    7 |     int& get_ref() { return i; }
```

So with such work-around, the code no
longer builds with gcc. See here for details:
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36566

So by forcing people to pack whatever is
already packed, we get the deserved
punishment from `-Wpacked` (#62353 )
and also get the code that can't be compiled
with gcc, and works properly with clang only
because clang allows to bind packed fields
to a reference. I wouldn't be surprised if one
day clang would disallow also that, and the
work-around of adding explicit packing
will stop working even for clang.

The problem is introduced by https://github.com/llvm/llvm-project/commit/277123376ce08c98b07c154bf83e4092a5d4d3c6
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVlGP4jgS_jXmpQQKNknggQcaGml10t1Kc9I-thy7SHxr7Mh2mmV-_alMSDfM9Eh32lGLMbar6vuqviojYzStQ9yy8oWVh5kcUufDNqbYzxqvr1tWFeNfcWDFjnFhnLKDRmBir2LSxjPxOh1qPBmH8Ptu_4_XA7y9yZSCaYaEb2-Mrxlf91L9iZrxDf3drPKnsjJGOHoPrH65bQEAGJfAMDHu9ENjjWJi93CB8QpaTG8BTznGhlxAwDQEl42B1YcxVn2YnH2O-yLDR9yfRBlcTpMG1ckAavJBZ4S5f9h5ur18OCRGiv-w9SWBfvFw9ExnTPUTK_J4lsaNNuPlKSTRbT5B6INx6RaAR_MdPa2bHO3AePndsHLvGOeM7-HxPAO6uxkRNz8g_kj-bfGkqfz5785EUF4jNIOxOsIFrQXvQFnp2vmypODS6bFGhDhC6hACxsEmYJwvixHjpTOqg-wvBFRpcTN6GdInhxVckNIOqTNxqvYTtnyX8RfGX8hi_oe0FuZ_3FQ8d97Ne6-nDZh76N-Ude2ygt7rher7Ee_4ReyWgondhokdXGRwxrW0dD4BuTCuhZNBq4Hxume8BhnBJKJCsX7_1wFOPmTa_RB6HzGCP4GVVz8kYOXLMzRWHn6lVPrHyrF9l3dE0KLDIBPqxU-TIp3-nHkTYVk9pJ3oXDqZKMMOJzfffDa7-PDnXAY_OE2Xk8_U4daJrCqO3lMovgeLKd5MNZnKtIB_-stYt69KxvgK_q6yka8F48f76W33uUWWxS90_c3DSQZi88yd1pEKjH_1qBJNxf3NpvVeL4DkGget0dkrcdYmKv-O4ZeCHVk_Ub4xbJX6SpXwm4MznhsMcBqcSsY7YK-crQu2WY8zluoidkzsHtt7vLWZEH14rZnY8ax1DMEHWijpSB2NcRpGbKPg79Fu7wQF4zvGN5ks38yZe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