Commit Graph

382 Commits

Author SHA1 Message Date
Egor Zhdan
e5899ee167 [cxx-interop] Use fully-qualified type names of C++ template parameters
When importing C++ class template instantiations, Swift generates a type name for each instantiation. The generated names must be unique, since they are used for mangling.

If multiple different C++ types declare nested types with the same name, which are then used as template arguments, Swift was generating the same name for those template instantiations (e.g. `shared_ptr<Impl>` for different `Impl` types).

This change makes sure we use fully-qualified type names of template parameters when generating Swift type names for class template instantiations (e.g. `shared_ptr<MyNamespace.MyClass.Impl>`).

This fixes an assertion failure coming out of IRGen:
```
Assertion failed: (Buffer.empty() && "didn't claim all values out of buffer"), function ~ConstantInitBuilderBase, file ConstantInitBuilder.h, line 75.
```

rdar://141962480
2025-01-02 18:03:56 +00:00
Egor Zhdan
d2801ecee9 Merge pull request #78242 from swiftlang/egorzhdan/simulator-deployment-target
[build] Respect `DEPLOYMENT_VERSION_{IOS|TVOS|WATCHOS}` for Simulator targets
2024-12-18 18:28:55 +00:00
Egor Zhdan
0fa65bea8a Merge pull request #77864 from CrazyFanFan/feature_cxxmap
[cxx-interop]: Enhance CxxDictionary with Merging, Initializer, and Unsafe Erasure
2024-12-18 13:37:44 +00:00
Egor Zhdan
d1a435c42a [build] Respect DEPLOYMENT_VERSION_{IOS|TVOS|WATCHOS} for Simulator targets
This makes sure that Cxx and CxxStdlib modules are built with consistent deployment targets for iOS Simulator and iOS Device.

