Commit Graph

3005 Commits

Author SHA1 Message Date
Egor Zhdan
cc23c32058 [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
2025-10-10 17:46:22 +01:00
Gabor Horvath
4af3992bb4 [cxx-interop] Restrict the uses of anonymous types
Make sure they are excluded from the reflection metadata (although in
the future we want to make sure indirect fields are included). Make sure
the users cannot refer to the anonymous field, only its members.
2025-10-10 17:46:22 +01:00
Henrik G. Olsson
33a059f689 Merge pull request #84136 from hnrklssn/print-lifetime-arg-backticks
[ASTPrinter] Escape @_lifetime arguments when needed

Printing a LifetimeDescriptor would never wrap it in backticks (even if originally wrapped in backticks). This would result in the output not being able to be parsed

rdar://159992995
2025-09-08 15:29:16 -07:00
Henrik G. Olsson
5ea84522fa update Interop/C/swiftify-import/counted-by-noescape.swift 2025-09-07 12:10:23 -07:00
Susana Monteiro
cd01e914fa Merge pull request #84121 from susmonteiro/susmonteiro/copy-operations-requires-clause
[cxx-interop] Check if copy constructor is eligible
2025-09-05 17:40:06 +01:00
Gábor Horváth
8cc45ddec3 Merge pull request #84105 from Xazax-hun/no-vwt-for-anon-types
[cxx-interop] Basic support for anonymous structs with non-copyable fields
2025-09-05 09:07:17 +01:00
susmonteiro
96d00189e7 [cxx-interop] Check if copy constructor is eligible 2025-09-04 22:09:42 +01:00
Gabor Horvath
0c909a8395 [cxx-interop] Basic support for anonymous structs with non-copyable fields
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.
2025-09-04 15:20:17 +01:00
Anthony Latsis
158708b9ad Merge pull request #84098 from swiftlang/jepa-main3
[test][Windows] Fix some test failures on rebranch
2025-09-04 06:58:14 +01:00
Anthony Latsis
65285ff2d0 [test][Windows] Fix Interop/Cxx/class/method/msvc-abi-return-indirect-trivial-record.swift failure on rebranch
On rebranch the `captures(none)` attribute comes after `sret`.

Both the syntax and relative order of the LLVM `nocapture` parameter
attribute changed in llvm/llvm-project#123181.
2025-09-04 01:07:16 +01:00
Egor Zhdan
45dcb68af0 [cxx-interop] Test initializing foreign reference type when passed by parameter
This only adds a test.
2025-09-03 16:21:55 +01:00
Susana Monteiro
6e56c24a03 Merge pull request #83973 from susmonteiro/nonescapable-std-optional
[cxx-interop] Prevent crash when importing a std::optional of nonescapable
2025-09-02 10:04:06 +01:00
Gábor Horváth
1b856e1fc9 Merge pull request #84040 from Xazax-hun/func-ptr-is-not-fragile
[cxx-interop] Do not consider function types fragile
2025-09-01 20:39:37 +01:00
susmonteiro
1bfa16a419 [cxx-interop] Prevent crash when importing a std::optional of nonescapable 2025-09-01 17:50:49 +01:00
Gabor Horvath
ff6679da28 [cxx-interop] Do not consider function types fragile
We can refer to these types in resilient interfaces when compiled in ObjC
interop mode.
2025-09-01 15:05:41 +01:00
Gabor Horvath
7ac9a81b1e [cxx-interop] Add attribute to hide Swift declarations from interop
This can help work around problems when the names of a C++ declaration
and a Swift declaration would collide, or to temporarily work around
compiler bugs.

rdar://152838988&140802127&158843666
2025-09-01 12:29:34 +01:00
Egor Zhdan
521c2cea94 Merge pull request #83943 from egorzhdan/egorzhdan/wstring
[cxx-interop] Provide overlay for `std::wstring`
2025-08-28 10:35:51 +01:00
Egor Zhdan
0e1be7769b Merge pull request #83701 from ludwwwig/interop-strcpy
[cxx-interop] Copy lazily bridged Cocoa strings correctly without hanging
2025-08-27 22:13:53 +01:00
Evan Wilde
5eb727b668 Merge pull request #83908 from etcwilde/ewilde/freebsd-disable-known-failures
FreeBSD: Disable known/in-progress test failures
2025-08-27 10:30:53 -07:00
Egor Zhdan
4dda78846d [cxx-interop] Provide overlay for std::wstring
This adds CxxStdlib overlay features to `std::wstring` similarly to `std::string`, `std::u16string`, etc.

This lets clients e.g. convert between Swift String and C++ wide string simply by calling an initializer.

rdar://159272493
2025-08-27 13:20:09 +01:00
fahadnayyar
720406fdd6 Diagnose unannotated C++ APIs returning SWIFT_SHARED_REFERENCE at Swift call sites (#83025)
This patch improves the warning for C++ APIs returning
`SWIFT_SHARED_REFERENCE` types but not annotated with either
`SWIFT_RETURNS_RETAINED` or `SWIFT_RETURNS_UNRETAINED` in the following
ways:

1. The warning for missing `SWIFT_RETURNS_(UN)RETAINED` annotations is
now emitted on Swift use sites, rather than while importing the API
(func/method decls).
- This logic is now implemented as a Misl Diagnostic in function
`diagnoseCxxFunctionCalls` in file lib/Sema/MiscDiagnostics.cpp.
- The warning is now triggered only when the API is actually used, which
reduces noise in large C++ headers.
- These warnings are still gated behind experimental-feature-flag `WarnUnannotatedReturnOfCxxFrt`

rdar://150800115
2025-08-25 14:44:25 -07:00
Egor Zhdan
a782752131 [cxx-interop] Add missing // REQUIRES: executable_test (#83896) 2025-08-26 00:58:20 +05:30
Eric Miotto
aa927b44b1 Merge pull request #83840 from edymtt/edymtt/disable-some-tests-on-backdeploy-configurations
Restrict a few tests to run against the just built runtime
2025-08-25 11:59:16 -07:00
Evan Wilde
d68d62d4ac FreeBSD: CXX Interop Voidify Tests
Disable Cxx tests failing on failed import of incorrectly imported
voidify symbol. This isn't just a FreeBSD issue, it's related to how we
import libcxx. Apple has a modified libcxx module to work around the
issue, but is not implemented in the copy on FreeBSD.

rdar://121551667

```
[2025-08-25T14:24:03.682Z] ld.lld: error: undefined hidden symbol: void* std::__1::__voidify[abi:se190107]<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&)
[2025-08-25T14:24:03.682Z] >>> referenced by use-std-optional-a50ecf.o
[2025-08-25T14:24:03.682Z] >>>               /tmp/lit-tmp-_7a7wdmg/use-std-optional-a50ecf.o:(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* std::__1::__construct_at[abi:se190107]<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&))
[2025-08-25T14:24:03.682Z] >>> referenced by use-std-optional-a50ecf.o
[2025-08-25T14:24:03.682Z] >>>               /tmp/lit-tmp-_7a7wdmg/use-std-optional-a50ecf.o:(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>* std::__1::__construct_at[abi:se190107]<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>*, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>&&))
[2025-08-25T14:24:03.682Z] clang: error: linker command failed with exit code 1 (use -v to see invocation)
[2025-08-25T14:24:03.682Z] <unknown>:0: error: link command failed with exit code 1 (use -v to see invocation)
```
2025-08-25 09:31:11 -07:00
Egor Zhdan
d1a1f70c61 Merge pull request #83850 from egorzhdan/egorzhdan/frt-indirection
[cxx-interop] Pass foreign reference types with correct level of indirection
2025-08-24 04:50:59 +01:00
Egor Zhdan
0a766e59ce [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
2025-08-22 11:56:57 +01:00
Egor Zhdan
d89333f85c Merge pull request #83825 from egorzhdan/egorzhdan/constexpr-in-namespace
[cxx-interop] Import constexpr globals in a namespace
2025-08-21 12:19:21 +01:00
Egor Zhdan
ed70a1bb1a Merge pull request #83802 from egorzhdan/egorzhdan/frt-windows
[cxx-interop] Enable reference-counted types on Windows
2025-08-21 09:56:41 +01:00
Eric Miotto
d6df83d8fa Restrict a few tests to run against the just built runtime
Addresses rdar://158442561
2025-08-20 14:50:47 -07:00
Egor Zhdan
7c9bd160c1 Merge pull request #83813 from egorzhdan/egorzhdan/partial-spec-var
[cxx-interop] Do not import partial specializations of variables
2025-08-20 15:55:04 +01:00
Egor Zhdan
6bdd44e78e [cxx-interop] Import constexpr globals in a namespace
rdar://150756970 / resolves https://github.com/swiftlang/swift/issues/81327
2025-08-20 12:57:58 +01:00
Egor Zhdan
b440c4ff70 [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
2025-08-20 12:35:10 +01:00
Hamish Knight
303c1ee951 Merge pull request #83803 from hamishknight/err-fallback 2025-08-20 10:13:23 +01:00
Egor Zhdan
d837d6052c Merge pull request #83811 from egorzhdan/egorzhdan/ambiguous-init
[cxx-interop] Avoid ambiguous C++ overloads for functions from Swift extensions
2025-08-19 20:29:29 +01:00
Egor Zhdan
77975524be [cxx-interop] Do not import partial specializations of variables
They do not get imported correctly and trigger linker errors.

rdar://149232900 / resolves https://github.com/swiftlang/swift/issues/80802
2025-08-19 18:41:07 +01:00
Hamish Knight
5c3f6703a0 Remove diag::type_of_expression_is_ambiguous
This is a diagnostic that is only really emitted as a fallback when
the constraint system isn't able to better diagnose the expression.
It's not particulary helpful for the user, and can be often be
misleading since the underlying issue might not actually be an
ambiguity, and the user may well already have a type annotation. Let's
instead just emit the fallback diagnostic that we emit in all other
cases, asking the user to file a bug.
2025-08-19 17:14:23 +01:00
Egor Zhdan
cfbdd76493 [cxx-interop] Avoid ambiguous C++ overloads for functions from Swift extensions
Reverse interop has logic that avoids emitting ambiguous overloads into the generated header. That logic did not apply for functions declared within Swift extensions. This meant that the generated C++ header would sometimes not compile.

rdar://158252800
2025-08-19 15:27:26 +01:00
Egor Zhdan
5a6219cbf7 Merge pull request #83793 from egorzhdan/egorzhdan/c-frt-validation
[cxx-interop] Validate C foreign reference types
2025-08-19 15:05:35 +01:00
Egor Zhdan
3d3331f1c6 Merge pull request #83787 from egorzhdan/egorzhdan/weak-reference
[cxx-interop] Prohibit weak references to foreign reference types
2025-08-19 12:15:38 +01:00
Gábor Horváth
46677a53f5 Merge pull request #83786 from Xazax-hun/diag-for-templated-classes
[cxx-interop] Make sure we emit lifetime warnings for templated classes
2025-08-18 20:16:49 +01:00
Egor Zhdan
c73f528b82 [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
2025-08-18 19:18:18 +01:00
Gábor Horváth
37663cef2c Merge pull request #83785 from Xazax-hun/embedded-swift-reverse-interop
[cxx-interop] Make reverse interop header compile in embedded mode
2025-08-18 18:09:09 +01:00
Egor Zhdan
9abadf5483 [cxx-interop] Prohibit weak references to foreign reference types
This fixes a runtime crash when a `weak` reference to a C++ foreign reference type is used.

Instead of a runtime crash, Swift would now emit a compiler error saying that `weak` keyword is incompatible with foreign reference types.

rdar://124040825 / resolves https://github.com/swiftlang/swift/issues/83080
2025-08-18 18:08:43 +01:00
Gábor Horváth
d2691dcb61 Merge pull request #83370 from Xazax-hun/addressable-param-copy-reapply
Reapply [cxx-interop] Avoid copies when accessing pointee
2025-08-18 16:41:27 +01:00
Gabor Horvath
5d8a0335e2 [cxx-interop] Make sure we emit lifetime warnings for templated classes
rdar://155542619
2025-08-18 15:17:51 +01:00
Gabor Horvath
ad502146b3 [cxx-interop] Make reverse interop header compile in embedded mode
These are the minimal changes to make the reverse interop header compile
when embedded Swift is used. Previously, the _SwiftStdlibCxxOverlay.h
and the generated interop header disagreed on the mangling of a symbol.

This is not sufficient yet to use the embedded Swift standard library from
reverse interop, there are some missing symbols while linking. But this
PR enables doing reverse interop without using the stdlib types like
swift::String in C++. Follow-up PRs will fix the rest of the issues.

rdar://154740225
2025-08-18 13:11:43 +01:00
Egor Zhdan
d8edd86cfb Merge pull request #83753 from egorzhdan/egorzhdan/retain-release-unsigned
[cxx-interop] Allow retain/release operations to return an unsigned integer
2025-08-18 11:52:33 +01:00
Gabor Horvath
66d15a1813 Reapply [cxx-interop] Avoid copies when accessing pointee
After #83289 and #82879 landed we should no longer get deserialization
failures and this feature is no longer behind a flag. This patch also
changes how we query if a function's return value depends on self.
Previously, we queried the lifetime dependencies from the Swift
declaration. Unfortunately, this is problematic as we might have not
finished fully importing the types in the function signature just yet
and the compiler might end up populating the conformance tables
prematurely. To work this around, I store functions with self-dependent
return values where lifetimes are computed in the importer for later
use.

The PR also adds a test to make sure the addressable dependency feature
will not result in deserialization errors.

rdar://155319311&154213694&112690482&128293252
2025-08-18 10:51:43 +01:00
Egor Zhdan
299f17395c Merge pull request #83761 from egorzhdan/egorzhdan/tilde-operator
[cxx-interop] Import tilde operators
2025-08-18 08:25:52 +01:00
Gábor Horváth
16fbfec493 Merge pull request #83754 from Xazax-hun/reenable-addressable-params 2025-08-16 09:17:46 +01:00