-
-
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?
Conversation
Thanks for your pull request and interest in making D better, @ntrel! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please see CONTRIBUTING.md for more information. If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment. Bugzilla references
Testing this PR locallyIf you don't have a local development environment setup, you can use Digger to test this PR: dub run digger -- build "master + dmd#16858" |
union { | ||
float f2; | ||
int i2; | ||
} |
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.
I guess the current problem (CI errors) would be uncovered by adding a regular zero-initialized field to S7
here, before the anonymous union. That shows that you cannot simply break out of the loop if the 2nd field happens to be overlapped.
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.
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 comment
The 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 comment
The reason will be displayed to describe this comment to others. Learn more.
There doesn't seem to be a way to detect anonymous unions easily at the semantic stage.
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 (T[0]
).
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.
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.
FYI fields
is a flattened array of all found field vars, but members
is the full parse tree. There's an extra cost to traverse recursively over all members though.
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.
Thanks both for the info. Perhaps I will look at fixing anonymous unions in another pull.
and that the previous field isn't empty (T[0]).
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 comment
The reason will be displayed to describe this comment to others. Learn more.
fixing anonymous unions
Now done!
// only consider first sized member of an (anonymous) union | ||
if (vd.overlapped && vd.offset == lastOffset && vd.type.size(vd.loc) != 0) | ||
continue; | ||
lastOffset = vd.offset; |
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):
struct {
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;
}
}
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 setting lastOffset
, 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:
union U3
{
int y;
struct {
float z, w; // z ignored but w has different offset to x
}
}
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.
No description provided.