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

    <tr>
        <th>Summary</th>
        <td>
            [clang] Union of a non-literal type and a literal type should be literal
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            clang
      </td>
    </tr>

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

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

<pre>
    Clang fails to compile this code : 

https://godbolt.org/z/qvxGcPx6x

```cpp
struct A {
 A();
};

template <class T>
struct opt
{
    union Data {
        
        constexpr Data() : x{} {}
        constexpr ~Data() {}

        char x;
        T data;
    };

 constexpr opt() : data{} {}
    constexpr ~opt() { if (engaged) data.data.~T(); }

    Data data;
    bool engaged = false;
};

consteval void foo()
{
    opt<A> a;
}

int main()
{
    foo();
}
```

With the error `non-literal type 'opt<A>' cannot be used in a constant expression` (in C++23, in C++20 the message is slightly different). `opt<A>` is not considered a literal type because of the implementation of CXXRecordDecl::isLiteral : 
https://github.com/llvm/llvm-project/blob/ae5d63924a6214154194c286a13c6ae74d31c086/clang/include/clang/AST/DeclCXX.h#L1457

which does not take into account that the CXXRecordDecl in question might be an union, in which case the type is literal if at least one of its member is literal. Perhaps the meaning of the bitfield `hasNonLiteralTypeFieldsOrBases` should change when the Decl is an union? since it does not seems to serve any other purposes than to detect whether the type is literal or not. 

(this causes problem with the constexpr-isation of `std::format` which i'm currently working on) 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyUVU2TozYQ_TXypWtdIDAMBx889pLLVrKVTCpzbaQGlBUSK4n5yGF_e0pgj_HObqoyNYXtRv30-j2pG71XnSHas9092502OIXeun3n7GSkfyatcZLKbhorX_dHjaaDFpX2ECwIO4xKE4ReeRBWErDsACw5seSwPPsQRs-yA-M143VnZWN12FrXMV7_w3j99enlF_H5pXhZJ7EiWf7FOC4RH9wkAh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