Commit Graph

597 Commits

Author SHA1 Message Date
finagolfin
21d90c5a41 [test] Fix or disable tests for 32-bit platforms (#82501)
Fix two IRGen tests that are failing on Android armv7 and disable eight ClangImporter, C++ Interop, and SILOptimizer tests, two of which that were already failing on other 32-bit platforms.
2025-07-11 16:20:08 +05:30
Gábor Horváth
b51b58db30 [6.2][cxx-interop] Fix unqualified name lookup failure
Explanation: C++ interop synthesizes certain forwarding functions in an
_ObjC module. This confuses MemberImportVisibility. This patch adds
logic to work this around by keeping a mapping between the synthesized
and the original function and looks up where the synthesized functions
belong to based on the original functions' parent module.
Scope: C++ forward interop when MemberImportVisibility is enabled.
Issues: rdar://154887575
Original PRs: #82840
Risk: Low, a narrow change makes getModuleContextForNameLookupForCxxDecl more
precise, and it is only used with MemberImportVisibility.
Testing: Added a compiler test.
Reviewers: @egorzhdan, @tshortli, @hnrklssn
2025-07-09 17:59:11 +01:00
Joe Groff
79b59f1977 Lower imported C structs and unions as addressable-for-dependencies.
C code is highly likely to want to use pointers as references between dependent
structs, and we would like to be able to readily map these to lifetime-dependent
Swift values. Making C types addressable-for-dependencies ensures that any function
producing a dependency on such a value receives a stable in-memory address for that
value, allowing borrows and inout accesses to always be representable as pointers.

rdar://153648393
2025-07-07 08:46:43 -07:00
Egor Zhdan
1a4176041e [cxx-interop] Do not crash for void begin()
When importing C++ methods, Swift always assumes that methods named `begin()` and `end()` are unsafe, since these methods commonly return iterator types that are inherently unsafe in Swift.

Some additional logic in Sema tries to diagnose usages of `.begin()` and `.end()` from Swift and suggest safe alternatives. That logic had a null pointer dereference bug.

rdar://153814676 / resolves https://github.com/swiftlang/swift/issues/82361
(cherry picked from commit 883ff18adb)
2025-07-01 17:38:30 +01:00
Egor Zhdan
ee86a80ada Merge pull request #82419 from swiftlang/egorzhdan/6.2-old-runtime-tests
🍒[cxx-interop] Disable test with an older runtime
2025-06-24 22:33:37 +01:00
Tim Kientzle
3bacc327e9 Merge pull request #82186 from finagolfin/droid
[6.2][android] Fix the remaining tests and add a build flag that enables standalone cross-compilation SDKs
2025-06-23 11:41:14 -07:00
Egor Zhdan
b16816b4ee [cxx-interop] Disable test with an older runtime
848fad00 introduced support for printing foreign reference types. It changes both the compiler and the runtime, and having the runtime change applied is required for the test to pass. Let's not try to run it with an old runtime.

This change also splits up a test for printing of value types from a test for printing of foreign reference types, since we don't have any runtime restrictions for value types.

rdar://153205860
(cherry picked from commit 62d56067c8)
2025-06-23 18:16:24 +01:00
Egor Zhdan
bf20dab48c Merge pull request #82185 from swiftlang/susmonteiro/6.2-class-metadata-private-fields
🍒 [cxx-interop] Support for printing C++ foreign references
2025-06-23 17:52:00 +01:00
Gábor Horváth
7575b1fead [6.2][cxx-interop] Shared references are considered safe
Explanation: Shared references imported from C++ were not considered
safe. This is a widely used feature and this fix is blocking the users
from adopting strictly memory safe Swift.
Issue: rdar://151039766
Risk: Low, the fix only changes what declarations are considered safe.
Testing: Regression test added.
Original PR: #82203
Reviewer: @egorzhdan @fahadnayyar
2025-06-17 18:13:49 +01:00
finagolfin
6611ea97e9 [android][test] Fix or disable the remaining failing tests on the Android CI (#81398)
Also, fix and enable `IRGen/lto_autolink` for all non-Wasm targets and
`IRGen/static_initializer` for aarch64.

This should get [the community Android
CI](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-build/)
green
[again](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-arm64/).
2025-06-11 21:52:04 +05:30
susmonteiro
862efcf3a0 [cxx-interop] Support for printing C++ foreign references
Adds support for printing a C++ foreign reference in Swift.

Also skips metadata of private fields in C++ records imported as Swift classes, following up on #81035

(cherry-picked from 848fad0021)
2025-06-11 17:13:01 +01:00
Gábor Horváth
7548cbf71a [6.2][cxx-interop] Avoid swiftifying private and protected methods
Explanation: We do not serialize the private macro generated swift
method's bodies which crashes the compiler. This PR skips generating
swiftified overloads to these private/protected method to work around
the crash.
Issue: rdar://152181531
Risk: Low, the change is straightforward.
Testing: Regression test added.
Original PR: #82016
Reviewer: @hnrklssn
2025-06-09 13:15:55 +01:00
Pavel Yaskevich
77a36f2512 [Tests] NFC: Rename a few Cxx tests to use .cpp extension instead of .swift
These are split-file C++ tests, this is a problem for swift-syntax
because `.swift` tests get parsed for round-trip testing if
swift-syntax is located near swift.

(cherry picked from commit c019b669a1)
2025-06-04 13:16:55 -07:00
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
susmonteiro
d81d6547ba [cxx-interop] Fix metadata mismatch regarding fields of structs
In https://github.com/swiftlang/swift/pull/78467 and https://github.com/swiftlang/swift/pull/78961, we stopped emitting metadata for private C++ fields. However, this created a mismatch between the fields emitted and the number of fields + their offsets in the StructDescriptor.

rdar://147263490
(cherry picked from commit 72b13b3b48)
2025-05-23 11:16:09 +01:00
Andrew Trick
f0ec734c2e Diagnostic message: suppress printing "requires LifetimeDependence"
Adopters of the new Span and MutableSpan types should not encounter information
about an experimental feature when they attempt to use these types in
unsupported ways, such as simply returning them from a function.

Fixes rdar://151788740 (Diagnostic message: suppress printing "requires
'-enable-experimental-feature LifetimeDependence'")

(cherry picked from commit 6cc1a8d880)
2025-05-21 23:00:52 -07:00
Doug Gregor
751678d7fb Merge pull request #81235 from j-hui/cherry-pick-6.2/fix-ast-layout-dependent-types
🍒 [6.2] [cxx-interop] Avoid querying layout of dependent types during symbolic import
2025-05-06 14:04:51 -07:00
Allan Shortlidge
e867788d65 ClangImporter: Introduce the #ClangDeclarationImport diagnostic group.
The warnings that ClangImporter emits about issues it encounters while
importing declarations from Clang modules should all belong to a diagnostic
group so that users of `-warnings-as-errors` can control their behavior using
the compiler flags introduce with SE-0443. It's especially important that these
diagnostics be controllable since they are often caused by external
dependencies and therefore the developer may not have any control over whether
they are emitted.

The `#ClangDeclarationImport` diagnostic group is intentionally broad so that
developers have a way to control all of these diagnostics with a single
`-Wwarning` flag. I fully expect that we'll introduce finer-grained diagnostic
groups for some of these diagnostics in the future, but those groups should be
hierarchically nested under `#ClangDeclarationImport`, which is supported by
SE-0443.

Resolves rdar://150524204.
2025-05-05 14:19:04 -07:00
John Hui
b7812babdf Merge pull request #81212 from j-hui/base-6.2/fix-recursive-inheritance
🍒 [6.2] [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
2025-05-02 10:24:37 -07: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
John Hui
c4fed45f54 [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
It is possible for a C++ class template to inherit from a specialization
of itself. Normally, these are imported to Swift as separate (unrelated)
types, but when symbolic import is enabled, unspecialized templates are
imported in place of their specializations, leading to circularly
inheriting classes to seemingly inherit from themselves.

This patch adds a check to guard against the most common case of
circular inheritance, when a class template directly inherits from
itself. This pattern appears in a recent version of libc++,
necessitating this patch. However, the solution here is imperfect as it
does not handle more complex/contrived circular inheritance patterns.

This patch also adds a test case exercising this pattern. The
-index-store-path flag causes swift-frontend to index the C++ module
with symbolic import enabled, without the fix in this patch, that test
triggers an assertion failure due to the circular reference (and can
infinitely recurse in the StorageVisitor when assertions are disabled).

rdar://148026461
(cherry picked from commit 1f2107f357)
2025-04-30 13:40:19 -07:00
Gábor Horváth
7f0d24b810 Merge pull request #81100 from swiftlang/gaborh/unique-address-padding-on-6.2 2025-04-25 14:03:27 -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
Gábor Horváth
c3a7203221 [6.2][cxx-interop] Fix runtime crash passing FRTs as const FRT*
Explanation: There was an inconsistency between non-const and const FRT
pointers. The former used Direct_Unowned the latter used Indirect
calling convention. We want to use Direct_Unowned for both cases. The
crash was the result of a calling convention mismatch between the
SILFunctionType of a Swift closure and the SILFunctionType of the C++
function's formal parameter that is taking a function pointer. The
compiler tried to insert a conversion between the two function types
that does not exist and caused an assertion in debug compilers and
miscompilation in production compilers.
Issue: rdar://149398905
Risk: Low, the fix is targeted and we change to a well-tested behavior
with non-const FRT pointers.
Testing: Regression test added.
Original PR: #81070
Reviewer: @j-hui
2025-04-25 10:59:45 +01:00
Gábor Horváth
a3b9c409ef [6.2][cxx-interop] Properly import annotations from functions in namespace scope
Explanation: C++ namespaces imported as Swift enums. This was not
accounted for in a condition and that resulted in not importing lifetime
annotations for functions that are in namespace scope. This PR fixes
that condition.
Issue: rdar://149756644
Risk: Low, the fix is very targeted.
Testing: Regression test added.
Original PR: #80986
Reviewer: @j-hui
2025-04-23 16:18:11 +01:00
John Hui
d7b44f4e94 Merge pull request #80718 from j-hui/fix-interop-private-debug-symbols-6.2 2025-04-15 17:08:18 -07:00
Gábor Horváth
710c26e87d Merge pull request #80710 from swiftlang/gaborh/escapable-type-nonesc-field-on-6.2 2025-04-14 11:10:04 -07:00
John Hui
bafcb9dcca [cxx-interop] Do not get private discriminator for private Clang types (#80485)
When generating debug symbols for private Clang types (which we started
importing recently), the compiler crashes due to an assertion failure
from ClangModuleUnit::getDiscriminatorForPrivateDecl(), which is called
by getFilePrivateScope().

This patch fixes the issue crash by not calling getFilePrivateScope()
for Clang types. A discriminator is usually needed to disambiguate
private Swift types declared in different files, but Clang types follow
different scoping conventions that make this discriminator unnecessary.

rdar://148481025
(cherry picked from commit dd2f465025)
2025-04-10 06:54:58 -07:00
John Hui
bbe10ec53f [cxx-interop] Add test case that checks irgen for imported private members (#80515)
(cherry picked from commit ce680eae55)
2025-04-10 06:54:58 -07:00
fahadnayyar
f3ecb7ea8b [cxx-interop] convert CXXForeignReferenceTypeInitializers into SuppressCXXForeignReferenceTypeInitializers to be an opt-out flag
Turning the feature "ctor of C++ foreign reference types is callable as Swift Initializers" on by default.

rdar://148285972
2025-04-10 05:55:05 -07:00
fahadnayyar
5cab57439f [cxx-interop] Import parameterized public ctors of C++ foreign ref types as Swift Initializer (#80449)
Extends PR #79986 by adding support for calling parameterized C++ initializers from Swift. This patch synthesizes static factory methods corresponding to C++ parameterized constructors, allowing Swift to call them as Swift initializers (e.g., init(_:), init(_:_:), etc.). This patch also aded tests and logic to make sure that we emit no additional diagnostics when a C++ foreign ref type is just referred from Swift and its initializer is not explicitly called.

rdar://148285251
2025-04-10 05:55:05 -07:00
Gábor Horváth
8c4be8a37d [6.2][cxx-interop] Diagnose Escapable C++ types with non-escapable fields
Explanation: In Swift, Escapable types cannot have non-escapable stored
properties. Unfortunately, these checks could be circumvented via C++
interop, constructing invalid Swift code. This patch errors out on
importing C++ code of this shape.
Issue: rdar://148899224
Risk: Low, the fix is fairly targeted to the affected scenario.
Testing: Added tests to test suite
Original PR: #80671
Reviewer: John Hui
2025-04-10 10:23:57 +01:00
John Hui
e8bcc52356 [cxx-interop] Fix access check for nested private C++ enums (#80366)
This patch fixes the access check for nested private C++ enums to look for the SWIFT_PRIVATE_FILEID of the enclosing C++ class, if any. Previously, the check was looking at for SWIFT_PRIVATE_FILEID on the enum decl itself (which is meaningless); that prevented nested private enum members from being accessible in Swift.

This patch also specializes the type signature of getPrivateFileIDAttrs to clarify the fact that SWIFT_PRIVATE_FILEID is not a meaningful annotation on anything other than CXXRecordDecl, because that is the only kind of decl that can assign access specifiers to its members.

rdar://148081340
2025-03-31 22:23:22 -07:00
fahadnayyar
9694cc8d70 [cxx-interop] Import default public ctor of C++ foreign ref types as Swift Initializer (#79986)
Building on top of PR #79288, this update synthesizes a static factory method using the default new operator, with a call to the default constructor expression for C++ foreign reference types, and imports them as Swift initializers.

rdar://147529406
2025-03-31 20:33:53 -07:00
John Hui
bbf92fd6e1 [cxx-interop] Import non-public members of SWIFT_PRIVATE_FILEID-annotated classes (#80320) 2025-03-26 22:41:38 -07:00
Gabor Horvath
ff42f2ee79 [cxx-interop] Fix a regression making std::string unsafe
Currently, we only get warnings for using unsafe types in expressions
but not in the function signature. The tests did not use the std::string
object in the function body. As a result, we regressed and std::string
was considered unsafe.

The reason is that the annotation only mode for calculating escapability
of a type did not do what we intended. std::basic_string is
conditionally escapable if the template argument is escapable. We
considered 'char' to have unknown escapability in annotation only mode.
The annotation only mode was introduced to avoid suddenly importing
certain types as not escapable when they have pointer fields and break
backward compatibility.

The solution is to make annotation only mode to still consider char and
co as escapable types and only fall back to unknown when the inference
otherwise would have deduced non-escapable (for unannotated typed).
2025-03-26 15:25:37 +00:00
Gábor Horváth
b0ffa78e27 Merge pull request #80267 from swiftlang/gaborh/unbreak-ci-irgen
[cxx-interop] Unbreak CI
2025-03-25 16:24:03 +00:00
Gabor Horvath
bad91d6260 [cxx-interop] Unbreak CI
Make the test more resilient to variations in the IR to match what is
generated on one of the CI machines.

rdar://147091383
2025-03-25 11:39:09 +00:00
Gábor Horváth
16cefca106 Merge pull request #80197 from swiftlang/gaborh/mutating-class-func
[cxx-interop] Do not create mutating properties for classes
2025-03-24 14:17:36 +00:00
Gabor Horvath
51193fa538 [cxx-interop] Do not create mutating properties for classes
In Swift, only value types can have mutating instance member functions
or computed properties. The importer logic was violating this invariant
when generating setters for bit fields of shared references.

Fixes #80182
2025-03-21 19:02:35 +00:00
Guillaume Lessard
d66fde2e47 [test] fix availability 2025-03-20 13:03:54 -07:00
Susana Monteiro
a8111e4786 Merge pull request #80094 from swiftlang/susmonteiro/destructor-missing-dbg-info
[cxx-interop] Disable debug info in synthesized C++ methods
2025-03-20 10:26:56 +00:00
Andrew Trick
8432d2e788 Fix C++ interop tests using swift_attr("~Escapable")) 2025-03-19 11:59:05 -07:00
Andrew Trick
64a48d08e1 Update tests for strict @lifetime type checking 2025-03-19 11:59:04 -07:00
susmonteiro
25efb5fc04 [cxx-interop] Disable debug info in synthesized C++ methods
rdar://141167229
2025-03-19 18:02:50 +00:00
Florian Hahn
dff5da4b32 [Test] Use -disable-llvm-merge-function-pass to unblock MergeFunc fix.
To unblock CI for https://github.com/swiftlang/llvm-project/pull/10112.
2025-03-14 09:49:14 +00:00
Gabor Horvath
14d92ff7a1 [cxx-interop] Test static factory backed initializers with value types
We can use swift_name to import a static factory function as a Swift
initializer. This was tested with foreign reference types but not with
value types. This PR adds a test case for the latter.

rdar://117531428
2025-03-11 17:36:54 +00:00
Gábor Horváth
94ca6fa86a Merge pull request #79793 from swiftlang/gaborh/remove-feature-flag 2025-03-10 17:57:26 +01:00