Commit Graph

382 Commits

Author SHA1 Message Date
Egor Zhdan
974a6bdc0b Merge pull request #62885 from apple/egorzhdan/std-string-test-windows
[cxx-interop] Enable `test/Interop/Cxx/stdlib/use-std-string.swift` on Windows
2023-01-09 12:36:15 +01:00
Christopher White
70a7a88027 Make std.string conform to CustomDebugStringConvertible 2023-01-06 21:28:33 +00:00
Egor Zhdan
ec2a645c5e [cxx-interop] More test coverage for Collection conformances
This adds a few tests for the default implementation of subscript, slicing, map/reduce.
2023-01-06 19:22:13 +00:00
Egor Zhdan
0a8e838b47 [cxx-interop] Enable test/Interop/Cxx/stdlib/use-std-string.swift on Windows 2023-01-06 18:30:30 +00:00
Egor Zhdan
1f920d5e96 [cxx-interop] Instantiate templated operator== for iterator types
C++ iterator types are often templated, and sometimes declare `operator==` as a non-member templated function. In libc++, an example of this is `__wrap_iter` which is used as an iterator type for `std::vector` and `std::string`.

We don't currently import templated non-member operators into Swift, however, we still want to support common C++ iterator patterns.

This change adds logic to instantiate templated non-member `operator==` for types that define `iterator_category` and are therefore likely to be valid iterator types.

rdar://97915515
2023-01-04 11:56:36 +00:00
Egor Zhdan
1625013def [cxx-interop] Update and enable libc++ module interface test
rdar://84036022
2023-01-03 16:26:52 +00:00
Egor Zhdan
623bfb1223 [cxx-interop] Fix typo in test/Interop/Cxx/stdlib/overlay/Inputs/custom-iterator.h 2022-12-22 19:32:29 +00:00
Egor Zhdan
abfd790bb4 Merge pull request #62575 from apple/egorzhdan/cxx-disambiguate
[cxx-interop] Disambiguate const and non-const methods consistently
2022-12-19 17:36:03 +00:00
Egor Zhdan
168ef490af [cxx-interop] Disambiguate const and non-const methods consistently
When importing a C++ struct that contains two methods that only differ in const-ness, we append `Mutating` to the name of the non-const method to make it possible to call from Swift unambiguously.

Unfortunately that logic was dependent on the order in which we import methods of a class: the `Mutating` suffix was added when another method with the same name was already imported.

This caused lookup failures, and the behavior was incorrect when the pair of methods return instances of an unsafe type: the const overload was renamed as `Unsafe` properly, but the non-const overload was not renamed.
2022-12-19 15:03:04 +00:00
Alex Lorenz
9759c4dd69 [interop] add a testcase for std::set iteration in Swift 2022-12-15 14:27:40 -08:00
Egor Zhdan
bbf8f0d5bc [cxx-interop] Fix lookup failure for operators in templated classes
If an operator is declared as a method of a templated class, we were failing to look it up during auto-conformance to `UnsafeCxxInputIterator`.

This fixes `Interop/Cxx/stdlib/use-std-map.swift` on Ubuntu.

rdar://102420290
2022-12-13 17:05:38 +00:00
Egor Zhdan
69b511bf84 Revert "[cxx-interop] Workaround interpreter crash"
This reverts commit 5d86f3e15a.
2022-12-07 16:18:39 +00:00
Egor Zhdan
5d86f3e15a [cxx-interop] Workaround interpreter crash
https://github.com/apple/swift/issues/52881 / rdar://49906866
2022-12-07 12:37:25 +00:00
Egor Zhdan
9f542898cd Merge pull request #62293 from apple/egorzhdan/synthesize-cxx-convertible
[cxx-interop] Synthesize conformances to `CxxConvertibleToCollection`
2022-12-01 11:44:02 +00:00
Egor Zhdan
7203a05479 [cxx-interop] Replace std module name with the new spelling CxxStdlib
This is the second step in renaming the C++ stdlib module `std` into `CxxStdlib`.

