Commit Graph

1876 Commits

Author SHA1 Message Date
Gábor Horváth
a9d59034b3 Merge pull request #76956 from swiftlang/gaborh/treat-c-struct-as-resilient
[cxx-interop] Consider extern "C" structs as resilient
2024-10-14 12:31:16 +01:00
Gabor Horvath
625f45a928 [cxx-interop] Consider extern "C" structs as resilient
Changing the members of these structs are still API and ABI breaking
changes but they are not as fragile as C++ classes where adding/removing
virtual functions or doing other changes can also result in breaking the
ABI.

rdar://119319825
2024-10-10 17:58:09 +01:00
swift-ci
1a59843e00 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-10 09:15:22 -07:00
Gábor Horváth
b5a85ed8dc Merge pull request #76907 from swiftlang/gaborh/assume-resilient-cxx-types
[cxx-interop] Add flag to assume C++ types are resilient
2024-10-10 17:15:15 +01:00
swift-ci
c49a0e84d3 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-09 10:44:16 -07:00
Meghana Gupta
4df55a12cd Merge pull request #76834 from meg-gupta/lifetimedepmultiple
Replace dependsOn with @lifetime
2024-10-09 10:17:43 -07:00
Gabor Horvath
cf3b333eb6 [cxx-interop] Add flag to assume C++ types are resilient
Currently, C++ types cannot appear in resilient interfaces. There are
some cases where this is overly restrictive. We plan to improve the
logic to detect what types should not appear on resilient moduel
boundaries. In the meantime, this PR introduces a flag to disable these
errors. Users relying on this flag are on their own, this should only be
a temporary workaround until we land further improvements to this
diagnostic.

rdar://137457118
2024-10-09 11:47:10 +01:00
swift-ci
43530d6fb5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 23:54:07 -07: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
swift-ci
929ebb1290 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-08 15:14:22 -07:00
Meghana Gupta
008431c3b4 Update some dependsOn tests to @lifetime 2024-10-08 15:12:13 -07:00
Meghana Gupta
f591680c2a Update SIL printing of @lifetime
Lifetime dependencies in SIL tests continue to be represented as a type modifier on the target.

As before, they are represented as a LifetimeDependentTypeRepr in the AST.
2024-10-08 15:10:59 -07:00
Ravi Kandhadai
cda4dc54b6 Merge pull request #76859 from j-hui/fix-frt-conversion
[cxx-interop] Fix swift::Type to clang::QualType conversion to correctly handle foreign reference types
2024-10-08 15:05:48 -07:00
John Hui
ec506f824b [cxx-interop] Fix swift::Type to clang::QualType conversion to correctly handle foreign reference types
For types imported from C++, the original clang::TypeDecl is saved in
the swift::Type and reused for conversions back to clang::QualType.
However, the conversion did not account for foreign reference types,
which should be mapped to a pointer to the C++ record type, rather than
the record type itself.

This bug first appeared as a function template instantiation failure
due to a sanity check performed by SwiftDeclConverter::foreignReferenceTypePassedByRef()
but may also affect other round-tripping type conversions.
The added test case demonstrates the template instantiation scenario,
where templates were being used to overcome Swift/C++ interop's current
lack of support for class inheritance.

