-
-
Notifications
You must be signed in to change notification settings - Fork 606
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
Fix: isZeroInit does not take into account unions #16858
base: master
Are you sure you want to change the base?
Changes from all commits
cbb7cb6
261044e
18790f6
3d2ac20
179da5d
e606fad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,45 @@ static if (is(Vector!(int[4]))) | |
static assert(__traits(isZeroInit, Holder!(Vector!(int[4]), 0))); | ||
static assert(!__traits(isZeroInit, Holder!(Vector!(int[4]), 1))); | ||
} | ||
|
||
// https://issues.dlang.org/show_bug.cgi?id=24776 | ||
struct S6 { | ||
union { | ||
int i1; | ||
float f1; | ||
} | ||
} | ||
static assert(__traits(isZeroInit, S6)); | ||
|
||
struct S7 | ||
{ | ||
union { | ||
float f2; | ||
int i2; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess the current problem (CI errors) would be uncovered by adding a regular zero-initialized field to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. There doesn't seem to be a way to detect anonymous unions easily at the semantic stage. So this no longer fixes anonymous unions but does fix named unions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. P.S. It was weird that the CI didn't seem to be reporting clear errors about what had broken. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Perhaps checking whether the byte offset is the same as the previous field's - and whether the field is overlapped, due to bitfields. Edit: Oh, and that the previous field isn't empty ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
FYI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks both for the info. Perhaps I will look at fixing anonymous unions in another pull.
I didn't realize that, taking account of it (seems to have) fixed this pull. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Now done! |
||
} | ||
static assert(!__traits(isZeroInit, S7)); | ||
|
||
// https://issues.dlang.org/show_bug.cgi?id=23841 | ||
union U | ||
{ | ||
float x = 0; | ||
float y; | ||
} | ||
static assert(__traits(isZeroInit, U)); | ||
|
||
union U2 | ||
{ | ||
float x; | ||
int y; | ||
} | ||
static assert(!__traits(isZeroInit, U2)); | ||
|
||
struct S8 { | ||
int[0] dummy; // same offset as anon union, but doesn't overlap; should be ignored anyway for zero-init check | ||
union { | ||
float f; // is the first member of the anon union and must be checked | ||
int i; | ||
} | ||
} | ||
static assert(!__traits(isZeroInit, S8)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! - I meant the previous field's size though; I guess this fails (wrongly treated as zero-initialized):
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[So a simple early
if (vd.type.size(vd.loc) == 0) continue;
at the iteration start should do the job, shortcutting the check for 0-length static arrays and not settinglastOffset
, so that the first union member is always checked.]There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops, thanks. Added that test. It is a lot better, but there are (less common) cases where it wrongly doesn't zero init:
If this is merged, I'll file that case separately.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oof yeah, no idea how we'd easily fix those cases.