Commit Graph

6187 Commits

Author SHA1 Message Date
Henrik G. Olsson
6a9ac22cd2 Merge pull request #84507 from hnrklssn/swiftify-import-as-method
[Swiftify] Add support for free functions imported as instance methods

(cherry picked from commit ed4f058c01)
2025-10-07 20:12:44 -07:00
Egor Zhdan
c5c33d6c5a [cxx-interop] Allow retain/release operations to be methods
Some foreign reference types such as IUnknown define retain/release operations as methods of the type.

Previously Swift only supported retain/release operations as standalone functions.

The syntax for member functions would be `SWIFT_SHARED_REFERENCE(.doRetain, .doRelease)`.

rdar://160696723
(cherry picked from commit e78ce6165f)
2025-09-23 14:45:41 +01:00
Gábor Horváth
859f60d4d4 Merge pull request #84120 from Xazax-hun/no-vwt-for-anon-types-on-6.2
[6.2][cxx-interop] Basic support for anonymous structs with non-copyable fields
2025-09-17 15:38:33 +02:00
Gábor Horváth
95ff51c739 Merge pull request #84199 from Xazax-hun/no-reflection-anon-types
[cxx-interop] Restrict the uses of anonymous types
2025-09-11 09:56:35 +01:00
Egor Zhdan
7b6b576d45 [cxx-interop] Avoid trying to instantiate copy constructor of std::optional<NonCopyable>
In libc++, `std::optional` inherits from several mixin base types. Some of those base types do not declare a deleted copy constructor for non-copyable value types, which works fine because clients are not supposed to use those base types directly from C++.

Swift, however, imports all of the transitive base types when importing a C++ type. As part of this, ClangImporter will attempt to instantiate e.g. `std::__optional_copy_assign_base<NonCopyable>`, and will fail with a hard error while doing so.

rdar://152718041

(cherry picked from commit 124055a51a)
2025-09-10 18:42:26 +01:00
Gábor Horváth
67bc46d0d0 [6.2][cxx-interop] Fix duplicate symbol error with default arguments
Explanation: We synthesize a Swift function that only calls a C++ funtion that produces the default argument. We produce this function for each module that imports the C++ function with the default argument. This symbol needs to be public as it is created in the context of the Clang module and used from the Swift module. To avoid the linker errors, we always emit this function into the client which is also the right thing to do as the whole body is a single function call.
Issues: rdar://160182651
Original PRs: #82333
Risk: Low, this is merged to main in early July and no one complained
yet. And the change is straightforward.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-09 10:17:44 +01:00
Gábor Horváth
e58347e8d2 [6.2][cxx-interop] Basic support for anonymous structs with non-copyable fields
Explanation: Anonymous structs cannot be copied or moved, these operations only
can happen to their enclosing non-anonymous types. Stop trying to emit special
member functions and value witness tables for these structs. This fix is
required to unblock a high priority libc++ change that fixes an
unintended ABI break.
Issues: rdar://159928354
Original PRs: #84105
Risk: Medium. I believe this is the right change but hard to anticipate
if something depends on the presence of these operations (which is
likely to be a bug). But this is required to unblock an important libc++
fix.
Testing: Added a compiler test.
Reviewers: @rjmccall
2025-09-05 10:53:46 +01:00
susmonteiro
29e5549e44 [6.2][cxx-interop] Prevent crash when importing a std::optional of nonescapable
When we tried to create a std::optional of a nonescapable type in Swift, the compiler would run into an assertion failure.
This is because std::optional has an anonymous union where one of its members would be of this nonescapable type.

Turns out that we were not handling anonymous unions with nonescapable members correctly.
Fields of anonymous unions are injected to the parent as IndirectFieldDecl, so we need to handle these indirect fields the same way we handle "normal" fields.