rdar://134979343
2024-10-07 14:40:38 -07:00
swift-ci
48648e95a7 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-07 04:14:40 -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
swift-ci
068fb6dd2b Merge remote-tracking branch 'origin/main' into rebranch 2024-10-04 13:09:09 -07:00
Egor Zhdan
106c743f69 Merge pull request #76869 from swiftlang/egorzhdan/remove-extern-c-tests
[cxx-interop] Remove redundant `[extern_c]` in tests
2024-10-04 20:42:50 +01:00
Egor Zhdan
80cd6b0795 [cxx-interop] Remove redundant [extern_c] in tests
`[extern_c]` module attribute has no effect in these tests.
2024-10-04 16:50:21 +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
swift-ci
4505e5a30d Merge remote-tracking branch 'origin/main' into rebranch 2024-10-03 09:35:14 -07:00
Egor Zhdan
5285c22ef3 Merge pull request #76846 from swiftlang/egorzhdan/std-map-by-literal
[cxx-interop] Conform `std::map` to `ExpressibleByDictionaryLiteral`
2024-10-03 17:29:59 +01:00
swift-ci
7a9a95d9f5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-03 09:14:49 -07:00
Akira Hatanaka
90e4a98a46 [cxx-interop] Use the name of the typedef when an unnamed class is used as a template argument (#76844)
This fixes a bug where IRGen would try to use the same name for two
different protocol witness methods.

rdar://134149098
2024-10-03 08:58:29 -07: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
swift-ci
cd956f3b28 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-03 04:53:49 -07: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
swift-ci
72619743bf Merge remote-tracking branch 'origin/main' into rebranch 2024-09-30 05:15:11 -07: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
swift-ci
f00e29868e Merge remote-tracking branch 'origin/main' into rebranch 2024-09-28 08:15:47 -07:00
Allan Shortlidge
d2b562aca3 Merge pull request #76756 from tshortli/member-import-visibility-cxx
SE-0444: Fix interactions with Cxx interop
2024-09-28 08:07:03 -07:00
swift-ci
21ca39540e Merge remote-tracking branch 'origin/main' into rebranch 2024-09-27 22:54:31 -07:00
fahadnayyar
bd9095e69f Merge pull request #76733 from fahadnayyar/cxx-interop-frt-retain-release-diagnostics-patch-2
[cxx-interop] Add diagnostic notes when retain/release are not import…
2024-09-27 22:37:07 -07:00
fahadnayyar
d1b380ffd6 [cxx-interop] Add diagnostic notes when retain/release are not imported into swift for SWIFT_SHARED_REFERENCE types 2024-09-27 18:57:31 -07:00
Allan Shortlidge
b11bb1ceea SE-0444: Fix interactions with Cxx interop.
With the upcoming `MemberImportVisibility` feature enabled, code built with Cxx
interop also enabled could be rejected by the compiler with cryptic errors
about the `__ObjC` module not being imported. This is the result of a
surprising implementation detail of Cxx interop. When importing C++ namespaces
and their members, the Clang importer puts these declarations in the Clang
header import module (a.k.a. the bridging header module, `__ObjC`). C++
namespaces don't have a logical modular home in the Swift AST because they can
span multiple modules, so it's understandable why this implementation was
chosen. However, the concrete members of namespaces also get placed in the
`__ObjC` module too, and this really confuses things.

To work around this idiosyncrasy of Cxx interop, I've introduced
`Decl::getModuleContextForNameLookup()` which returns the module that a
declaration would ideally belong to if Cxx interop didn't have this behavior.
This alternative to `Decl::getModuleContext()` is now used everywhere that
`MemberImportVisibility` rules are enforced to provide consistency.

Additionally, I found that I also had to further special-case the header import
module for Cxx interop because it turns out that there are some additional
declarations, beyond imported namespaces, that also live there and need to be
implicitly visible in every source file. The `__ObjC` module is not implicitly
imported in source files when Cxx interop is enabled, so these declarations are
not deemed visible under normal name lookup rules. When I tried to add an
implicit import of `__ObjC` when Cxx interop is enabled, it broke a bunch
tests. So for now, when a decl really belongs to the `__ObjC` module in Cxx
interop mode, we just always allow it to be referenced.

This Cxx interop behavior really needs a re-think in my opinion, but that will
require larger discussions.

Resolves rdar://136600598.
2024-09-27 12:16:38 -07:00
swift-ci
327f62e576 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-27 07:15:47 -07: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
swift-ci
59a7eb4710 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-27 06:15:16 -07:00
Gábor Horváth
89708dd15a Merge pull request #76673 from swiftlang/gaborh/structural-escapability
[cxx-interop] Add rules to recognize escapability of aggregates
2024-09-27 13:58:33 +01:00
Ben Barham
c15bc19946 Merge remote-tracking branch 'origin/main' into main-to-rebranch
Conflicts:
  - `lib/Serialization/ModuleFileSharedCore.cpp` new headers on main
2024-09-26 16:01:56 -07:00
Egor Zhdan
c126bab3ae Merge pull request #74185 from swiftlang/egorzhdan/frt-test-nonobjc
[cxx-interop] C++ reference types are not `AnyObject`s
2024-09-26 12:54:45 +01:00
Egor Zhdan
ec962fcae9 Merge pull request #76695 from swiftlang/egorzhdan/disable-std-function-debian
[cxx-interop] Disable a test for `std::function` on Debian 12
2024-09-25 18:35:53 +01:00
swift-ci
0210aab197 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-25 07:55:15 -07:00
Egor Zhdan
82c076c9d3 [cxx-interop] C++ reference types are not AnyObjects
C++ foreign reference types have custom reference counting mechanisms, so they cannot conform to `AnyObject`.

Currently Swift's type system treats C++ FRTs as `AnyObject`s on non-Darwin platforms, which is incorrect. This change makes sure the behavior is consistent with Darwin platform, i.e. a cast of C++ FRT to `AnyObject` is rejected by the typechecker.

rdar://136664617
2024-09-25 15:15:11 +01: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
Gabor Horvath
f2911643f4 [cxx-interop] Reenable test
Additional errors were triggered due to missing availability attributes.

rdar://136620623
2024-09-25 11:38:18 +01:00
swift-ci
44e93b7140 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-24 14:54:56 -07:00
Pavel Yaskevich
f59ccb796b [Tests] NFC: Disable Interop/Cxx/class/safe-interop-mode.swift due to failures on iphonesimulator 2024-09-24 14:36:57 -07:00
swift-ci
6dc53cce15 Merge remote-tracking branch 'origin/main' into rebranch 2024-09-24 11:14:58 -07:00