See https://github.com/apple/swift/pull/61099.
2022-11-29 17:33:35 +00:00
Egor Zhdan
cb562d26b8 [cxx-interop] Synthesize conformances to CxxConvertibleToCollection
This extends the existing auto-conformance mechanism to synthesize the conformances to `CxxConvertibleToCollection` protocol for C++ sequence types.

This means that the developer can now call `Array(myCxxSequence)` or `Set(myCxxSequence)` without adding any extensions manually.
2022-11-29 14:28:17 +00:00
Egor Zhdan
3e1a6dcea4 [cxx-interop] Add CxxConvertibleToCollection protocol
Since we don't automatically conform C++ non-random-access collections to `Swift.Sequence` anymore for performance reasons, we need an alternative way to access the elements of a C++ sequence from Swift.

This allows explicitly converting a C++ sequence to a Swift Array/Set.
2022-11-24 14:04:21 +00:00
Allan Shortlidge
31d52b064c Tests: Temporarily disable Interop/Cxx/stdlib/use-std-map.swift. 2022-11-16 13:20:16 -08:00
Egor Zhdan
904938a6a4 [cxx-interop] Avoid side-effects in member lookup when conforming types to CxxSequence
This became an issue in rebranch: multiple interop tests started failing (e.g. `Interop/Cxx/foreign-reference/pod.swift`).
The problem is not specific to rebranch though.

rdar://102151836
2022-11-15 23:00:20 +00:00
Egor Zhdan
40650baf7f [cxx-interop] Disable auto-conformance to CxxSequence for non-random-access collections
Iterating over a `CxxSequence` that is not a `CxxRandomAccessCollection` triggers a copy of the C++ collection. Let's disable the automatic conformances until we find a more efficient solution.

This means that for now developers won't be able to iterate over a `std::set` or `std::list` with a Swift for-in loop. I will submit a separate patch with an alternative solution for such types.

C++ random access collections, such as `std::vector` or `std::string`, are not affected.
2022-11-15 14:42:46 +00:00
Egor Zhdan
3cf84214b8 [cxx-interop] Synthesize conformances to CxxRandomAccessCollection
This makes ClangImporter automatically conform C++ collection types to `Cxx.CxxRandomAccessCollection` protocol.

We consider a C++ sequence type to be a random access collection type its iterator conforms to `UnsafeCxxRandomAccessIterator`.
2022-11-03 12:01:18 -07:00
Egor Zhdan
3e29920949 [cxx-interop] Fix nullptr casts in auto-conformance logic
This fixes a couple cases of cases where a malformed C++ iterator type would trigger an assertion in ClangImporter: `Assertion failed: (Val && "isa<> used on a null pointer")`.
2022-11-01 15:36:30 -07:00
Egor Zhdan
0efd20d016 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This makes ClangImporter automatically conform C++ sequence types to `Cxx.UnsafeCxxInputIterator` protocol.

We consider a C++ type to be a random access iterator type if conforms to `UnsafeCxxInputIterator`, and additionally defines `operator-` and `operator+=`.
2022-10-24 17:18:42 +01:00
Egor Zhdan
6a88f75072 [cxx-interop] Implicitly import Cxx module
This lifts the requirement for the user to explicitly add `import Cxx` in Swift code that relies on protocols from the `Cxx` module, such as `CxxSequence`.
2022-10-19 15:00:50 +01:00
Egor Zhdan
3daa8753c7 Merge pull request #61554 from apple/egorzhdan/cxx-random-access-collection
[cxx-interop] Add `CxxRandomAccessCollection` protocol
2022-10-19 10:57:21 +01:00
Zoe Carver
4603c84f04 Merge pull request #61181 from zoecarver/lookup-operator
[cxx-interop] Fix friend operators that come from class template specializations.
2022-10-17 09:00:06 -07:00
zoecarver
001033763a Fix tests. 2022-10-14 13:33:21 -07:00
zoecarver
492f2aaff2 [cxx-interop] Fix friend operators that come from class template specializations. 2022-10-14 13:33:06 -07:00
Egor Zhdan
bc16131166 [cxx-interop] Add CxxRandomAccessCollection protocol
This helps to bridge C++ random access collections, such as `std::vector` and `std::string`, to Swift by conforming them to `Swift.RandomAccessCollection`
2022-10-14 17:39:37 +01:00
Egor Zhdan
c53b8147a3 [cxx-interop] Correctly check iterator conformances
To determine whether to conform a C++ type to `CxxSequence` protocol automatically, ClangImporter checks if the corresponding iterator type conforms to `UnsafeCxxInputIterator`.

