Commit Graph

1876 Commits

Author SHA1 Message Date
Egor Zhdan
38371cdeb9 Merge pull request #73508 from apple/egorzhdan/witness-table-test
[cxx-interop] Re-enable test for reference types in generic contexts
2024-05-09 13:24:34 +01:00
Egor Zhdan
27829b7ca7 [cxx-interop] Fix execution test for reference types
When compiling with optimizations, the compiler might reorder some instructions, leading to test failures.

rdar://127795755
2024-05-09 13:08:36 +01:00
Egor Zhdan
d84cbace14 [cxx-interop] Fix test for reference types on armv7k
This adjusts the test to be correct on 32-bit platforms.

rdar://127795392
2024-05-09 12:56:07 +01:00
Kavon Farvardin
21b303ffb0 Merge pull request #73501 from kavon/ncgeneric-not-experimental
NCGenerics: it's not experimental
2024-05-08 16:29:03 -07:00
Egor Zhdan
1078fd3bd5 Merge pull request #73366 from apple/egorzhdan/frt-release-nullptr
[cxx-interop][IRGen] Do not try to retain/release a null pointer
2024-05-08 19:39:16 +01:00
Kavon Farvardin
b04be89dce CxxInterop: remove checks for NoncopyableGenerics
It will always evaluate to true now; it's not experimental. This change
effectively reverts the following:
 - 6140ba1079
 - 78eee32467
2024-05-08 10:49:12 -07:00
Kavon Farvardin
0420310623 NCGenerics: it's no longer "experimental"
resolves rdar://127701059
2024-05-08 10:49:12 -07:00
Egor Zhdan
961a2e2558 [cxx-interop] Re-enable test for reference types in generic contexts
rdar://95738946
2024-05-08 15:31:26 +01:00
Egor Zhdan
ef47dd6655 [cxx-interop] Re-enable a test for reference counted types
This also re-enables a test that was disabled since 2022.

rdar://97532642
2024-05-07 20:20:45 +01:00
Egor Zhdan
bd6da5dc64 [cxx-interop][IRGen] Do not try to retain/release a null pointer
This teaches IRGen to only emit a lifetime operation (retain or release) for a C++ foreign reference type if the pointer is not `nullptr`.

Previously the compiler would in some cases emit a release call for `nullptr`, which breaks the assumption that the argument to a custom release function is `_Nonnull`. For instance:
```
var globalOptional: MyRefType? = nil
func foo() { globalOptional = MyRefType.create() }
```
When emitting IR for the assignment operation to `globalOptional`, the compiler would emit code to first retrieve the existing value of `globalOptional` and release it. If the value is `nil`, it does not need to be released.

rdar://97532642
2024-05-07 20:20:45 +01:00
Egor Zhdan
c88e65b604 Merge pull request #73337 from apple/egorzhdan/unnamed-subscript-param
[cxx-interop] Do not crash when calling a subscript with unnamed parameter
2024-05-06 14:25:00 +02:00
Egor Zhdan
ae27e884d5 [cxx-interop] Do not crash when calling a subscript with unnamed parameter
This fixes a crash in SILGen when calling a C++ subscript that has an unnamed parameter from Swift.

The parameters from a C++ `operator[]` get carried over to the synthesized Swift subscript. If the Swift parameter has no name, there is no way to refer to it in SIL. However, the synthesized subscript accessor needs to pass this parameter to C++.

This change makes sure that we give a name to the Swift parameter if there isn't already a name on the C++ side.

rdar://83163841
2024-05-01 12:30:18 +01:00
Akira Hatanaka
428fe21855 [SILGen] Fix a bug where the wrong convention was being used for computing the type of a closure thunk (#73299)
The ObjC selector family convention was being used instead of the C
function type convention.

rdar://127090209
2024-04-29 18:03:59 -07:00
Egor Zhdan
442f9c12d3 [cxx-interop] Test symbolic interface generation for new features
This adds a test that ensures that a symbolic interface for a C++ module will include these features:
* move-only types
* virtual methods

Default arguments are already tested.

rdar://125816549
2024-04-25 16:33:02 +01:00
Egor Zhdan
fae5d2c039 Merge pull request #73222 from apple/egorzhdan/id-crash-test
[cxx-interop] Add test for an already-fixed IRGen crash
2024-04-24 23:20:45 +01:00
Egor Zhdan
62c0a3325f [cxx-interop] Add test for an already-fixed IRGen crash
resolves https://github.com/apple/swift/issues/57308
2024-04-24 18:02:44 +01:00
Egor Zhdan
b330376a43 [cxx-interop] Pull changes from swift-6 compat mode into swift-5.9
This gives projects using C++ interop compat mode 5.9 access to the new features such as virtual methods and move-only types.

rdar://126485814
2024-04-24 11:53:31 +01:00
Egor Zhdan
70c3e432f6 Merge pull request #73055 from apple/egorzhdan/ncg-unsafepointer
[cxx-interop] Import `NonCopyable*` as `OpaquePointer` if noncopyable generics are disabled
2024-04-24 00:45:25 +01:00
Egor Zhdan
6140ba1079 [cxx-interop] Do not try to use UnsafePointer<T> for non-copyable T when adding base member accessors 2024-04-23 18:47:55 +01:00
Egor Zhdan
78eee32467 [cxx-interop] Import NonCopyable* as OpaquePointer if noncopyable generics are disabled
C++ pointer type `T*` is generally imported as `Unsafe(Mutable)Pointer<T>`. However, if `T` is non-copyable in Swift (e.g. it has a deleted C++ copy constructor), using `UnsafePointer<T>` type requires noncopyable generics to be enabled.

This was causing assertion failures when building SwiftCompilerSources in https://github.com/apple/swift/pull/72912.
2024-04-23 18:47:55 +01:00
Egor Zhdan
5fce3f9d5b Merge pull request #71715 from eltociear/patch-50
Fix typo in runtime-polymorphism.swift
2024-04-22 12:36:41 +02:00
Egor Zhdan
cfe27287da Merge pull request #72903 from apple/egorzhdan/zero-init
[cxx-interop] Zero-initialize C++ structs when calling their default constructors
2024-04-17 13:39:21 +01:00
Akira Hatanaka
d76dbb1a64 [SILGen] Fix a bug where the wrong convention was being used for lowering a closure to a C++ function pointer (#73039)
Use the C function pointer convention instead of the block convention.

rdar://122977380
2024-04-16 15:18:50 -07:00
Egor Zhdan
bac5d0e9a1 [cxx-interop] Zero-initialize C++ structs when calling their default constructors
When Swift imports C structs, it synthesizes an initializer that takes no arguments and zero-initializes the C struct.

When C++ interop is enabled, Clang treats all C structs as if they were C++ structs. This means that some of the C structs will get a default constructor implicitly generated by Clang. This implicit default constructor will not zero-initialize trivial fields of the struct.

This is a common source of confusion and subtle bugs for developers who try to enable C++ interop in existing projects that use C interop and rely on zero-initialization of C structs.

rdar://115909532
2024-04-16 13:42:02 +01:00
Egor Zhdan
802f20ec00 [cxx-interop][IRGen] Assign Clang function types to copy constructors
This fixes an assertion failure when building certain projects for arm64e with `-use-clang-function-types` Swift compiler flag:
```
Expected non-null Clang type for @convention(c)/@convention(block) function but found nullptr
```

rdar://121227452
2024-04-03 19:26:32 +01:00
Egor Zhdan
a0c1027b6d Merge pull request #72766 from apple/egorzhdan/bump-std-default
[cxx-interop] Use C++17 standard by default
2024-04-03 19:13:03 +01:00
Egor Zhdan
1be7230876 [cxx-interop] Use C++17 standard by default
Clang is using C++17 standard version by default since Clang 16.

Swift’s ClangImporter should do the same, to make sure that clients who run clang and then swiftc without explicit std version see consistent behavior.

rdar://125777068
2024-04-02 16:23:32 +01:00
Alex Lorenz
bdc3717834 Merge pull request #72643 from hyp/eng/cxx-interop/xrefcts
[cxx-interop] serialize x-refs for class template specializations
2024-04-01 08:08:25 -07:00
Alex Lorenz
0c7b1cee13 [cxx-interop] serialize x-refs for class template specializations
Fixes https://github.com/apple/swift/issues/70253
2024-03-29 10:08:18 -07:00
Rintaro Ishizaki
7b3738d1d0 [SourceKit] Adjust newlines between decls
Previously, Clang modules didn't have empty lines between top-level
decls. This was inconsistent with Swift module.
2024-03-25 22:07:50 +09:00
Daniel Rodríguez Troitiño
8b4821f1a8 [cxx-interop] Fix test header to declare linkage for a implicit template (#72522)
The header was defining a function, the function created a lambda, and
the lambda was transformed into a `std::function`. This transformation
is incorrect because the function scope does not have linkage, so the
instantiated types will not have linkage either, causing the error
below.

```
.../include/c++/11.2.0/bits/invoke.h:104:5:
error: function 'std::__invoke_r<int, (lambda at .../swift/test/Interop/Cxx/stdlib/Inputs/std-function.h:9:10) &, int>' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
102 │   template<typename _Res, typename _Callable, typename... _Args>
103 │     constexpr enable_if_t<is_invocable_r_v<_Res, _Callable,
    _Args...>, _Res>
    104 │     __invoke_r(_Callable&& __fn, _Args&&... __args)
        │     ╰─ error: function 'std::__invoke_r<int, (lambda at .../swift/test/Interop/Cxx/stdlib/Inputs/std-function.h:9:10) &, int>' is used but not defined in this translation unit, and cannot be defined in any other translation unit because its type does not have linkage
        105 │     noexcept(is_nothrow_invocable_r_v<_Res, _Callable,
            _Args...>)
  106 │     {

  }
```

Declaring the function `inline` forces each TU to have their own copies
of the function, which avoids the instantiated templates from being in a
different TU than the one using them.

The header would not have worked in a normal C++ program, since none of
the functions declare linkage, they would have been defined in each TU
that included the header and it would fail linking as soon as two of
those TU tried to be linked together. Declaring the functions `inline`
avoids the problem (a more normal header, only with declarations, would
also worked).
2024-03-23 07:55:28 -07:00
Egor Zhdan
19a257b36b [cxx-interop] Test stdlib support with newer C++ standards 2024-03-21 16:44:38 +00:00
Egor Zhdan
7df8812feb Merge pull request #72006 from naknut/remove_LIBCPP_DISABLE_AVAILABILITY
Remove -D_LIBCPP_DISABLE_AVAILABILITY from tests
2024-03-19 16:18:36 +00:00
Karoy Lorentey
9725b892cc [test] More test updates 2024-03-18 13:27:46 -07:00
Kavon Farvardin
2951162527 Test: disable move-only-cxx-value-type.swift
The test was relying on UnsafeMutablePointer (UMP) not having a Copyable
 requirement. Those Copyable requirements are more prominent when using
the new infrastructure for noncopyable types. Since UMP is planned to
have `~Copyable` on its generic parameter quite soon, this shouldn't
remain broken for too long.
2024-03-14 23:10:44 -07:00
Egor Zhdan
2c6cc4a6e5 Merge pull request #72186 from apple/egorzhdan/swift-bridging-modulemap
[cxx-interop] Refer to `SwiftBridging` module from `usr/include/module.modulemap`
2024-03-11 19:01:33 +00:00
Nate Chandler
dff0b2efaa [SILGen] Allocs for VDs are var_decl.
Annotate alloc_stack instructions that correspond to VarDecls with the
var_decl flag.
2024-03-08 22:28:22 -08:00
Egor Zhdan
d494632e98 [cxx-interop] Refactor: move swift/bridging source elsewhere for better testability
rdar://123334601
2024-03-08 20:13:27 +00:00
Doug Gregor
59d1846b59 De-XFAIL a test that now works with noncopyable generics 2024-03-07 15:32:13 -08:00
Nate Chandler
e40581a61b [MoveChecker] Visit and delete markers in PO.
Visit in post-order in order to resolve markers from the inside out,
required for per-field consume.
2024-03-06 20:54:17 -08:00
Egor Zhdan
b19ca43a43 [cxx-interop] Fix virtual method tests on non-macOS 2024-03-03 00:49:45 +00:00
Egor Zhdan
5c2ae737c9 Merge pull request #71994 from apple/egorzhdan/virtual-methods
[cxx-interop] Overhaul virtual method support
2024-03-01 22:19:38 +00:00
Egor Zhdan
8ead7224b7 [cxx-interop] Overhaul virtual method support
This adds a new implementation of virtual method dispatch that handles reference types correctly.

Previously, for all C++ types an invocation of a virtual method would actually get dispatched statically. For value types this is expected and matches what C++ does because of slicing. For reference types, however, this is incorrect, we should do dynamic dispatch.

rdar://123852577
2024-03-01 19:45:58 +00:00
Egor Zhdan
53fb6531f2 [cxx-interop] Add conversions between std::chrono::duration and Swift.Duration
rdar://123696734
2024-03-01 13:15:05 +00:00
Egor Zhdan
d3a0bf4ea8 Merge pull request #71836 from apple/egorzhdan/cxx-tests-swift-6
[cxx-interop] Run tests with `swift-6` compat mode
2024-02-26 10:52:03 +00:00
Doug Gregor
0c9c734f7e Merge pull request #71722 from DougGregor/diagnostic-style-swift-default 2024-02-24 14:42:38 -10:00
Egor Zhdan
78b9de1391 [cxx-interop] Run tests with swift-6 compat mode 2024-02-23 16:24:14 +00:00
Marcus Isaksson
df759eeb85 Remove -D_LIBCPP_DISABLE_AVAILABILITY from tests 2024-02-23 13:57:53 +01:00
Akira Hatanaka
b3f302b96b [IRGen] Fix a bug where an argument wasn't annotated with sret (#71459)
Fix a bug in expandExternalSignatureTypes where it wasn't annotating a function call parameter type with sret when the result was being returned indirectly.

The bug was causing calls to ObjC methods that return their results indirectly to crash.

Additionally, fix the return type for C++ constructors computed in expandExternalSignatureTypes. Previously, the return type was always void even on targets that require constructors to return this (e.g., Apple arm64), which was causing C++ constructor thunks to be emitted needlessly.

Resolves rdar://121618707
2024-02-22 14:14:47 -08:00
Mishal Shah
cb96ec2893 Merge remote-tracking branch 'origin/main' into bump-swift-version-to-6 2024-02-22 09:44:44 -08:00