This is an archive of the discontinued LLVM Phabricator instance.

[Clang][Sema] Fix incorrect deletion of default constructors for some unions
ClosedPublic

Authored by royjacobson on Mar 11 2023, 8:46 AM.

Details

Summary

If a union has explicit initializers for some members, we shouldn't delete
its default constructor.
Fixes https://github.com/llvm/llvm-project/issues/48416.

Diff Detail

Event Timeline

royjacobson created this revision.Mar 11 2023, 8:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2023, 8:46 AM
royjacobson requested review of this revision.Mar 11 2023, 8:46 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 11 2023, 8:46 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
royjacobson added a subscriber: Restricted Project.

Small cleanup

Generally looks good to me. Do we do anything special if there are multiple initializers? Also, can we have a codegen test that validates that we actually construct it correctly (and perhaps a constexpr test for the same!)?

clang/lib/Sema/SemaDeclCXX.cpp
9164

This way you don't need the assert!

9168

else needs curleys, since its associated 'if' has them.

Add more tests, small nit.

royjacobson marked 2 inline comments as done.Mar 13 2023, 10:02 AM

Generally looks good to me. Do we do anything special if there are multiple initializers? Also, can we have a codegen test that validates that we actually construct it correctly (and perhaps a constexpr test for the same!)?

Added constexpr + codegen tests.

If we have multiple initializers it's an error that's already diagnosed, I assume the error recovery just drops any initializer after the first.

Generally looks good to me. Do we do anything special if there are multiple initializers? Also, can we have a codegen test that validates that we actually construct it correctly (and perhaps a constexpr test for the same!)?

Added constexpr + codegen tests.

If we have multiple initializers it's an error that's already diagnosed, I assume the error recovery just drops any initializer after the first.

Got it, thanks! LGTM!

shafik added a subscriber: shafik.Mar 13 2023, 8:55 PM

LGTM, I wlll let @royjacobson give the final approval after reviewing codegen tests.

clang/lib/Sema/SemaDeclCXX.cpp
9162–9163

Precommit CI found related failures:

/var/lib/buildkite-agent/builds/llvm-project/clang/test/CodeGen/union-non-trivial-member.cpp:29:16: error: found empty check string with prefix 'CHECK:'
// CHECK-NEXT: 
               ^
error: unknown argument: '-g0'
erichkeane added inline comments.Mar 14 2023, 7:01 AM
clang/test/CodeGen/union-non-trivial-member.cpp
31

Looking more closely, this test is a little over specific. First, you can just do CHECK: to start each function, that way you don't have the blank lines.

In the top function, all we care about is the call void <name>s, so the rest of the lines can go away. Since you're checking function's define line, you can count on ordering that way rather than check-next.

This middle function should use more wildcard/placeholders, checking fully specific names like this or this.addr is a mistake, as those aren't guaranteed to be there. Also, all the 'align' checks are likely to make this fail in post-commit.

on the last function, we don't even care about the parameters list, so you just need the call void <name> part.

Fix the codegen test, add a standard ref to the comment.

Oops, sorry for the bad codegen test and thanks for the comments.

royjacobson marked 2 inline comments as done.Mar 14 2023, 12:28 PM
royjacobson added inline comments.
clang/test/CodeGen/union-non-trivial-member.cpp
31

removed the non-essential parts.

erichkeane added inline comments.Mar 14 2023, 12:30 PM
clang/test/CodeGen/union-non-trivial-member.cpp
24
25

Don't need the entry: line,

26

Wildcards are implicit at the end of a line.

27
28

This and the line after it aren't really necessary/valuable, particularly in case we get debug info/builtin/etc added later.

31

I typically suggest not requiring a space either before or after a {{.*}} block, since that wont match 'empty'.

32

Same here, skip the entry , ret, and closing curley brace line.

33

same advise as above on the following ones too.

erichkeane accepted this revision.Mar 14 2023, 12:32 PM

Fix the codegen test, add a standard ref to the comment.

No worries, there is definitely somewhat of an art to writing a codegen test that won't be fragile/overspecified, I've made a few more suggestions to make it more clear/easier, else this LGTM. Feel free to put the rest of the codegen test suggestions up for further review, but if you're comfortable with them, you may commit instead.

This revision is now accepted and ready to land.Mar 14 2023, 12:32 PM
royjacobson marked an inline comment as done.

slimmer codegen test

erichkeane accepted this revision.Mar 15 2023, 2:19 PM

Still LGTM!