This logic had false-positives, e.g. `Optional<OpaquePointer>` was treated as if it conforms to `UnsafeCxxInputIterator` while it actually doesn't. This happened because `lookupConformance` returned a conformance with a conditional requirement (`Wrapped : UnsafeCxxInputIterator`) that is not satisfied for `OpaquePointer`.

rdar://100265664
2022-10-13 18:13:31 +01:00
Egor Zhdan
6472997f54 [cxx-interop] Handle std::strings with \0 character
Previously the conversion mechanism called `std::string::c_str` and passed it to `String(cString:)` which accepts a null-terminated string. If the string contains a `\0` character, this failed to initialize the entire string properly.
2022-10-03 20:16:00 +01:00
Egor Zhdan
e28605c0c9 [cxx-interop] Allow using std::map subscript
This fixes an error that occurred when trying to use the subscript on an instance `std::map`:
```
error: cannot assign through subscript: 'map' is immutable
```
This was happening even with a mutable `std::map` instance.

`std::map::operator[]` has two overloads:
* `T& operator[]( const Key& key )`
* `T& operator[]( Key&& key )`

The second one is imported with an `inout` parameter, and we picked it as an implementation of the subscript getter because it was the last of the two overloads to get imported.

Swift does not allow subscripts with `inout` parameters. This is checked at the AST level, and those checks do not run for synthesized Swift code. This caused Swift to produce a surprising error which actually indicated that the argument of the subscript, not the instance itself, must be mutable.

rdar://100529571
2022-10-03 11:18:56 +01:00
Egor Zhdan
dc8ff65195 [cxx-interop] Do not crash for non-ASCII strings
This fixes a crash that happened when creating an instance of `std::string` from a `Swift.String` that contains non-ASCII characters.

When converted to a UTF-8 array, such characters might be represented by numbers that fall out of `CChar`'s boundaries if they have a sign bit set to 1. This is normal and shouldn't trigger a crash or an assertion failure.
2022-09-28 18:30:20 +01:00
Egor Zhdan
e3c58386b7 [cxx-interop] Recognize CxxStdlib as an alias for std
C++ standard library module is called `std`. To make it more clear to a Swift developer that this module is a C++ stdlib, and not a Swift stdlib, let's rename it to `CxxStdlib`.

This is the first step in the module rename. We don't ban `std` in this patch to be able to build SwiftCompilerSources with hosttools until a new Swift compiler is shipped.
2022-09-15 14:37:38 +01:00
Egor Zhdan
e3a321721d [cxx-interop] Synthesize conformances to CxxSequence
This makes ClangImporter automatically conform C++ sequence types to `Cxx.CxxSequence` protocol.

We consider a C++ type to be a sequence type if it defines `begin()` & `end()` methods that return iterators of the same type which conforms to `UnsafeCxxInputIterator`.
2022-08-17 16:06:37 +01:00
Saleem Abdulrasool
8fa2019db4 test: add a C++ interop test for the msvcprt module
Add a test to verify that the C++ module is importable and expected
functions are visible.  This was requested in #59767 by @egorzhdan.
2022-08-12 11:26:31 -07:00
Egor Zhdan
616edf76a8 [cxx-interop] Fix circular reference errors during conformance synthesis
These errors were sometimes produced when synthesizing conformance to `UnsafeCxxInputIterator`:
```
<unknown>:0: error: circular reference
<unknown>:0: note: through reference here
<unknown>:0: note: through reference here
```

