Commit Graph

103370 Commits

Author SHA1 Message Date
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
Joe Groff
04ed960218 Merge pull request #84157 from jckarter/protocol-objc-class-dyn-self-mismatch-6.2
[6.2] SILGen: Use lowered function type parameters in objc-to-swift thunks for initializers.
2025-09-09 14:41:10 -07:00
Gábor Horváth
22f5eb79aa Merge pull request #84152 from Xazax-hun/add-nullptr-to-metadata
[cxx-interop] Make the size of anonymous types metadata is unchanged
2025-09-09 10:44:07 +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
Joe Groff
f3d3c5a898 SILGen: Use lowered function type parameters in objc-to-swift thunks for initializers.
Fixes a regression caused by the `self` declaration being given a `DynamicSelfType`
even though the formal type of the enclosing function sometimes still uses the
concrete base class type.

Fixes #83876 | rdar://158956768.
2025-09-08 10:52:01 -07:00
Egor Zhdan
0059c4250a Merge pull request #84090 from egorzhdan/egorzhdan/6.2-frt-indirection
🍒[cxx-interop] Pass foreign reference types with correct level of indirection
2025-09-05 21:47:16 +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
Konrad `ktoso` Malawski
64e98e66d3 Merge pull request #83997 from ktoso/pick-nonisolatednonsending-by-default-and-distributed 2025-09-05 17:10:34 +09:00
Susana Monteiro
f592747d96 Merge pull request #84104 from susmonteiro/6.2-nonescapable-std-optional
🍒 [cxx-interop] Prevent crash when importing a std::optional of nonescapable
2025-09-05 00:53:32 +01:00
Konrad Malawski
0ab9f4f35e [Distributed] We're currently not using this isDistributedThunk func 2025-09-05 08:34:31 +09:00
Dario Rexin
5bc14ff7e6 Merge pull request #84007 from drexin/wip-159143492-6.2
[6.2][IRGen] Fix computation of spare bits for fixed arrays
2025-09-04 13:25:39 -07:00
Gábor Horváth
cbdeaec4b6 Merge pull request #84076 from Xazax-hun/os-objects-support-on-6.2
[6.2][cxx-interop] Properly support OS objects in reverse interop
2025-09-04 21:15:19 +01:00
Artem Chikin
fdaafd6a10 Merge pull request #84089 from artemcm/62_DepScanWorkersNoShareDiagEngine
[6.2 🍒][Dependency Scanning] Give each scanner worker a unique Diagnostic Engine
2025-09-04 12:42:42 -04: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
209fecaba2 [6.2][cxx-interop] Properly support OS objects in reverse interop
Explanation:These objects are behind typedefs and user code supposed to
use the typedef names. We already have some logic in place for Obj-C
interop. Reuse the same logic for C++ to use the correct names.
Issues: rdar://150453489
Original PRs: #83670
Risk: Low, only OS object types are affected and they were problemtic
before the change.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-04 10:56:24 +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
4008d3031a Merge pull request #84077 from Xazax-hun/optional-cf-type-fix-on-6.2
[6.2][cxx-interop] Fix passing optional CoreFoundation types from Swift to C++
2025-09-04 10:49:23 +01:00
Gábor Horváth
cf97e8a5fe Merge pull request #84075 from Xazax-hun/inherited-ctors-lifetime-dependence-on-6.2
[6.2][cxx-interop] Do not define inherited copy/move operations
2025-09-04 10:48:17 +01:00
Gábor Horváth
4a394fcfbb Merge pull request #84073 from Xazax-hun/func-ptr-is-not-fragile-on-6.2
[6.2][cxx-interop] Do not consider function types fragile
2025-09-04 10:47:19 +01:00
Gábor Horváth
f114a1e60f Merge pull request #84053 from Xazax-hun/attr-hide-swift-decls-on-6.2
[cxx-interop] Add attribute to hide Swift declarations from interop
2025-09-04 10:37:36 +01:00
Artem Chikin
0fa722f41f [6.2][Dependency Scanning] Give each scanner worker a unique Diagnostic Engine
Otherwise, when multiple workers encounter a diagnostic simultaneously we can encounter races which lead to corrupted diagnostic data or crashes

