Commit Graph

2783 Commits

Author SHA1 Message Date
Gábor Horváth
816290f0cd [6.2.1][cxx-interop] Do not consider function types fragile
Explanationion: Function pointer types wee always considered fragile in C++ mode,
this manifested as a regression when interfacing with glibc.
Issues: rdar://159184118
Original PRs: #84040
Risk: Low, this only removes a spurious error for library evolution.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-09-09 09:55:52 +01:00
Gábor Horváth
ef63ec99e9 [6.2][cxx-interop] Fix crash when using bridging headers in reverse interop
Explanation: The original code had the assumption we only import
modules. However, there is a flag to import an umbrella header in which
case the clang nodes have no owning module. This PR prevents a null
dereference in that case.
Issues: rdar://157489426
Original PRs: #83540
Risk: Low, added a check to avoid null dereference.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-08-07 15:58:31 +01:00
Gábor Horváth
7429124124 Merge pull request #83065 from Xazax-hun/guard-more-objc-on-6.2
[6.2][cxx-interop] Types exposed from ObjC modules should be behind a macro
2025-07-22 11:20:22 +01:00
Gabor Horvath
f48da45438 [cxx-interop] Configure requires ObjC from frontend option
We sometimes don't have the information in the modulemaps whether a
module requires ObjC or not. This info is useful for reverse interop.
This PR introduces a frontend flag to have a comma separated list of
modules that we should import as if they had "requires ObjC" in their
modulemaps.
2025-07-18 10:35:51 +01:00
Gábor Horváth
ab123929f6 [6.2][cxx-interop] Types exposed from ObjC modules should be behind a macro
Explanation: We the generated reverse interop headers to be valid C++,
so every declaration coming from an Obj-C module should be behind an
ifdef. Unfortunately, we do not always have this information but we do
know that our frameworks contain Obj-C code. So this PR makes sure every
entity coming from our frameworks are behind ifdef.
Issues: rdar://152836730
Original PRs: #83002
Risk: Low, the change is narrow and straightforward.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-07-16 11:19:55 +01:00
finagolfin
21d90c5a41 [test] Fix or disable tests for 32-bit platforms (#82501)
Fix two IRGen tests that are failing on Android armv7 and disable eight ClangImporter, C++ Interop, and SILOptimizer tests, two of which that were already failing on other 32-bit platforms.
2025-07-11 16:20:08 +05:30
Gábor Horváth
b51b58db30 [6.2][cxx-interop] Fix unqualified name lookup failure
Explanation: C++ interop synthesizes certain forwarding functions in an
_ObjC module. This confuses MemberImportVisibility. This patch adds
logic to work this around by keeping a mapping between the synthesized
and the original function and looks up where the synthesized functions
belong to based on the original functions' parent module.
Scope: C++ forward interop when MemberImportVisibility is enabled.
Issues: rdar://154887575
Original PRs: #82840
Risk: Low, a narrow change makes getModuleContextForNameLookupForCxxDecl more
precise, and it is only used with MemberImportVisibility.
Testing: Added a compiler test.
Reviewers: @egorzhdan, @tshortli, @hnrklssn
2025-07-09 17:59:11 +01:00
Doug Gregor
958e74c48c Merge pull request #82852 from DougGregor/span-back-deployment-version-fixes-6.2
[6.2] Fix deployment version for Span back-deployment library
2025-07-08 16:09:03 -07:00
Doug Gregor
5568fb25fc Update tests for Span back-deployment 2025-07-07 23:02:55 -07:00
Joe Groff
79b59f1977 Lower imported C structs and unions as addressable-for-dependencies.
C code is highly likely to want to use pointers as references between dependent
structs, and we would like to be able to readily map these to lifetime-dependent
Swift values. Making C types addressable-for-dependencies ensures that any function
producing a dependency on such a value receives a stable in-memory address for that
value, allowing borrows and inout accesses to always be representable as pointers.

rdar://153648393
2025-07-07 08:46:43 -07:00
Gábor Horváth
93e1c787f0 [6.2][cxx-interop] Fix a crash when exposing @objc Swift classes
Explanation: There was a null pointer dereference in reverse interop
when we wanted to expose an ObjC class written in Swift. There was a
crash during generating the scaffolding we do for Clang types. Since
the type is written in Swift, no such scaffolding is needed, this patch
skips this operation avoiding the null dereference.
Scope: Reverse C++ interop when exposing @ObjC classes written in Swift.
Issues: rdar://154252454
Original PRs: #82684
Risk: Low, the fix is narrow to the affected scenario.
Testing: Added a compiler test.
Reviewers: @egorzhdan
2025-07-03 09:25:38 +01:00
Egor Zhdan
0be39efad4 [cxx-interop] Fix generated header for Swift closures using CF_OPTIONS types
If a Swift class has a field, which has a closure type, which takes an instance of a `CF_OPTIONS`/`NS_OPTIONS` type as a parameter, the reverse interop logic would generate an invalid Objective-C++ header for such type.

This was discovered with UIKit's `UIControlState` type, which is declared with `NS_OPTIONS` in Objective-C, then renamed to `UIControl.State` with API Notes, and then re-exported to Objective-C++ via the generated header.

rdar://129622886

(cherry picked from commit e95c9ecffc)
2025-07-02 14:52:15 +01:00
Egor Zhdan
1a4176041e [cxx-interop] Do not crash for void begin()
When importing C++ methods, Swift always assumes that methods named `begin()` and `end()` are unsafe, since these methods commonly return iterator types that are inherently unsafe in Swift.

Some additional logic in Sema tries to diagnose usages of `.begin()` and `.end()` from Swift and suggest safe alternatives. That logic had a null pointer dereference bug.

rdar://153814676 / resolves https://github.com/swiftlang/swift/issues/82361
(cherry picked from commit 883ff18adb)
2025-07-01 17:38:30 +01:00
Henrik G. Olsson
8db1ad6222 Cherry-pick [Swiftify] Don't import counted_by with suffixed integer literals (#82469) (#82594)
Integer literal expressions with types that are not of type `int` are
printed with a suffix to indicate the type (e.g. `123U` or `456L` for
`unsigned` and `long`). This is not valid syntax for integer literals in
Swift, so until we fully translate the count expr syntax to Swift we
need to avoid importing these count expressions.

Also fixes some -Werror related stuff in test cases.

rdar://154141719
(cherry picked from commit 374658a)
2025-06-30 11:58:55 -07:00
Egor Zhdan
a686f323e5 Merge pull request #82420 from swiftlang/egorzhdan/6.2-namespace-printer-null-crash
🍒[cxx-interop] Fix printing of namespaces declared in bridging headers
2025-06-24 22:35:11 +01:00
Egor Zhdan
ee86a80ada Merge pull request #82419 from swiftlang/egorzhdan/6.2-old-runtime-tests
🍒[cxx-interop] Disable test with an older runtime
2025-06-24 22:33:37 +01:00
Egor Zhdan
45b8045e75 [cxx-interop] Fix test failure on older macOS
The runtime logic for printing a foreign reference type is behind `if #available(SwiftStdlib 6.2, *)`, which means it won't run on older versions of macOS, even if you use a newer Swift runtime.

rdar://153735437
(cherry picked from commit a292113ef2)
2025-06-24 13:40:32 +01:00
Tim Kientzle
3bacc327e9 Merge pull request #82186 from finagolfin/droid
[6.2][android] Fix the remaining tests and add a build flag that enables standalone cross-compilation SDKs
2025-06-23 11:41:14 -07:00
Egor Zhdan
782f26e726 [cxx-interop] Fix printing of namespaces declared in bridging headers
If a C++ namespace has redeclarations in a bridging header, printing AST for the namespace would crash the compiler. This is because such a redeclaration would not have an owning Clang module, and the AST printer did not account for that.

This change fixes the crash.

rdar://151715540
(cherry picked from commit cc9c51deea)
2025-06-23 18:56:19 +01:00
Egor Zhdan
b16816b4ee [cxx-interop] Disable test with an older runtime
848fad00 introduced support for printing foreign reference types. It changes both the compiler and the runtime, and having the runtime change applied is required for the test to pass. Let's not try to run it with an old runtime.

This change also splits up a test for printing of value types from a test for printing of foreign reference types, since we don't have any runtime restrictions for value types.

rdar://153205860
(cherry picked from commit 62d56067c8)
2025-06-23 18:16:24 +01:00
Egor Zhdan
bf20dab48c Merge pull request #82185 from swiftlang/susmonteiro/6.2-class-metadata-private-fields
🍒 [cxx-interop] Support for printing C++ foreign references
2025-06-23 17:52:00 +01:00
Gábor Horváth
2f7ea38223 Merge pull request #82305 from swiftlang/gaborh/shared-references-are-safe-on-6.2
[6.2][cxx-interop] Shared references are considered safe
2025-06-23 18:48:10 +02:00
Gábor Horváth
70f1a0f9aa Merge pull request #82175 from swiftlang/gaborh/using-shadow-decl-mangling-on-6.2
[6.2][cxx-interop] Fix crash in ASTMangler triggered by UsingShadowDecls
2025-06-23 18:46:14 +02:00
Gábor Horváth
7575b1fead [6.2][cxx-interop] Shared references are considered safe
Explanation: Shared references imported from C++ were not considered
safe. This is a widely used feature and this fix is blocking the users
from adopting strictly memory safe Swift.
Issue: rdar://151039766
Risk: Low, the fix only changes what declarations are considered safe.
Testing: Regression test added.
Original PR: #82203
Reviewer: @egorzhdan @fahadnayyar
2025-06-17 18:13:49 +01:00
Gábor Horváth
9bf1d288f6 Merge pull request #82105 from swiftlang/gaborh/disable-non-aliased-instantiations-on-6.2
[6.2][cxx-interop] Only swiftify template instantiations behind typealiases
2025-06-13 04:21:55 +01:00
Devin Coughlin
1507bcd421 Merge pull request #82193 from hnrklssn/skip-bounds-safety-types2-6.2
Cherry-pick [ClangImporter] Look through bounds attributes for template matching #82076
2025-06-12 18:36:27 -07:00
Gábor Horváth
4590d5551b [6.2][cxx-interop] Only swiftify template instantiations behind type aliases
Explanation: C++ template instantiations that are not behind type
aliases don't have corresponding Swift names that are both syntactically
and semantically valid types. This PR prevents generating swiftified
overloads for those types.
Issue: rdar://151422108
Risk: Low, we swiftify functions less often.
Testing: Regression test added.
Original PR: #81973
Reviewer: @hnrklssn
2025-06-12 19:05:36 +01:00
Gábor Horváth
5d243730b4 Merge pull request #82061 from swiftlang/gaborh/swiftify-initializers-on-6.2
[6.2][cxx-interop] Support Swiftifying C++ constructors
2025-06-12 19:01:26 +01:00
Devin Coughlin
5c4066017a Merge pull request #82106 from swiftlang/gaborh/ambiguous-overloads-on-6.2
[6.2][cxx-interop] Support _LIBCPP_PREFERRED_OVERLOAD
2025-06-12 10:17:35 -07:00
Devin Coughlin
dd2f521d86 Merge pull request #82104 from swiftlang/gaborh/swiftify-private-on-6.2
[6.2][cxx-interop] Avoid swiftifying private and protected methods
2025-06-12 10:08:29 -07:00
Meghana Gupta
8d3f38623d [NFC] Update tests and diagnostics 2025-06-11 13:15:22 -07:00
Henrik G. Olsson
96f0ab2068 Merge pull request #82076 from hnrklssn/skip-bounds-safety-types2
When instantiating templated functions with pointers to the templated type, the ClangImporter does not strip type sugar. This strips type sugar for bounds attributes, to make sure that they import the same regardless of whether they are parsed or not.

rdar://151041990
(cherry picked from commit 00afb8ba81)
2025-06-11 21:46:09 +02:00
finagolfin
6611ea97e9 [android][test] Fix or disable the remaining failing tests on the Android CI (#81398)
Also, fix and enable `IRGen/lto_autolink` for all non-Wasm targets and
`IRGen/static_initializer` for aarch64.

This should get [the community Android
CI](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-build/)
green
[again](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-arm64/).
2025-06-11 21:52:04 +05:30
susmonteiro
862efcf3a0 [cxx-interop] Support for printing C++ foreign references
Adds support for printing a C++ foreign reference in Swift.

Also skips metadata of private fields in C++ records imported as Swift classes, following up on #81035

(cherry-picked from 848fad0021)
2025-06-11 17:13:01 +01:00
Gábor Horváth
2327cec99f [6.2][cxx-interop] Fix crash in ASTMangler triggered by UsingShadowDecls
Explanation: We did not handle this declaration kind. This PR makes sure we
mangle it the same way we do for the target declaration.
Issue: rdar://152841420
Risk: Low, the fix is small, localized, and straightforward.
Testing: Regression test added.
Original PR: #82144
Reviewer: @egorzhdan @hnrklssn @j-hui
2025-06-11 11:37:27 +01:00
Henrik G. Olsson
993929c3e1 Merge pull request #81752 from hnrklssn/swiftify-sized-sizedby
[Swiftify] Support __sized_by on byte-sized pointee types

(cherry picked from commit 89b09a69e4)
2025-06-10 21:49:52 +02:00
Gábor Horváth
e700c6b8d4 [6.2][cxx-interop] Support _LIBCPP_PREFERRED_OVERLOAD
Explanation: Some functions are implemented both in libc and libc++.
Clang uses the enable_if attribute to resolve otherwise ambiguous
functions calls. This PR makes the name lookup aware of this attribute.
Issue: rdar://152192945
Risk: Low, only C/C++ APIs with enable_if attributes are affected.
Testing: Regression test added.
Original PR: #82019
Reviewer: @hnrklssn
2025-06-09 13:40:34 +01:00
Gábor Horváth
7548cbf71a [6.2][cxx-interop] Avoid swiftifying private and protected methods
Explanation: We do not serialize the private macro generated swift
method's bodies which crashes the compiler. This PR skips generating
swiftified overloads to these private/protected method to work around
the crash.
Issue: rdar://152181531
Risk: Low, the change is straightforward.
Testing: Regression test added.
Original PR: #82016
Reviewer: @hnrklssn
2025-06-09 13:15:55 +01:00
Henrik G. Olsson
fc67649b46 Merge pull request #82003 from hnrklssn/ignore-unused-bounds-attributes-6.2
[ClangImporter] Look through __ended_by and __null_terminated (#81630)
2025-06-06 08:37:17 -07:00
Gábor Horváth
2b33c44796 [6.2][cxx-interop] Support Swiftifying C++ constructors
Explanation: We did not have support to generate swiftified overload for
initializers. This PR adds that support.
Issue: rdar://152112660
Risk: Low, the feature is localized to swiftified overloads.
Testing: Regression test added.
Original PR: #81947
Reviewer: @hnrklssn
2025-06-06 15:34:15 +01:00
Henrik G. Olsson
1f1cc3bd59 update test case for release/6.2 branch 2025-06-05 01:13:51 -07:00
Pavel Yaskevich
608a37ad04 Merge pull request #81997 from xedin/using-for-default-isolation-in-file-context-6.2
[6.2][AST/Sema] SE-0478: Implement using declaration under an experimental flag
2025-06-05 00:44:21 -07:00
Henrik G. Olsson
e2661ca12e update test case for release/6.2 branch 2025-06-04 21:58:57 -07:00
Henrik G. Olsson
5ef54aaada [ClangImporter] Look through __ended_by and __null_terminated (#81630)
Previously we would not import decls containing these types. This was
not an issue, because they can only occur when -fbounds-safety or
-fexperimental-bounds-safety-attributes is passed to clang. When
SafeInteropWrappers is enabled we pass
-fexperimental-bound-safety-attributes to clang however, so these types
can now occur without the user specifying any -Xcc flags.

rdar://151611718
(cherry picked from commit ac068c2a6b)
2025-06-04 15:56:02 -07:00
Pavel Yaskevich
77a36f2512 [Tests] NFC: Rename a few Cxx tests to use .cpp extension instead of .swift
These are split-file C++ tests, this is a problem for swift-syntax
because `.swift` tests get parsed for round-trip testing if
swift-syntax is located near swift.

(cherry picked from commit c019b669a1)
2025-06-04 13:16:55 -07:00
Henrik G. Olsson
18a3f083ba Merge pull request #81827 from hnrklssn/swiftify-fixes-6.2
Swiftify fixes for 6.2
2025-06-04 11:30:34 -07:00
Artem Chikin
5749ef3c14 Hard-code the 'Darwin' module as having been built without C++ interop
Textual interfaces for 'Darwin' built with recent compilers specify that it is built witout C++ interop enabled. However, to ensure compatibility with versions of the 'Darwin' module built with older compilers, we hard-code this fact. This is required to break the module cycle that occurs when building the 'Darwin' module with C++ interop enabled, where the underlying 'Darwin' clang module depends on C++ standard library for which the compiler brings in the 'CxxStdlib' Swift overlay, which depends on 'Darwin'.
2025-06-02 14:17:53 -07:00
Artem Chikin
20b38687c5 [Implicit Module Builds] Do not query CxxStdlib Swift overlay for textual modules which were not built with c++interop
When the compiler is building a module without a defined formal C++ interop mode (e.g. building a textual interface which specifies it was built without C++ interop enabled), avoid looking up the C++ standard library Swift overlay for it. This is required for the case of the Darwin module, for example, which includes headers which map to C++ stdlib headers when the compiler is operating in C++ interop mode, but the C++ standard library Swift overlay module itself depends on 'Darwin', which results in a cycle. To resolve such situations, we can rely on the fact that Swift textual interfaces of modules which were not built with C++ interop must be able to build without importing the C++ standard library Swift overlay, so we avoid specifying it as a dependency for such modules. The primary source module, as well as Swift textual module dependencies which were built with C++ interop will continue getting a direct depedency of the 'CxxStdlib' Swift module.

This was previously fixed in the dependency scanner for explicitly-built modules in https://github.com/swiftlang/swift/pull/81415.
2025-06-02 12:51:53 -07:00
Egor Zhdan
3688f6a92f Merge pull request #81872 from swiftlang/egorzhdan/6.2-zero-sized-no-unique-address
🍒[cxx-interop] Do not crash when emitting layout of `std::string`
2025-05-31 02:23:53 +01:00
Henrik G. Olsson
58366a8159 [Swiftify] Always remove count parameters when possible (#81585)
Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

The message in the bounds check is changed so that it includes the
expected and actual value, to aid in debugging.

This patch also fixes some incorrect indentation, and adds the
Whitespace.swift test case to act as a regression test in case the
indentation changes, since the other test cases don't use significant
whitespace.

rdar://151488820
rdar://151511090
rdar://146333006
rdar://147715799
(cherry picked from commit f5fa481205)
2025-05-30 18:20:03 -07:00