-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[cxx-interop] Fix a crash with [[no_unique_address]] #80786
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
base: main
Are you sure you want to change the base?
Conversation
cf5df84
to
75838d6
Compare
@swift-ci please smoke test |
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'm wondering whether skipping this field is really the right approach, and whether there are other alternatives. Can you say more about why these overlapping fields are problematic for Swift at the moment?
@@ -24,6 +27,14 @@ struct HasZeroSizedField { | |||
void set_c(short c) { this->c = c; } | |||
}; | |||
|
|||
struct ReuseFieldPadding { | |||
[[no_unique_address]] std::optional<int> a; |
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.
Can you add some getter/setter methods to ensure this field is still accessible via those?
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.
Also, can you add another test that doesn't really on the layout of std::optional
, i.e., using your own padded type?
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.
Can you add some getter/setter methods to ensure this field is still accessible via those?
Added a getter.
Also, can you add another test that doesn't really on the layout of std::optional, i.e., using your own padded type?
I tried and failed. Not sure what is unique about std::optional
but my couple of attempts to make the compiler do the same optimisation with my own type did not work. Will need to look deeper to figure out what is the difference because I tried to recreate the same layout.
decl->getASTContext().getASTRecordLayout(rd); | ||
if (!decl->isZeroSize(decl->getASTContext()) && | ||
fieldLayout.getDataSize() != fieldLayout.getSize()) | ||
return nullptr; |
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.
What happens to derived conformances that rely on be presence of this field, eg Sendable or Equatable?
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.
Good point. Unfortunately, we would not consider these fields for Sendable
. Maybe we could conservatively always say types with unimported fields should always be not Sendable
? We already have many fields that we do not import for one reason or another.
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 think Equatable
might already be really problematic for C++ types. We do not generate metadata for private fields, so I think even if we can auto conform a type to that the generated implementation is probably faulty :/
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.
It is not a problem for Equatable
. Trying to conform a C++ the type to it gives this error message: "error: extension outside of file declaring struct 'ReuseFieldPadding' prevents automatic synthesis of '==' for protocol 'Equatable'"
. Unfortunately, I don't think we can do anything about the Sendable problem for now.
Absolutely, I would love to support this and that would be the right approach long term. We use |
Swift does not support storing fields in the padding of the previous fields just yet, so let's not import fields like that from C++. Represent them as opaque blobs instead. Fixes #80764
75838d6
to
664efc6
Compare
@swift-ci please smoke test |
Swift does not support storing fields in the padding of the previous fields just yet, so let's not import fields like that from C++. Represent them as opaque blobs instead.
Fixes #80764