rdar://156704699
2025-09-04 16:21:21 +01:00
Gábor Horváth
3ba22e2ac0 Merge pull request #84080 from Xazax-hun/fix-rvalue-ref-virtual-on-6.2
[6.2][cxx-interop] Fix import virtual methods with rvalue ref params
2025-09-04 10:51:51 +01:00
Gábor Horváth
a2ced86c80 Merge pull request #84078 from Xazax-hun/simd-is-escapable-on-6.2
[6.2][cxx-interop] Handle vector types in escapability analysis
2025-09-04 10:50:32 +01:00
Gábor Horváth
448cdce50c [6.2][cxx-interop] Fix import virtual methods with rvalue ref params
Explanation: We generate forwarding calls for virutal methods. These forwarding
calls had a type error when the original parameter had an rvalue reference type.
In those scenarios we need to insert a static cast to make the type checker happy.
Issues: rdar://154969620
Original PRs: #83453
Risk: Low, narrow fix.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-03 12:41:53 +01:00
Gábor Horváth
a2f4aba7e0 [6.2][cxx-interop] Handle vector types in escapability analysis
Explanation: Whenever we have a vector type, the escpability depends on the
escapability of the element type. This will enable us to consider more type
like std::vector<simd::float3> as safe by default.
Issues: rdar://157141552
Original PRs: #83542
Risk: Low, narrow fix.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-03 12:36:16 +01:00
Gábor Horváth
cda402c225 [6.2][cxx-interop] Do not define inherited copy/move operations
Explanationi: When inheriting constructors, we define the inherited ctors in the derived class. We should not do that for copy and move operations as these operations can never be invoked (the implicit/defined/deleted ctor in the derived class always takes precedence). This fixes an issue where the lifetime parameters are not getting inferred for these spurious constructor definitions. This should also save us from doing some redundant work in the importer.
Issues: rdar://153081347
Original PRs: #83694
Risk: Low, we do not import some ctors that can never be called.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-03 11:30:03 +01:00
Egor Zhdan
eaf9a80ab8 Merge pull request #83810 from egorzhdan/egorzhdan/6.2-c-frt-validation
🍒[cxx-interop] Validate C foreign reference types
2025-08-21 09:55:26 +01:00
Egor Zhdan
a57432aae9 Merge pull request #83815 from egorzhdan/egorzhdan/6.2-retain-release-unsigned
🍒[cxx-interop] Allow retain/release operations to return an unsigned integer
2025-08-20 02:58:41 +01:00
Egor Zhdan
44429648ea [cxx-interop] Allow retain/release operations to return an unsigned integer
Release/retain functions for C++ foreign reference types might return a reference count as an integer value.

Swift previously emitted an error for such functions, saying that the retain/release functions need to return void or a reference to the value.

rdar://157853183

(cherry picked from commit 3887ed0637)
2025-08-19 18:11:18 +01:00
Egor Zhdan
fd7b7fede2 [cxx-interop] Validate C foreign reference types
Swift validates the retain/release operations for foreign reference types to check for obvious errors, e.g. a wrong parameter type or return type.

That logic was only running for C++ foreign reference types. This patch enables it for C foreign reference types as well.

rdar://158609723
(cherry picked from commit c73f528b82)
2025-08-19 15:09:39 +01:00
Gábor Horváth
88ea7f198d Merge pull request #83692 from Xazax-hun/fix-ref-name-mangling-on-6.2
[6.2][cxx-interop] Fix name mangling for reference types
2025-08-14 20:52:32 +01:00
Gábor Horváth
037e348526 Merge pull request #83691 from Xazax-hun/check-safety-function-types-on-6.2
[6.2][StrictMemorySafety] Check the safety of return types of calls
2025-08-14 20:27:53 +01:00
Doug Gregor
c291adaaa5 Merge pull request #83677 from DougGregor/c-interop-improvements-6.2 2025-08-14 12:13:47 -07:00
Gábor Horváth
f974dba2e0 [6.2][cxx-interop] Fix name mangling for reference types
Explanation: When generating type names for template instantiations for
name mangling we did not generate correct names for reference types
resulting in collisions in the mangled names.
Issues: rdar://157237301
Original PRs: #83176
Risk: Low, the generated string is only used for name mangling.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-08-13 11:57:49 +01:00
Gábor Horváth
99f9c318ca [6.2][StrictMemorySafety] Check the safety of return types of calls
Explanation: There were some scenarios where we could call an unsafe
function without marking the expression as unsafe. These affect mostly
cases where the function's result is passed to another function or
returned. This PR makes sure we always flag functions with unsafe return
types, even if their result is not stored anywhere for later use.
Issues: rdar://157237301
Original PRs: #83520
Risk: Low, worst case scenario the user has to add redundant unsafe
keywords in strict memory safe mode.
Testing: Added a compiler test.
Reviewers: @DougGregor
2025-08-13 11:49:22 +01:00
Artem Chikin
fe4880072e Merge pull request #82939 from artemcm/62_DepScanExplicitInput
[6.2 🍒][Dependency Scanning] Consider `-swift-module-file` inputs when looking for dependencies
2025-08-12 23:00:13 -07:00
Doug Gregor
99eeda50a6 Fix for bad cherry-pick to the 6.2 branch 2025-08-12 12:59:25 -07:00
Crazy凡
21f23dcebb [c++ interop] Swift should allow multiple SWIFT_CONFORMS_TO_PROTOCOL attributes on a C++ class.
(cherry picked from commit c05d1fc135)
2025-08-12 12:19:59 -07:00
Doug Gregor
ed05421b6c Documentation comment cleanups
(cherry picked from commit 2e7e8843ca)
2025-08-12 11:29:55 -07:00
Doug Gregor
fb4e3164b7 Add SWIFT_NONCOPYABLE_WITH_DESTROY to swift/bridging header
This packages up the ~Copyable and "destroy" attributes in a macro.

