-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Do not codegen zero-sized fields #79076
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// RUN: %target-run-simple-swift(-I %S/Inputs/ -Xfrontend -enable-experimental-cxx-interop -Xcc -std=c++20) | ||
// | ||
// REQUIRES: executable_test | ||
|
||
import StdlibUnittest | ||
import MemberVariables | ||
|
||
var FieldsTestSuite = TestSuite("Generating code with zero sized fields") | ||
|
||
func takeTypeWithZeroSizedMember(_ x: HasZeroSizedField) {} | ||
|
||
FieldsTestSuite.test("Zero sized field") { | ||
var s = HasZeroSizedField() | ||
s.a = 5 | ||
s.set_c(7) | ||
takeTypeWithZeroSizedMember(s) | ||
let s2 = s | ||
let myInt : Empty.type = 6 | ||
expectEqual(s.a, 5) | ||
expectEqual(s.a, s.get_a()) | ||
expectEqual(s2.c, 7) | ||
expectEqual(s2.c, s2.get_c()) | ||
expectEqual(takesZeroSizedInCpp(s2), 5) | ||
expectEqual(s.b.getNum(), 42) | ||
} | ||
|
||
runAllTests() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
Don't have a lot of context about the interop side of things, but just wanted to point out that isZeroSized does not imply empty type (i.e.,
[[no_unique_address]]
on non-empty fields allows Clang to fold other fields into the tail padding of that non-empty NUA field).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.
llvm/llvm-project#50766
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.
Ah looks like Clang still doesn't currently make use of that (but it's a thing that could start happening, llvm/llvm-project#90462)
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, that is a good point. Once Clang starts to reuse the tail padding we definitely should have some tests for that. I do remember us running into some trouble when a base class' tail padding was reused, Egor did some fixes in that space.
Here,
isZeroSized
comes fromFieldDecl::isZeroSize
and it is documented as"Determine if this field is a subobject of zero size, that is, either a zero-length bit-field or a field of empty class type with the [[no_unique_address]] attribute."
. I think based on this, it should be false for non-empty fields.Uh oh!
There was an error while loading. Please reload this page.
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.
One quirk is for example:
where
isZeroSize
onFoo::e
would also betrue
(I think). Just brought it up in case it brings some edge-cases to mind :)Sounds good! 👍
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.
Ah, I see! Thanks a lot for the explanation! Now I see what you mean. I think this code path should be fine but this definitely worth having a test.