Skip to content

Clang incorrectly rejects default construction of union with nontrivial member, part 2 #81774

Open
@StephanTLavavej

Description

@StephanTLavavej

Accepted by VS 2022 17.10 Preview 1, rejected by Clang 17.0.3.

On Compiler Explorer, accepted by GCC 13.2, rejected by Clang 17.0.1 and trunk: https://godbolt.org/z/8fGhWa1ss

Presumably related to #48416 which was previously reported and fixed, but this larger repro is still failing.

C:\Temp>type meow.cpp
enum class BasicFormatArgType { NoType, CustomType };

struct Monostate {};

struct Handle {
    Handle(int, int) {}
};

template <typename Context>
struct BasicFormatArg {
    BasicFormatArg() = default;

    BasicFormatArg(int x, int y) : ActiveState(BasicFormatArgType::CustomType), CustomState(x, y) {}

    BasicFormatArgType ActiveState = BasicFormatArgType::NoType;

    union {
        Monostate NoState = Monostate{};
        Handle CustomState;
    };
};

int main() {
    BasicFormatArg<double> arg{};
    (void) arg;
}
C:\Temp>cl /EHsc /nologo /W4 /std:c++latest /c meow.cpp
meow.cpp

C:\Temp>clang-cl -v
clang version 17.0.3
Target: x86_64-pc-windows-msvc
Thread model: posix
InstalledDir: C:\Program Files\Microsoft Visual Studio\2022\Preview\VC\Tools\Llvm\x64\bin

C:\Temp>clang-cl /EHsc /nologo /W4 /std:c++latest /c meow.cpp
meow.cpp(24,28): error: call to implicitly-deleted default constructor of 'BasicFormatArg<double>'
   24 |     BasicFormatArg<double> arg{};
      |                            ^  ~~
meow.cpp(11,5): note: explicitly defaulted function was implicitly deleted here
   11 |     BasicFormatArg() = default;
      |     ^
meow.cpp(19,16): note: default constructor of 'BasicFormatArg<double>' is implicitly deleted because field 'CustomState'
      has no default constructor
   19 |         Handle CustomState;
      |                ^
1 error generated.

Metadata

Metadata

Assignees

Labels

clang:frontendLanguage frontend issues, e.g. anything involving "Sema"rejects-valid

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions