Commit Graph

597 Commits

Author SHA1 Message Date
Egor Zhdan
c2f6cc6e05 [cxx-interop] Update an inheritance test
`__swift_interopStaticCast` is now defined in the CxxShim module. Clients do not need to redefine it manually.
2023-08-23 18:45:52 +01:00
Finagolfin
30ba49e7fa [android][test] Fix a handful of tests and disable one CxxToSwiftToCxx bridging test
Also, make the analogous change to apple/swift-driver#1372, which gets the
sanitizer tests working on Android again, and remove the lld_lto feature in the
tests, which is now unused.
2023-08-12 16:36:35 +05:30
Saleem Abdulrasool
d1b63d6969 Merge pull request #67776 from compnerd/the-missing-link
IRGen: rework linking against CxxStdlib
2023-08-08 15:08:48 -07:00
Alex Lorenz
1addca37a2 Merge pull request #67685 from hyp/eng/hoping-one-day-67674-gets-fixed
[cxx-interop][windows] additional fix for indirect return in methods
2023-08-08 12:13:01 -07:00
Saleem Abdulrasool
1040116e32 IRGen: rework linking against CxxStdlib
Rewrite the handling for the `CxxStdlib` implicit linking to use a
slightly more functional style for filtering.  Additionally, add Windows
to the list providing the overlay.  The Windows linking scenario is a
slightly more complicated as the library names differ between static and
dynamic variants to disambiguate between import libraries and static
libraries.  Take this into account when embedding the library name so
that the linker can find the appropriate content.
2023-08-08 09:03:37 -07:00
Alex Lorenz
8d81f5e3f7 [cxx-interop][windows] Pass trivial indirect result after 'this' when invoking C++ MSVC ABI method 2023-08-07 11:15:20 -07:00
Alex Lorenz
f8dbc01dc6 [cxx-interop] do not add friend decls as a member of Swift type that represents a C++ class 2023-08-03 14:03:45 -07:00
Egor Zhdan
9c22a7a530 [cxx-interop] Memberwise init is not synthesized for C++ type with templated using decl
When Swift fails to import a member of a struct, it checks to see if this member could affect the memory layout of the struct, and if it can, Swift doesn't synthesize the memberwise initializer for this struct. This logic was overly restrictive and treated templated using-decls as potentially affecting the memory layout of the struct.

rdar://113044949
2023-07-28 19:53:59 +01:00
Alex Lorenz
15a0345857 Merge pull request #67268 from hyp/eng/windows-method-oh-no
[cxx-interop] windows methods fixes
2023-07-21 06:57:01 -07:00
Alex Lorenz
96806f4d44 [cxx-interop] Windows: unify address-only logic and mark non-trivial loadable C++ types as unavailable
Windows logic for determining address-only type layout for a C++ type is now unified with other platforms.
However, this means that on Windows, a C++ type with a custom destructor, but a default copy constructor
is now loadable, even though it's non-trivial. Since Swift does not support such type operations at the
moment (it can't be yet destroyed), mark such type as unavailable in Swift instead, when building for
the Windows target.

This fixes the Windows miscompilation related to such types when they were passed indirectly to C++
functions even though they're actually passed directly.
2023-07-20 14:58:02 -07:00
zoecarver
ffc57bf903 [cxx-interop][nfc] Fix last straggling test. 2023-07-18 17:42:56 -07:00
zoecarver
a6a0f637ab [nfc][cxx-interop] Fix a few tests. 2023-07-18 17:42:54 -07:00
zoecarver
7db669ce5b [cxx-interop] Fix a bug with explicit, nested, self-contained types (and add a test). 2023-07-18 17:42:05 -07:00
zoecarver
474a7cd834 [cxx-interop] Only mark projections of self-contained types as unsafe.
Projections of trivial types and view types aren't unsafe. This matches what was described in the vision document.
2023-07-18 17:42:05 -07:00
Andrew Trick
2817bbbabc Disable [5.9] test: Interop/Cxx/class/safe-use-of-returned-reference-with-borrowing.swift
rdar://111065819
2023-07-17 14:49:24 -07:00
Alex Lorenz
c09135b8f3 [cxx-interop] Fix the windows ABI for returning indirect values out of methods
Fixes https://github.com/apple/swift/issues/66326

This allows us to reneable Windows method tests. Note that Windows still has
a broken convention for non-trivial record with non-trivial destructor but
trivial copy-constructor, so classes in the methods.swift test need an explicit
copy constructor.

Fixes rdar://88391102
2023-07-14 15:56:05 -07:00
swift-ci
b6d248f1dd Merge pull request #66687 from hyp/eng/cxx-borrowing
[cxx-interop] add a testcase to verify that 'borrowing' works with co…
2023-07-14 13:40:16 -07:00
Alex Lorenz
3652347a6d [cxx-interop] add a testcase to verify that 'borrowing' works with copyable C++ types
This lets us verify that our suggested pattern for calling unsafe functions is working as intended in Swift

This also lets us verify that we can still pass borrowed C++ types to C++ functions that take in a const ref parameter

Tests now fixed https://github.com/apple/swift/issues/61454
2023-07-14 09:00:26 -07:00
Alex Lorenz
4d084100ce [cxx-interop] Mark C++ functions with unavailable return type as unavailable
This prevents users from calling functions with unsupported or unavailable return types. This ensures that users don't for example call a function that returns a non-copyable and non-movable type

Fixes https://github.com/apple/swift/issues/64401
2023-07-11 16:40:57 -07:00
Alex Lorenz
a10332548c [cxx-interop] Do not add base class members that cause lookup ambiguities with a derived class member of the same name
Fixes https://github.com/apple/swift/issues/66323
2023-07-07 10:38:48 -07:00
Egor Zhdan
39a837c1b5 Merge pull request #67115 from apple/egorzhdan/cxx-semantics-kind
[cxx-interop] Tweak C++ type semantics detection
2023-07-05 19:09:00 +01:00
Alex Lorenz
0be01c415f Merge pull request #67097 from hyp/eng/revert-8e0c17b27470de49fecf19d5a222ab247984d677
Revert "[interop] do not import functions whose return type is not imported"
2023-07-05 08:06:25 -07:00
Egor Zhdan
6e7fb3263b [cxx-interop] Tweak C++ type semantics detection
`CxxRecordSemanticsKind::ExplicitlyUnsafe` and `CxxRecordSemanticsKind::UnsafePointerMember` were never directly used, and those do not indicate semantics: they indicate safety of the type when used from Swift, which should be handled by another request `IsSafeUseOfCxxDecl` instead of `CxxRecordSemantics`.

Having `ExplicitlyUnsafe` and `UnsafePointerMember` as semantics indicators was problematic, for instance, for types that are move-only and store a pointer at the same time. Swift allowed the usage of these types (under the rules for `UnsafePointerMember` types) when move-only types are disabled, and did not apply the move-only attribute on such types when move-only types are enabled.

rdar://110644300
2023-07-04 20:12:06 +01:00
Alex Lorenz
14020201f5 Revert "[interop] do not import functions whose return type is not imported"
This reverts commit 8e0c17b274.
2023-07-03 10:06:54 -07:00
Arnold Schwaighofer
79894ff461 Fix test/Interop with opaque pointers 2023-07-03 03:36:07 -07:00
Alex Lorenz
4590aa1ede Merge pull request #65813 from hyp/eng/passInRegs
[cxx-interop] Itanium ABI C++ records should have address-only layout when they can't be passed in registers
2023-06-16 07:47:48 -07:00
Arnold Schwaighofer
d1d7438a70 Move windows tests to %use_no_opaque_pointers usage 2023-06-14 10:49:50 -07:00
Arnold Schwaighofer
c1a93e0bde Move tests over to use the %use_no_opaque_pointers option 2023-06-14 10:49:48 -07:00
Egor Zhdan
b459fb5fc2 [cxx-interop] Avoid linker errors when calling a defaulted constructor
When a default constructor is declared, but does not have a body because it is defaulted (`= default;`), Swift did not emit the IR for it. This was causing linker error for types such as `std::map` in libstdc++ when someone tried to initialize such types from Swift.

rdar://110638499 / resolves https://github.com/apple/swift/issues/61412
2023-06-12 20:06:49 +01:00
Alex Lorenz
b25dec58ba [cxx-interop] add a SWIFT_MUTATING customization macro
Fixes https://github.com/apple/swift/issues/66322
2023-06-06 10:54:40 -07:00
Egor Zhdan
fec48f9e63 [cxx-interop] Synthesize a deprecated zero initializer for C++ structs
When importing a C header in the C++ language mode, Clang/Swift treat C structs as C++ structs.

Currently Swift synthesizes a default initializer that zero-initializes the backing memory of the struct for C structs, but not for C++ structs.

This is causing issues in existing projects that use C libraries and rely on having the default initializer available in Swift. This change enables the synthesis of a default initializer for C++ structs. Since many C++ structs are not designed to be initialized this way, the initializer is marked as deprecated in Swift.

rdar://109727620
2023-05-31 13:41:25 +01:00
Alex Lorenz
2c4188b8f4 [cxx-interop] mark C++ classes with trivial_abi attribute as unavailable in Swift 2023-05-09 16:23:36 -07:00
zoecarver
408ce853d0 [diags][cxx-interop] for-loop -> for-in loop. 2023-05-04 10:26:45 -07:00
zoecarver
b2988f6e98 [diags][cxx-interop] Update diagnostic for 'begin' to suggest using a for-loop instead.
Does not fix the fix-it. The current fix it will be left as a stop-gap solution and we can hopefully update this fix it in the near future to actually plop in a for loop (too much work for this PR though).
2023-05-03 09:05:55 -07:00
Alex Lorenz
89b1c6391f Revert "[cxx-interop] Treat un-instantiated templated types as unsafe"
This reverts commit c81325461a.

This caused https://github.com/apple/swift/issues/65446
2023-05-01 10:25:47 -07:00
Alex Lorenz
f45d9f7662 Merge pull request #65056 from hyp/eng/no-never-ever
[interop] do not import functions whose return type is not imported
2023-04-12 08:20:04 -07:00
Alex Lorenz
5fa6d08f33 Merge pull request #65043 from hyp/eng/diag-clenup
[interop] make interop diagnostics more consistent with the rest of S…
2023-04-12 06:53:26 -07:00
Alex Lorenz
8e0c17b274 [interop] do not import functions whose return type is not imported 2023-04-11 08:27:45 -07:00
Alex Lorenz
b60d635db7 [interop] make interop diagnostics more consistent with the rest of Swift diagnostics 2023-04-10 16:20:07 -07:00
Alex Lorenz
c2315060b6 [interop] mark C++ virtual functions as unavailable in Swift
They're not yet supported
2023-04-10 11:51:38 -07:00
Alex Lorenz
612eefffd4 Merge pull request #64911 from hyp/eng/grand-macro-rename
[interop] update names and add docs for the interop C++ helper macros
2023-04-06 17:17:52 -07:00
Alex Lorenz
64a4b3159c [interop] add SWIFT_ prefix to the C++ interop annotations 2023-04-06 12:11:25 -07:00
Egor Zhdan
8f58eafbd3 [cxx-interop] Make sure interop does not trigger TBD validation errors
This disables TBD validation when C++ interop is enabled, unless an explicit `-validate-tbd-against-ir=` flag was passed.

rdar://83405989 / https://github.com/apple/swift/issues/56458
2023-04-05 16:06:09 +01:00
nate-chandler
7e15303407 Merge pull request #42019 from nate-chandler/rdar90495704
[SILOpt] Run DestroyAddrHoisting in the mandatory pipeline when optimizing.
2023-04-04 16:37:30 -07:00
Alex Lorenz
30d0c17bf9 [interop] update names and add docs for the interop C++ helper macros 2023-04-04 15:13:59 -07:00
Egor Zhdan
2f99a4ee89 Merge pull request #64897 from apple/egorzhdan/pointy-is-scary
[cxx-interop] Treat un-instantiated templated types as unsafe
2023-04-04 19:44:37 +01:00
Nate Chandler
90f7af22c9 [SILOpt] Run DestroyAddrHoisting in mandatory.
Run DestroyAddrHoisting in the pipeline where DestroyHoisting was
previously running.  Avoid extra ARC traffic that having no form of
destroy hoisting in the mandatory pipeline results in.

rdar://90495704
2023-04-04 11:11:34 -07:00
Egor Zhdan
c81325461a [cxx-interop] Treat un-instantiated templated types as unsafe
When determining whether a C++ method is safe to be imported, we look at its return type to see if it stores any pointers in its fields.

If the type is templated, we might not have its definition available yet. Unfortunately we cannot instantiate it on the spot, since the Clang AST would be read and written at the same time.

Let's stay on the safe side and treat such methods as unsafe.

rdar://107609381
2023-04-04 15:33:09 +01:00
Egor Zhdan
283d6642d2 Merge pull request #64877 from apple/egorzhdan/test-mutable-members
[cxx-interop] Update tests for mutable struct fields
2023-04-04 11:24:29 +01:00
Egor Zhdan
0e8ef4c05c [cxx-interop] Update tests for mutable struct fields
Methods that are marked as `const` in C++ can still modify a field of the object if the field is marked as `mutable`.

We previously imported all methods of structs with mutable fields as mutating in Swift. Unfortunately this doesn't work well with libstdc++, which uses mutable fields for some commonly used container types.

Our current user model assumes that we trust the `const` keyword on a C++ method, and import them as non-mutating in Swift. Let's make sure the tests reflect that.
2023-04-03 21:46:56 +01:00