(cherry picked from commit 8909f24440)
2025-08-12 11:29:27 -07:00
Doug Gregor
89018a7ec8 [Clang importer] Diagnose various issues with the new "destroy:"
When we cannot respect the "destroy:" annotation, mark the type as
deprecated with a message thst says why there is a problem. There are
various potential problems:

* Multiple conflicting destroy functions
* Destroy functions that don't meet the pattern
* Type isn't imported as a move-only type
* Type has a non-trivial destructor (in C++)

(cherry picked from commit 63135e8929)
2025-08-12 11:29:24 -07:00
Doug Gregor
8b10bdc189 [Clang importer] Allow noncopyable C structs to define "destroy" operation
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:

    typedef struct __attribute__((swift_attr("~Copyable")))
                   __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers")))
      WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo;

    void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo);

This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.

Implements rdar://156889370.

(cherry picked from commit 6ba560fb4b)
2025-08-12 11:27:54 -07:00
Doug Gregor
f10c42f7a3 [Clang importer] Allow C structs to be noncopyable, too
In C interoperability mode, respect the ~Copyable annotation on C
structs to import them as ~Copyable types.

Fixes rdar://156877772.

(cherry picked from commit 21b9b9f713)
2025-08-12 11:27:53 -07:00
Doug Gregor
8a4d30c620 [Clang importer] Handle Swift conformances on typedefs and C types
The code for handling Swift conforms_to attributes was specific to
C++ record types. Generalize it to work on typedefs imported as
nominal types, also in C.

Fixes rdar://156290361.

(cherry picked from commit c5db5cced5)
2025-08-12 11:27:50 -07:00
Egor Zhdan
7a67b350e9 [cxx-interop] Enable foreign reference types in C interop
Most of the logic for C++ foreign reference types can be applied to C types as well. Swift had a compiler flag `-Xfrontend -experimental-c-foreign-reference-types` for awhile now which enables foreign reference types without having to enable C++ interop. This change makes it the default behavior.

Since we don't expect anyone to pass `experimental-c-foreign-reference-types` currently, this also removes the frontend flag.

rdar://150308819
(cherry picked from commit 9178af3ec7)
2025-08-12 11:22:27 -07:00
Adrian Prantl
95ad22e799 [ClangImporter] Prefer serialized bridging headers in LLDB.
Especially in an explicit modules project, LLDB might not know all the
search paths needed to imported the on disk header.

rdar://157063577
(cherry picked from commit a6678476d8)
2025-08-04 11:15:55 -07:00
Artem Chikin
89268e9b4d [6.2 🍒][Dependency Scanning] Consider '-swift-module-file' inputs when looking for dependencies
Previously this flag was only used to pass explicit dependencies to compilation tasks. This change adds support for the dependency scanner to also consider these inputs when resolving dependencies.

Resolves https://github.com/swiftlang/swift-driver/issues/1951
2025-07-18 10:12:22 -07:00
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
Artem Chikin
1c9b864738 [Dependency Scanning] Keep track of each imported module's access control
Adds an access control field for each imported module identified. When multiple imports of the same module are found, this keeps track of the most "open" access specifier.
2025-07-03 11:12:46 -07:00
Henrik G. Olsson
43d8b13cf1 Merge pull request #82644 from hnrklssn/cherry-pick-macro-adjustment-check
Cherry-pick "Merge pull request #81855 from hnrklssn/swiftify-availability-check-sdk"
2025-07-03 11:07:43 -07:00
Ben Barham
075fc7de79 Merge pull request #82709 from bnbarham/6.2-dangling-reference
[6.2][ClangImporter] Fix dangling reference
2025-07-02 13:18:44 -07:00
Anthony Latsis
6ddd1c95f3 Merge pull request #82664 from swiftlang/jepa2-6.2
[6.2] Sema: Fix the insertion location for conformances attributes
2025-07-02 16:51:55 +01:00
Michael Spencer
7df095f6c1 [ClangImporter] Fix dangling reference.
LookupOverloadedBinOp stores `Args` for later use, so store the
backing array on the stack instead of using a temporary.

This fixes a crash that appeared under some build configurations.

Related to rdar://154291418

(cherry picked from commit 75ac27dc3d)
2025-07-01 19:48:15 -07:00
Pavel Yaskevich
ec750efad6 Merge pull request #82674 from xedin/rdar-154695053-6.2
[6.2][Concurrency] SE-0463: `Sendable` inference on sync and async variant…
2025-07-01 13:55:08 -07:00
Steven Wu
7606a6982e Merge pull request #82642 from cachemeifyoucan/eng/PR-153851818-release 2025-07-01 06:23:20 -07:00
Pavel Yaskevich
f422085b70 [Concurrency] SE-0463: Sendable inference on sync and async variants of imported APIs should match
This is a follow-up to https://github.com/swiftlang/swift/pull/82085
which made it so async variant doesn't get `@Sendable` inferred because
the proposal specified that inference should happen only on completion
handler parameter type of a synchronous variant of an imported API.

This runs into implementation issues related to thunking in some
cases were async convention expects the type of a completion handler
to match exactly for both variants of the imported API.

Resolves: rdar://154695053
(cherry picked from commit 30f0fa8d75)
2025-07-01 00:16:36 -07:00
Anthony Latsis
1f28a04d29 Sema: Fix the insertion location for conformances attributes 2025-07-01 02:58:48 +01:00
Henrik G. Olsson
9de099a4b0 Merge pull request #81855 from hnrklssn/swiftify-availability-check-sdk
[Swiftify] Adjust _SwiftifyImport invocation to align with the signature

(cherry picked from commit e9ba8f8a03)
2025-06-30 12:02:55 -07:00
Henrik G. Olsson
8db1ad6222 Cherry-pick [Swiftify] Don't import counted_by with suffixed integer literals (#82469) (#82594)
Integer literal expressions with types that are not of type `int` are
printed with a suffix to indicate the type (e.g. `123U` or `456L` for
`unsigned` and `long`). This is not valid syntax for integer literals in
Swift, so until we fully translate the count expr syntax to Swift we
need to avoid importing these count expressions.

Also fixes some -Werror related stuff in test cases.

rdar://154141719
(cherry picked from commit 374658a)
2025-06-30 11:58:55 -07:00
Steven Wu
bf32a8bb14 [ScanDependencies] Fix a bug in rewrite from #81454
When improving the speed of dependency scanning when the new clang API
to speed up bridging, some unintended change was introduced. This
restore the scanner to the behavior before #81454

rdar://153851818
(cherry picked from commit cebe173ada)
2025-06-30 10:51:42 -07:00
Argyrios Kyrtzidis
0127926255 Merge pull request #82513 from sina-mahdavi/sina-mahdavi/cherry-pick-fix-llvm-prefix-mapping-calls
[6.2🍒] fix calls to llvm prefix mapping functions to use space-separated option format
2025-06-27 15:08:32 -07:00
Pavel Yaskevich
56383771fe [ClangImporter] SE-0463: Implement @Sendable inference exception for global actor isolated functions
Functions that are isolated to a global actor don't have completion
handlers imported as `@Sendable`. Main actor isolated functions with
completion handlers that are always called on the main actor is a
very common Objective-C pattern, and this carve out will eliminate
false positive warnings in the cases where the main actor annotation
is missing on the completion handler parameter.

Resolves: rdar://149811049
(cherry picked from commit ab227b401d)
2025-06-26 09:32:54 -07:00
Pavel Yaskevich
b2bcd21b10 [ClangImporter] Use DefaultsToSendable with completion handler parameter
This lifts the check for the feature flag up into the `importParameterType`
from `importType` and means that completion handler type for `async` variant
is no longer gains `@Sendable` attribute.

(cherry picked from commit 74471e858b)
2025-06-26 09:32:52 -07:00