Resolves rdar://159598539
2025-09-03 13:55:02 -07:00
Egor Zhdan
8178aa8b65 [cxx-interop] Pass foreign reference types with correct level of indirection
When calling a C++ function that takes a reference to a pointer to a foreign reference type, Swift would previously pass a pointer to the foreign reference type as an argument (instead of a reference to a pointer), which resulted in invalid memory accesses.

This was observed when using `std::vector<ImmortalRef*>::push_back`.

rdar://150791778
(cherry picked from commit 0a766e59ce)
2025-09-03 20:42:06 +01:00
Dario Rexin
1a10f85782 [IRGen] Fix computation of spare bits for fixed arrays
rdar://159143492

Previously all bits after the spare bits of the first element were marked as spare bits. This caused enum tags to be stored in bits used by the payload.
2025-09-03 10:27:10 -07: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
1861073cf1 [6.2][cxx-interop] Fix passing optional CoreFoundation types from Swift to C++
ExplanationThese types are passed around as reference counted pointers,
so the optional representation of them are nullable pointers.
Previously, we tried to wrap them in swift::Optional.
Issues: rdar://157667946
Original PRs: #83609
Risk: Low, the fix is narrow.
Testing: Added a compiler test.
Reviewers: @hnrklssn
2025-09-03 12:09:32 +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
Gábor Horváth
95cf518be4 [6.2][cxx-interop] Do not consider function types fragile
Explanationion: Function pointer types wee always considered fragile in
C++ mode, this manifested as a regression when interfacing with glibc.
Issues: rdar://159184118
Original PRs: #84040
Risk: Low, this only removes a spurious error for library evolution.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-03 11:18:05 +01:00
Konrad Malawski
838bc9ee67 [Concurrency/Distributed] ensure distributed thunks are @concurrent
Otherwise the "nonisolated nonsending by default" mode blows up as
distributed thunk signatures dont match expectations.

This undoes the fix from https://github.com/swiftlang/swift/pull/83940
and applies the fix on the synthesis side of the distributed thunks,
such that they are @concurrent always -- which keeps their old semantics
basically, regardless of what "default" mode we have.
2025-09-03 18:58:49 +09:00
eeckstein
90e245a840 Merge pull request #84006 from eeckstein/fix-array-property-opt-6.2
[6.2] Optimizer: fix an ownership violation when duplicating loops
2025-09-02 18:52:35 +02:00
Gábor Horváth
fa65cc9b2b [cxx-interop] Add attribute to hide Swift declarations from interop
Explanation: We generate declarations in the C++ interop header with
"unavailable" annotations when we cannot export something to C++. These
declarations can collide with existing names. Previously, there were no
ways to resolve these name collisions. This PR introduces a new
attribute to hide declarations from the interop header.
Issues: rdar://158843666
Original PRs: #82616
Risk: Low, this adds a new, straightforward code path.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-02 13:28:27 +01:00
Alejandro Alonso
f15d600460 Merge pull request #83953 from Azoy/62-extended-existentials
[6.2] [AST & Runtime] Correctly mangle extended existentials with inverse requirements
2025-08-30 11:10:21 -07:00
Erik Eckstein
9081edd4c2 Optimizer: fix an ownership violation when duplicating loops
If a guaranteed value is used in a dead-end exit block and the enclosing value is _not_ destroyed in this block, we end up missing the enclosing value as phi-argument after duplicating the loop.
TODO: once we have complete lifetimes we can remove this check again.

rdar://159125605
2025-08-29 17:28:57 +02:00
Konrad Malawski
011d2d2edc [Concurrency/Distributed] nonisolated-nonsending by default breaks distributed thunks
the new NonisolatedNonsendingByDefault upcoming feature breaks remote
calls in distributed actors, because the expected isolation doesn't
match and the runtime swift_distributed_execute_target_resume will
crash.

This is a short term fix to unblock adopters, however preferably we
should mark the thunks as nonisolated(nonsending), though that seems to
be more involved.

resolves rdar://159247975
2025-08-29 16:38:10 +09:00
nate-chandler
5f20a5b8b5 Merge pull request #83962 from nate-chandler/cherrypick/release/6.2/rdar158149082
6.2: [AllocBoxToStack] Don't destroy in dead-ends.
2025-08-28 14:26:38 -07:00
Nate Chandler
5798e71baf [MemoryLifetimeVerifier] Permit leaks in dead-ends 2025-08-28 05:47:40 -07:00
eeckstein
6713168abd Merge pull request #83947 from eeckstein/fix-combine-apply-of-convertfunction-6.2
[6.2] SILCombine: fix `convert_function` -> `apply` peephole for generic function types
2025-08-28 06:38:46 +02:00
Nate Chandler
8e98bcfefd [AllocBoxToStack] Don't destroy in dead-ends.
It is valid to leak a value on paths into dead-end regions.
Specifically, it is valid to leak an `alloc_box`.  Thus, "final
releases" in dead-end regions may not destroy the box and consequently
may not release its contents.  Therefore it's invalid to lower such final
releases to `dealloc_stack`s, let alone `destroy_addr`s.  The in-general
invalidity of that transformation results in miscompiling whenever a box
is leaked and its projected address is used after such final releases.

Fix this by not treating final releases as boundary markers of the
`alloc_box` and not lowering them to `destroy_addr`s and
`dealloc_stack`s.

rdar://158149082
2025-08-27 17:12:51 -07:00
nate-chandler
c89bc61167 Merge pull request #83711 from nate-chandler/cherrypick/release/6.2/rdar158083136
6.2: [InlineArray] Fix outlining metadata collection.
2025-08-27 12:57:54 -07:00
Alejandro Alonso
0877b04efe Merge pull request #81365 from Azoy/my-existentials
[AST & Runtime] Correctly mangle extended existentials with inverse requirements
2025-08-27 10:00:59 -07:00
Erik Eckstein
bf3a05b7a5 SILCombine: fix convert_function -> apply peephole for generic function types
Currently we cannot deal with generic arguments/returns. Bail in this case.

Fixes a compiler crash
rdar://158809851
2025-08-27 17:39:12 +02:00
Mykola (Nickolas) Pokhylets
5d70bea757 Cherry-pick SE-0481 with source-incompatible changes to closure capture semantics still wrapped into a feature
See https://github.com/swiftlang/swift/pull/80440 and https://github.com/swiftlang/swift/pull/82732
2025-08-22 10:31:42 +02:00
Pavel Yaskevich
26ba69ab79 [CSSimplify] SE-0324: Use correct conversion when converting array types
If the argument type is an array and it's passed to an imported declaration
that accepts a raw pointer, the solver should use an "array-to-c-pointer"
conversion instead of the one for pointers.

Resolves: rdar://158629300
(cherry picked from commit 29b04f4a63)
2025-08-21 16:00:48 -07:00
Egor Zhdan
5c7d5f6414 [cxx-interop] Enable reference-counted types on Windows
This enables the use of reference-counted foreign reference types on Windows. As it turns out, the assertion that was failing on Windows previously was unnecessary. This also enabled many of the tests on Windows.

rdar://154694125 / resolves https://github.com/swiftlang/swift/issues/82643

(cherry picked from commit b440c4ff70)
2025-08-21 11:20:14 +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
Pavel Yaskevich
f408b2db4a Merge pull request #82922 from xedin/nonexhaustive-enums-6.2
[6.2][AST/Sema] SE-0487: Implement `@nonexhaustive` attribute and new enum exhaustivity checking rule
2025-08-20 20:31:07 -07: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