rdar://141232269
2024-12-17 15:51:18 +00:00
Gábor Horváth
8398e693e7 Merge pull request #77700 from swiftlang/gaborh/span-conversion
[cxx-interop] Explicit conversions between Swift and C++ spans
2024-12-13 19:54:27 +00:00
Gabor Horvath
b6cc118f71 [cxx-interop] Explicit conversions between Swift and C++ spans
A first step towards creating safe overloads for C++ APIs using span
(rdar://139074571).

Note that we need to mark span as owned because it the libc++
implementation was mistakenly recognized as owned and might now rely on
span methods like `data` being renamed as `__dataUnsafe`. We will change
it under a new interop version. But for the time being, we want
consistent behavior across stdlib versions.
2024-12-13 16:01:02 +00:00
Alex Lorenz
9c44e01e07 [cxx-interop][stdlib] windows - use new hash inline functions like other platforms
The PR https://github.com/swiftlang/swift/pull/77857 added windows-specific workaround for https://github.com/swiftlang/swift/issues/77856, that happened after https://github.com/swiftlang/swift/pull/77843. Unfortunately this caused a new issue on windows - https://github.com/swiftlang/swift/issues/78119. It looks like windows is suffering from a similar serialization issue as libstdc++, although its even more complex as the callAsFunction is not only a derived function from a base class, the base class although has a static call operator. In any case, the libstdc++ callAsFunction deserialization fix should align with the static operator () deserialization too, so for now make windows use the same workaround as other platforms to avoid the deserialization crash (77856).

This change was tested on i686 windows too, ensuring that IR verifier crash no longer happens
2024-12-12 23:17:17 -08:00
Egor Zhdan
282f3b127b [cxx-interop] Support CxxStdlib overlay for libc++ on Linux
This teaches Swift to rebuild the CxxStdlib overlay module from its interface when using a C++ standard library that is not the platform default, specifically libc++ on Linux.

rdar://138838506
2024-12-10 14:19:59 +00:00
Egor Zhdan
01aa141b6a Merge pull request #77987 from swiftlang/egorzhdan/no-cxx-stdlib
[cxx-interop] Avoid "libstdc++ not found" warning when `-nostdinc++` is passed
2024-12-06 14:29:13 +00:00
Egor Zhdan
9472c4ca08 [cxx-interop] Avoid "libstdc++ not found" warning when -nostdinc++ is passed
When explicitly asked not to load the C++ standard library, Swift should not emit warnings for missing libstdc++.

This fixes a compiler warning when building the Cxx module on Linux.
2024-12-05 17:51:30 +00:00
Egor Zhdan
8202dfdecd [cxx-interop] Do not emit spurious lifetime diagnostics for C++ types
In rare scenarios, Swift was emitting diagnostics that looked like this:
```
warning: 'import_owned' swift attribute ignored on type 'basic_string': type is not copyable or destructible
```

This change makes sure the compiler does not emit these (incorrect) warnings. See the inline comment for more details.
2024-12-04 16:10:08 +00:00
Crazy凡
c6b10dffd7 "Enhance CxxDictionary with removeValue(forKey:), subscript(key:default:), mergemerging and init(grouping:by:)" 2024-11-28 13:11:31 +08:00
Egor Zhdan
d7d3ca1310 Merge pull request #77611 from swiftlang/egorzhdan/std-function-ubi9
[cxx-interop] Temporarily disable a test for `std::function` on UBI 9
2024-11-14 17:34:19 +00:00
Egor Zhdan
0a4eeabd8d [cxx-interop] Temporarily disable a test for std::function on UBI 9
rdar://139839740
2024-11-14 13:45:25 +00:00
Crazy凡
aeaa8ecadd [cxx-interop] Allow removing elements from std::set. 2024-11-13 23:16:45 +08:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Akira Hatanaka
f73c2e51bd Revert "[SILGen] Fix the type of closure thunks that are passed const reference structs (#76903)" (#77309)
This reverts commit 9c44b79189.

The commit caused swift's deserialization code to crash.

rdar://138726860
2024-10-31 15:16:00 -07:00
Egor Zhdan
c854fa2c7e Merge pull request #77190 from swiftlang/egorzhdan/std-string-view
[cxx-interop] Allow creating a String from `std::string_view`
2024-10-25 01:39:01 +01:00
Egor Zhdan
cb486c6599 [cxx-interop] Allow creating a String from std::string_view
This adds overlay support for initializing a Swift String from C++ `std::string_view`, `std::u16string_view`, `std::u32string_view`.

rdar://138417835
2024-10-24 13:11:09 +01:00
Akira Hatanaka
a0b5e930b5 Disable use-std-function.swift on ubuntu-24.04 (#77101)
This fixes 9850a28ace, which was checking
for the wrong Ubuntu version.

rdar://138183421
2024-10-18 15:35:26 -07:00
Egor Zhdan
9c5519a74a Merge pull request #77098 from swiftlang/egorzhdan/chrono-linux-cxx23
[cxx-interop] Do not import `std::chrono::time_zone`
2024-10-18 20:52:02 +02:00
Egor Zhdan
c530d3c0db [cxx-interop] Do not import std::chrono::time_zone
This type is non-copyable and non-moveable despite having a defaulted move constructor. It cannot currently be expressed in Swift. Let's not try to import it into Swift.

rdar://138123064 / resolves https://github.com/swiftlang/swift/issues/76809
2024-10-18 15:25:50 +01:00
Akira Hatanaka
9850a28ace Disable use-std-function.swift on ubuntu-24.10 (#77002)
The test is failing because a templated constructor of std::function
with an rvalue-reference parameter is called, which isn't supported yet.

rdar://137637142
2024-10-17 16:21:25 -07:00
Egor Zhdan
07c4a8fe57 Merge pull request #77071 from swiftlang/egorzhdan/explicit-operator-bool
[cxx-interop] Add test for `explicit operator bool`
2024-10-17 18:23:20 +02:00
Egor Zhdan
c2cfd7500a [cxx-interop] Add test for explicit operator bool
rdar://137879510
2024-10-17 13:51:58 +01:00
Egor Zhdan
e645800edf [cxx-interop] Add test for iterator conformances with nested typedefs
Follow-up to 34f6cd3f.
2024-10-17 13:25:42 +01:00
Egor Zhdan
34f6cd3f1a [cxx-interop] Conform to UnsafeCxxContiguousIterator based on iterator_concept nested type
3a200dee has a logic bug where we tried to conform C++ iterator types to `UnsafeCxxContiguousIterator` protocol based on their nested type called `iterator_category`. The C++20 standard says we should rely on `iterator_concept` instead.

https://en.cppreference.com/w/cpp/iterator/iterator_tags#Iterator_concept

Despite what the name suggests, we are not actually using C++ concepts in this change.

rdar://137877849
2024-10-16 19:47:46 +01:00
Egor Zhdan
3a200deee9 [cxx-interop] Add UnsafeCxxContiguousIterator & UnsafeCxxMutableContiguousIterator protocols
This adds a pair of Swift protocols that represents C++ iterator types conforming to `std::contiguous_iterator_tag` requirements. These are random access iterators that guarantee that the values are stored in consequent memory addresses.

This will be used to optimize usage of C++ containers such as `std::vector` from Swift, for instance, by providing an overload of `withContiguousStorageIfAvailable` for contiguous containers.

rdar://137877849
2024-10-15 14:30:09 +01:00
Akira Hatanaka
9c44b79189 [SILGen] Fix the type of closure thunks that are passed const reference structs (#76903)
The thunk's parameter needs the @in_guaranteed convention if it's a
const reference parameter. However, that convention wasn't being used
because clang importer was removing the const reference from the
type and SILGen was computing the type of the parameter based on the
type without const reference.

This commit fixes the bug by passing the clang function type to
SILDeclRef so that it can be used to compute the correct thunk type.

This fixes a crash when a closure is passed to a C function taking a
pointer to a function that has a const reference struct parameter.

This recommits e074426 with fixes to
serialization/deserialization of function types. The fixes prevent clang
types of functions from being dropped during serialization.

rdar://131321096
2024-10-08 23:44:49 -07:00
Egor Zhdan
168413ef61 Merge pull request #76823 from swiftlang/egorzhdan/msvc-bit-module
[cxx-interop] Modularize __msvc_bit_utils on Windows
2024-10-07 12:10:55 +01:00
Egor Zhdan
16e7cbeafa [cxx-interop] Modularize __msvc_bit_utils on Windows
`__msvc_bit_utils.hpp` was added in a recent version of MSVC, and it is causing build errors for SwiftCompilerSources:
```
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\numeric:598:12: error: function '_Select_countr_zero_impl<unsigned long long, (lambda at C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.39.33519\include\numeric:598:55)>' with deduced return type cannot be used before it is defined
    return _Select_countr_zero_impl<_Common_unsigned>([=](auto _Countr_zero_impl) {
```

This change references the `__msvc_bit_utils.hpp` header from the modulemap. Since we still need to support older versions of Visual Studio that do not provide `__msvc_bit_utils.hpp`, this also teaches ClangImporter to inject an empty header file named `__msvc_bit_utils.hpp` into the system include directory, unless it already exists.

rdar://137066642
2024-10-04 13:15:20 +01:00
Egor Zhdan
535a9412df [cxx-interop] Conform std::map to ExpressibleByDictionaryLiteral
This adds an automatic conformance for `std::map` and `std::unordered_map` to Swift's `ExpressibleByDictionaryLiteral` protocol.

This makes it possible to pass a dictionary literal as an argument to a function that takes a `std::map` as parameter.

rdar://137126474
2024-10-03 13:33:40 +01:00
Egor Zhdan
9169d81bd5 Merge pull request #76830 from swiftlang/egorzhdan/cxx-set-array-literal
[cxx-interop] Conform `std::set` to `ExpressibleByArrayLiteral`
2024-10-03 12:44:07 +01:00
Egor Zhdan
f37f42fc7e [cxx-interop] Conform std::set to ExpressibleByArrayLiteral
Swift's own `Set` conforms to `ExpressibleByArrayLiteral`. This change conforms instantiations of C++ `std::set` to `ExpressibleByArrayLiteral` as well.

This makes it possible to pass an array literal as an argument to a function that takes a `std::set` as parameter.

rdar://137126325
2024-10-02 19:27:51 +01:00
Slava Pestov
9632979879 Revert "[SILGen] Fix the type of closure thunks that are passed const referen…"
This reverts commit e074426058.
2024-09-29 22:32:04 -04:00
Akira Hatanaka
e074426058 [SILGen] Fix the type of closure thunks that are passed const reference structs (#75491)
The thunk's parameter needs the @in_guaranteed convention if it's a
const reference parameter. However, that convention wasn't being used
because clang importer was removing the const reference from the
type and SILGen was computing the type of the parameter based on the
type without const reference.

This commit fixes the bug by passing the clang function type to
SILDeclRef so that it can be used to compute the correct thunk type.

This fixes a crash when a closure is passed to a C function taking a
pointer to a function that has a const reference struct parameter.

rdar://131321096
2024-09-27 07:04:47 -07:00
Egor Zhdan
530f705c92 [cxx-interop] Disable a test for std::function on Debian 12
Debian 12 ships with a version of libstdc++ that declares a templated constructor of `std::function` with an rvalue-reference parameter. This is not yet supported by Swift.

rdar://125816354
2024-09-25 14:53:47 +01:00
Ryan Mansfield
6989653b4f [test] Set _LIBCPP_VERSION check in check-libcxx-version.cpp to include 170004.
This fixes test failures of libcxx-module-interface.swift and
libcxx-symbolic-module-interface.swift where the std module was split.
e.g building against a MacOS 14.4 SDK.
2024-09-13 14:33:56 -04:00
Egor Zhdan
dd78e3b10e [cxx-interop] Add a MutableCollection test that relies on std::string being Comparable
This is possible after https://github.com/swiftlang/swift/pull/76223.
2024-09-06 17:02:38 +01:00
Egor Zhdan
03abb1f0f8 Merge pull request #76223 from ishon19/76220-string-should-conform-to-comparable
Add comparable conformance for C++ strings
2024-09-04 12:50:44 +01:00
Gábor Horváth
ecd5803ee4 Merge pull request #75923 from swiftlang/gaborh/smart-ptr-move-only
[cxx-interop] Add tests for move-only objects behind smart pointers
2024-09-04 12:47:31 +01:00
Shreyans Pathak
49d9067d3e feat: add comparable conformance for C++ strings 2024-09-03 13:47:27 -04:00
Gabor Horvath
a8dde60a4e [cxx-interop] Add tests for move-only objects behind smart pointers 2024-09-03 11:38:24 +01:00
Egor Zhdan
eee3947d43 Merge pull request #76210 from swiftlang/egorzhdan/faster-test
[cxx-interop] Make a test run faster
2024-09-03 11:02:51 +01:00
Susana Monteiro
467f25992f Merge pull request #76208 from swiftlang/susmonteiro/cxx-span-tests-hardening
[cxx-interop] change hardening level in `std::span` test
2024-09-02 18:35:54 +01:00
Egor Zhdan
7532390910 [cxx-interop] Make a test run faster
`foundation-and-std-module.swift` has started timing out in some CI jobs. This change makes the test run faster to stay within the 600 sec timeout.

rdar://134328721
2024-09-02 14:56:24 +01:00
Egor Zhdan
39b8b3c67e Merge pull request #76106 from swiftlang/egorzhdan/cxx-mutable-rac
[cxx-interop] Add `CxxMutableRandomAccessCollection` protocol
2024-09-02 11:35:43 +01:00
smonteiro2
2e2bd2f4d4 Change hardening level in std::span test
Fix rdar://134730449
2024-09-02 11:21:10 +01:00
Egor Zhdan
e1bff808d7 [build] Do not override macOS deployment target when macCatalyst is enabled
Cxx.swiftmodule should built with a macOS deployment target set to 10.9, the minimum possible version. Since we enabled macCatalyst for this target, it inadvertently started being built with a deployment target set to 13.0, which is too new and is causing build errors in certain projects.

This change makes sure that for Swift targets within the Swift project that explicitly specify `DEPLOYMENT_VERSION_OSX`, the macCatalyst build logic doesn't silently discard this flag.

rdar://133008827
2024-08-28 15:37:00 +01:00
Egor Zhdan
0ab681514b [cxx-interop] Add CxxMutableRandomAccessCollection protocol
This conforms mutable C++ container types, such as `std::vector`, to `MutableCollection` via a new overlay protocol `CxxMutableRandomAccessCollection`.

rdar://134531554
2024-08-28 12:43:42 +01:00