-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clang incorrectly rejects default construction of union with nontrivial member, part 2 #81774
Comments
@llvm/issue-subscribers-clang-frontend Author: Stephan T. Lavavej (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.
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;
}
|
Simplified slightly:
This is rejected with the following error message:
As far as I know an union can only have one active field at a time and since using NSDMI on the field should produce a valid default constructor. |
We are hitting this case here: llvm-project/clang/lib/Sema/SemaDeclCXX.cpp Line 9445 in ff409d3
I believe we need to make a similar change in this case along the lines of the fix in: something like this: if (SMOR.getKind() == Sema::SpecialMemberOverloadResult::NoMemberOrDeleted) {
if (CSM == Sema::CXXDefaultConstructor && Field && Field->getParent()->isUnion()) {
// [class.default.ctor]p2:
// A defaulted default constructor for class X is defined as deleted if
// - X is a union that has a variant member with a non-trivial default
// constructor and no variant member of X has a default member
// initializer
const auto *RD = cast<CXXRecordDecl>(Field->getParent());
if (!RD->hasInClassInitializer())
DiagKind = !Decl ? 0 : 1;
}else {
DiagKind = !Decl ? 0 : 1;
}
} This passes |
That seems roughly correct to me, good catch @shafik! |
…ith nontrivial member In 765d8a1 we impelemented a fix for incorrect deletion of default constructors in unions. This fix missed a case and so this PR will extend the fix to cover the additional case. Fixes: llvm#81774
…ith nontrivial member In 765d8a1 we impelemented a fix for incorrect deletion of default constructors in unions. This fix missed a case and so this PR will extend the fix to cover the additional case. Fixes: llvm#81774
Any updates on this? |
Hi! This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:
If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below. |
@llvm/issue-subscribers-good-first-issue Author: Stephan T. Lavavej (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.
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;
}
|
A simpler example:
It is accepted by GCC and MSVC, but not Clang. Online demo: https://gcc.godbolt.org/z/n91W74d3r |
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.
The text was updated successfully, but these errors were encountered: