Commit Graph

6 Commits

Author SHA1 Message Date
Egor Zhdan
ee5f7ad262 [cxx-interop] Do not crash when emitting layout of std::string
If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
(cherry picked from commit a7c174431f)
2025-05-30 17:33:31 +01:00
John Hui
f6f5adb5f4 [cxx-interop] Avoid querying layout of dependent types during symbolic import
In #80786, we started importing certain padded fields as opaque blobs.
Part of this logic involved querying those fields' ASTRecordLayout.
However, dependent types (which are imported symbolically) do not have
an ASTRecordLayout, so calling Clang's getASTRecordLayout() would lead
to an assertion error for class templates where a no_unique_address
field is some kind of dependent C++ record type.

This patch avoids the field padding check during symbolic import mode
because that check is only relevant for codegen anyway.

rdar://150067288
(cherry picked from commit 1fdb239cea)
2025-05-01 10:10:56 -07:00
John Hui
a2c5cff6a1 [NFC] Add test where the padding of a [[no_unique_address]] field with a custom type is reused
(cherry picked from commit 5fdbb44041)
2025-05-01 10:10:56 -07:00
John Hui
e5038f1f18 [NFC] Rename types to mention optional
(cherry picked from commit 9b0ab9095e)
2025-05-01 10:10:56 -07:00
Gabor Horvath
291a63fd80 [6.2][cxx-interop] Fix a crash with [[no_unique_address]]
Explanation: 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.
Issue: rdar://149072458
Risk: Low, the fix is targeted at a scenario that was crashing before.
Testing: Regression test added.
Original PR: #80786
Reviewer:
2025-04-25 12:51:13 +01:00
Gabor Horvath
998591e9f9 [cxx-interop] Do not import zero-sized fields
Zero sized fields are messing up the offset calculations when we import
C++ fields to Swift. We assume that the size of the field is determined
by the type of the field. This is not true for fields marked with
no_unique_address. Those fields can have 0 size while the
sizeof(decltype(field)) is still 1.

rdar://143907490
2025-02-03 11:55:54 +00:00