This happened because `NominalTypeDecl::lookupDirect` attempts to deserialize Swift extensions of the type, which might belong to the module which has a dependency to the module that is currently being imported, which leads to deserialization errors.

This change makes sure we don't call `NominalTypeDecl::lookupDirect` when synthesizing conformances for C++ types.
2022-08-03 20:47:10 +01:00
Zoe Carver
1f6f9695cd Merge pull request #60223 from apple/revert-60217-disable-test2 2022-07-30 13:52:40 -07:00
Egor Zhdan
d6089c91ed [cxx-interop] Conform std::string to ExpressibleByStringLiteral
This allows `std::string` to be constructed implicitly from a String literal, which is convenient e.g. when calling C++ APIs that take `std::string` as a parameter.
2022-07-28 11:07:09 +01:00
Zoe Carver
efd15298b7 Revert "tests: diable Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift" 2022-07-25 11:14:50 -07:00
Erik Eckstein
1b4919c6c5 tests: diable Interop/Cxx/stdlib/overlay/custom-sequence-typechecker.swift
It fails in CI.
Caused by https://github.com/apple/swift/pull/59509
2022-07-25 07:44:11 +02:00
Egor Zhdan
88167d71a5 [cxx-interop] Allow iterators with out-of-class operator==
Previosly we didn't detect `func ==` that was declared out-of-class when synthesizing conformaces to `UnsafeCxxInputIterator`. Now we do.

rdar://96235368
2022-07-20 17:23:09 +01:00
Egor Zhdan
d85d2e9e75 [cxx-interop] Synthesize conformances to UnsafeCxxInputIterator
This teaches ClangImporter to synthesize conformances of C++ iterator types to `UnsafeCxxInputIterator` protocol from the `Cxx` module.

We consider a C++ type to be an iterator if it defines a subtype (usually a typedef or a using decl) called `iterator_category` that inherits from `std::input_iterator_tag`.

rdar://96235368
2022-07-20 11:44:25 +01:00
zoecarver
3498ff9765 [cxx-interop] Remove UnsafeLifetimeOperation record semantic kind.
This makes "owned" the default kind for records that have custom copy constructors and no pointer-members.
2022-07-18 17:15:15 -04:00
zoecarver
6acffbbee6 [cxx-interop] Flip the switch: only import safe APIs. 2022-07-18 17:15:15 -04:00
Egor Zhdan
cb32af50a0 [cxx-interop] Enable a test on Windows
The `Cxx` module is present on all platforms, so we can enable at least the typechecker test unconditionally.
2022-07-11 13:50:25 +01:00
Egor Zhdan
82a90b90aa [cxx-interop] Split C++ stdlib overlay into two modules
This allows projects that don't want to pull in the entire C++ standard library to use stdlib-independent C++ interop utilities like `CxxSequence`.
This also makes the utilities available on platforms where we don't currently have the `std` overlay available, e.g. Windows.
2022-06-28 15:37:20 +01:00
Egor Zhdan
6754c3cf82 [cxx-interop] Add CxxSequence protocol to the stdlib overlay
This change adds basic helper protocols and structs that are going to be used for making C++ sequences and collection safe and Swifty by adding conformances to `Swift.Sequence`, `Swift.Collection`, etc.

This is not meant to be a final design.
2022-06-27 20:40:12 +01:00
Egor Zhdan
0ced5ec070 [cxx-interop] Re-enable C++ stdlib overlay on Linux 2022-06-14 23:28:37 +01:00
Alex Hoppen
95481a54bc Revert "Merge pull request #58983 from apple/egorzhdan/cxx-overlay-linux"
This reverts commit 9e69e4dd76, reversing
changes made to 59bb4cb11a.
2022-06-08 08:35:04 +02:00