Commit Graph

3697 Commits

Author SHA1 Message Date
John Hui e350e7e716 Merge pull request #89156 from j-hui/c_int128 2026-05-20 07:58:47 -07:00
John Hui 2f48ecb66b Merge pull request #89276 from j-hui/templated-operator-crash 2026-05-20 07:56:51 -07:00
Egor Zhdan 5cbb591a9f Merge pull request #89039 from egorzhdan/egorzhdan/libstdcxx-coroutine
[cxx-interop] Add remaining C++23 headers into libstdc++ modulemap
2026-05-20 12:46:52 +01:00
Gábor Horváth c9e20ac40e Merge pull request #89211 from Xazax-hun/refcounted-ptr-improvements
[cxx-interop] SWIFT_REFCOUNTED_PTR improvements
2026-05-20 11:40:08 +01:00
John Hui 168c928212 [cxx-interop] Fix crash when specializing a templated SubscriptDecl
rewriteIntegerTypes unwrapped the outer (Self) -> ... curry layer for
any instance member, constructor, or static decl. SubscriptDecl
interface types are not curried this way (they are already
(Args) -> Element), so the strip overshot into the bare element type
and the subsequent getParams() dereferenced garbage.

Gate the unwrap on hasImplicitSelfDecl(), which is the precise
condition for having an outer Self curry layer.

rdar://177482177
2026-05-19 18:34:26 -07:00
susmonteiro 95733ba4d4 [cxx-interop] Add target-run-simple-swift-split-file 2026-05-18 14:10:36 +01:00
Gabor Horvath 6cbda479af [cxx-interop] SWIFT_REFCOUNTED_PTR improvements
If a smart pointer type had both T& and T* taking constructors, we had
no way to figure out which one to pick for bridging and failed to import
the type. This PR makes sure we handle those scenarios and also support
references in the bridging operations. Moreover, it also handles
overloading based on const-ness of the ptrs/references. And makes sure
we do not create bridging operations (for now) from rvalue references.

The PR also fixes an assertion failure for types derived from
SWIFT_REFCOUNTED_PTR annotated types.

The PR also extends the test cases with more tests for fields.

rdar://177334824
2026-05-18 12:46:01 +01:00
Patryk Stefanski 57567402a8 Merge pull request #89149 from patrykstefanski/add-guards-for-swift-span
[cxx-interop] Guard Span-referencing declarations in Cxx module interface behind version check
2026-05-16 11:38:29 -07:00
susmonteiro f643b2651d [cxx-interop] Fix abiName of C++ templates 2026-05-15 19:08:23 +01:00
Egor Zhdan 7211cf480b [cxx-interop] Add remaining C++23 headers into libstdc++ modulemap
This adds all of the remaining C++ standard library headers that weren't added to the generated libstdc++ modulemap on Linux.

rdar://175928621
2026-05-15 12:15:32 +01:00
John Hui 1fe7f685b5 [cxx-interop] Import __int128 as CInt128
Swift gained native Int128 / UInt128 types in SE-0425, but the plumbing
for bridging them across the C/C++ boundary was incomplete. CInt128 and
CUnsignedInt128 were never added to CTypes.swift, even though
BuiltinMappedTypes.def has been mapping __int128 / unsigned __int128 to
those names since 2013. Without the typealiases, every C declaration
mentioning __int128 was silently dropped at import time.

This patch adds the CInt128 and CUInt128 type aliases to CTypes.swift
for forward interop, and also adds the Int128 -> __int128 mapping in
PrintAsClang and IRABIDetailsProvider for reverse interop.

rdar://177111210
2026-05-14 20:58:50 -07:00
Patryk Stefanski 9e057e5c14 [cxx-interop] Guard Span-referencing declarations in Cxx module interface behind version check
When a newer toolchain (Swift >=6.2) is used with an older SDK whose
stdlib predates Swift 6.2, the Cxx module interface references
`Swift.Span` and `Swift.MutableSpan` which don't exist in the older
stdlib, causing build failures.

Guard individual declarations in the Cxx overlay that reference
`Span`/`MutableSpan` behind `#if canImport(Swift, _version: 6.2)`. The
guard wraps (rather than replaces) existing feature-based guards like
`$LifetimeDependence`, so both compiler capability and stdlib
availability are checked.

rdar://176312542
2026-05-14 15:41:07 -07:00
Egor Zhdan 227346870c Merge pull request #89074 from egorzhdan/egorzhdan/swift-name-this
[cxx-interop] Do not allow `swift_name` to add a method to a different struct
2026-05-14 18:18:17 +01:00
Egor Zhdan e27c9fc5f0 [cxx-interop] Do not allow swift_name to add a method to a different struct
If a C++ method is not static, let's not allow changing its parent struct type to a different type, since the implicit `this` parameter would have an incompatible type.

This fixes an assertion that would previously fire for this scenario.

rdar://161208348
2026-05-13 18:55:29 +01:00
John Hui e68687d9e8 Merge pull request #88909 from j-hui/op-bool 2026-05-13 08:31:51 -07:00
Gábor Horváth 5db4b01233 Merge pull request #89033 from Xazax-hun/optimize-slow-test
[cxx-interop] Speed up test, disable for ASAN
2026-05-13 14:41:00 +01:00
Egor Zhdan 06dacdf843 Merge pull request #89008 from egorzhdan/egorzhdan/enum-char32_t
[cxx-interop] Fix assertion for enums of type char32_t
2026-05-13 12:22:45 +01:00
John Hui 9c35230739 [cxx-interop] Synthesize __convertToBool() on-demand
Previously, we would import operator bool() by simply renaming it to
__convertToBool() using NameImporter. When we had a struct with that
member, we would conform it to CxxConvertibleToBool, which would allow
Swift.Bool to be initialized via Bool(fromCxx:).

However, this conformance relies on importing operator bool() eagerly,
and didn't work reliably when operator bool() was inherited. (Part of
the reason is because our base method synthesis logic would only create
clang::CXXMethodDecls but logic elsewhere specifically expected a
clang::CXXConversionDecl.) We also weren't handling things like
const-ness etc. directly.

This patch instead synthesizes a shim method __convertToBool() that
calls the underlying operator bool() const, which we no longer import
directly. It follows the same "look up and import on demand" pattern as
other special synthesized members that wrap operators, like .pointee and
.successor() and .subscript.

rdar://170857484
2026-05-12 17:29:12 -07:00
John Hui 0597c897b8 Merge pull request #87871 from j-hui/lazy-subscriptz 2026-05-12 07:53:36 -07:00
Gabor Horvath c08ecc6189 [cxx-interop] Speed up test, disable for ASAN
The test in this PR is one of the slowest running tests that we have
because it rebuilds foundation and the standard library modules in a new
module cache directory. This PR attempts to speed this test up by making
only one of the 4 invocations do the rebuild from scratch. This is
sufficient for the FileCheck invocation to test if the expected files
are present.

Moreover, we disable this test for ASAN builds for now where this test was
reliably timing out. Since we have other tests importing foundation, the
overlay, and the C++ standard library, we should not lose code coverage
in ASAN builds.

rdar://176484441
2026-05-12 14:06:45 +01:00
Egor Zhdan 6da4da7153 Merge pull request #88930 from egorzhdan/egorzhdan/embedded-mangled-name
[cxx-interop] Fix reverse interop overlay header in Embedded mode
2026-05-12 13:00:25 +01:00
Egor Zhdan 8dd4ac5c4f [cxx-interop] Fix assertion for enums of type char32_t
`enum XYZ : char32_t` was triggering an assertion failure in the Swift compiler. Swift expects all underlying enum types to be `ExpressibleByBuiltinIntegerLiteral`. `char32_t` was imported as `Unicode.Scalar`, which doesn't conform.

This replaces `Unicode.Scalar` with `UInt32` whenever it is used as the underlying type of a C++ enum.

rdar://148336856 / resolves https://github.com/swiftlang/swift/issues/80436
2026-05-12 11:42:29 +01:00
John Hui 07c59112ae [cxx-interop] Import attributes onto synthesized subscript accessors
The subscript synthesis path was not calling importAttributes on the
accessor thunks, so Clang attributes like __attribute__((deprecated))
on operator[] were silently dropped. Import them onto the getter/setter
AccessorDecls, matching what the ObjC subscript import path does.
2026-05-11 23:23:40 -07:00
John Hui f1f17b8c75 [cxx-interop] Synthesize subscript on-demand
The implementation for this behavior was added in a previous patch;
this patch integrates and removes unused call sites.

This patch also adds some tests exercising the overloading behavior.

Annoyingly, imported members that point to the same source location
aren't sorted correctly in the module interface, so this changes the
order in a bunch of module interface tests.

rdar://170857428

Also, un-comment broken test case of using Base::operator[], which is
fixed by this commit.

rdar://145345345
2026-05-11 23:23:39 -07:00
John Hui 9c477bd4fd [PrintAsClang] Don't assert renamed decl is includable in Cxx header printer (#88945)
When generating C++ interop headers with
-experimental-skip-non-exportable-decls, a deprecated method's
@available(*, renamed:) target may not pass shouldInclude(), e.g.,
because the target uses `throws` or types that otherwise can't be
exposed to C++.

Instead of asserting, fall through to print the raw rename string from
the attribute (same behavior as when getRenamedDecl() returns null).

rdar://151425131
2026-05-11 09:27:42 -07:00
Patryk Stefanski 435ffb3ac0 Merge pull request #88854 from patrykstefanski/add-cycle-test
[cxx-interop] Add cycle warning test (NFC)
2026-05-11 08:39:06 -07:00
Gábor Horváth 33ae606011 Merge pull request #88964 from Xazax-hun/trivial-move-only-type
[cxx-interop] Never copy trivial move-only C++ types
2026-05-10 10:52:05 +01:00
Gabor Horvath f258c513f8 [cxx-interop] Never copy trivial move-only C++ types
We used unowned calling convention for all trivial C++ types, as a
result the move checker never diagnosed when an unintentional copy is
happened through a call. This PR changes move only types to use @owned
calling convention, matching what we do for native Swift types.

Now, we can no longer get away with these copies! Unfortunately, the fix
for now is turned off for types with the "destroy" attribute because the
synthesized code is triggering some errors. This can be fixed as a
follow-up.

rdar://135615824
2026-05-08 18:24:03 +01:00
Egor Zhdan 3d9a6d5a37 [cxx-interop] Fix reverse interop overlay header in Embedded mode
When targeting Embedded Swift and using reverse C++ interop to generate a C++ interface for a Swift module, the compiler was emitting mangled names of Swift stdlib symbols that began with `$s`. This is incorrect in Embedded Swift: the mangling uses `$e` as the prefix.

Swift was also emitting references to Foundation symbols which are not available in Embedded Swift. This change removes those references.

rdar://154738972
2026-05-08 12:38:05 +01:00
Henrik G. Olsson 8e7036f78b Merge pull request #88621 from hnrklssn/swiftify-span-typedefless
[Swiftify] enable safe wrappers for std::span without typedef
2026-05-07 09:56:47 -07:00
Egor Zhdan 58349501e6 Merge pull request #88832 from egorzhdan/egorzhdan/noncopyable-nonmovable
[cxx-interop] Do not crash on references to global non-copyable non-movable variables
2026-05-07 15:06:11 +01:00
Gábor Horváth 3046563eff Merge pull request #88707 from Xazax-hun/deserialization-failure-fix
[cxx-interop] Deserialization failure with namespaces
2026-05-06 12:18:04 +01:00
Gábor Horváth a274d0d7a2 Merge pull request #86521 from Xazax-hun/reenable-span-test-mac
[cxx-interop] Reenable disabled test
2026-05-06 12:13:06 +01:00
Patryk Stefanski 54d06487c6 Merge pull request #88597 from patrykstefanski/add_returns_retained_annotations_to_cxx_thunks
[cxx-interop] Add RETURNS_RETAINED annotations to C++ thunks
2026-05-05 16:27:38 -07:00
Patryk Stefanski c5b9b6183d [cxx-interop] Add cycle warning test (NFC)
Reproducer for a cycle warning during iterator conformance derivation.
The cycle requires three structural properties:
1. A class-template iterator with a free-function operator==
2. An unrelated nested class with a member operator==
3. The nested class's parent has a field of a different specialization
   of the same iterator template

When all three are present, a module-wide operator== scan during
conformance derivation for one specialization can re-enter a mid-import
context for the other, triggering a cycle warning.
2026-05-05 15:54:45 -07:00
Egor Zhdan 122bd990fd [cxx-interop] Do not crash on references to global non-copyable non-movable variables
C++ types that are non-copyable and non-movable are not imported into Swift.

Variables of such types are marked as invalid in Swift by ClangImporter, however, compilation still proceeds without emitting any errors, unless the user attempts to use the problematic type from Swift. This differs from pure-Swift where having invalid decls are not allowed.

This adjusts the typechecker to account for the fact that a declaration coming from Clang might be invalid and not diagnosed at the same time. Instead of crashing in SILGen, let's emit a typechecking error.

rdar://137880350
2026-05-05 16:36:18 +01:00
Gábor Horváth 1b00845e5a Merge pull request #88782 from Xazax-hun/fix-asan-failure
[cxx-interop] Fix a test failure on an ASAN bot
2026-05-01 22:11:35 +01:00
Mads Odgaard 66b5fdc03d Add support for SwiftImportAs: opaque_pointer (#88714)
Adds support for making pointers to C structs always import as OpaquePointer through API notes. This is needed to bring down the Android SDK to API level 23, since FILE is imported differently on API 23 vs 24.
2026-05-01 22:45:47 +05:30
Gabor Horvath e46041d086 [cxx-interop] Fix a test failure on an ASAN bot
When the swift compiler is built with ASAN, the standard library is also
built with ASAN. As a result, test executables will also load the ASAN
runtime. The failing test tried to load TSAN but having both at the same
time is not supported. This resulted in a failed test. Let's just not
run this test for ASAN builds.

rdar://175373146
2026-05-01 17:15:11 +01:00
Justice Adams ebe6da03ca [ubuntu 26.04] disable use-std-set.swift (#88757) 2026-05-01 14:58:34 +05:30
Henrik G. Olsson fd0b3da192 update function-members-with-incomplete-types.swift
This test started failing after swiftifyImpl was made more lazy in
importing return types, resulting in availability attributes not being
added before AST printing. We should fix this, but that's out of scope
for this PR. The availability attributes are still added if the user
tries to call the function, as that forces the return type import.
2026-04-29 12:30:47 -07:00
Henrik G. Olsson 83b0babaa1 disable -eager-macro-checking in import-as-instance-method.swift
Macro expansions in this test trigger warnings, because the listed Span
availability is wider than that of the FRT parameters. This also
triggers an assert since these functions are imported with 2 overloads
that both get macro expansions with warnings in them: one for the free
function, and one for the function re-mapped to a method. Since -verify
disambiguates expansions solely on source location, this use case is not
supported. Having 2 expansions share a source location is okay under
-verify, as long as they don't both have diagnostics emitted.

These diagnostics are only emitted with -eager-macro-checking enabled,
since decls in clang modules are not normally typechecked by the Swift
compiler. The warning could be fixed by adding availability to imported
Swift function when they reference FRTs, but since it's not user
visible, silence it for now.
2026-04-29 12:30:47 -07:00
Henrik G. Olsson 3a2ec2b51b [Macros] add -eager-macro-checking frontend option
Normally macro expansions are typechecked through the normal traversal
of the AST. This does not happen with macro expansions in clang modules
however, since the clang module is not traversed by the typechecker.
This is not too problematic, since all the code in clang modules is
generated by the toolchain, so incorrect code would only occur when
there's a bug in the toolchain. Catching these bugs would be a lot
easier if we actually ran the typechecker during testing. We don't want
to do this normally, as it breaks the laziness of the typechecking, so
this adds -eager-macro-checking to specifically enable this behavior.
It's intended for testing purposes only.
2026-04-29 12:30:47 -07:00
Henrik G. Olsson db1815a66b [Swiftify] check std::span element type for templates
Even if the std::span template itself might be stripped from the safe
wrapper signature, we still need to check for templates in the element
type.
2026-04-29 12:30:46 -07:00
Henrik G. Olsson 46bea82650 [CxxInterop] remove unnecessary conditional include (NFC)
These tests never use bridging headers or define BRIDGING_HEADER, so
there's not reason for them to check for this define.
2026-04-29 12:30:46 -07:00
Henrik G. Olsson 7bbaeebba1 [Swiftify] enable safe wrappers for std::span without typedef
We previously prevented generating safe wrappers on functions with
template instantiations in the signature, unless those instantiations
were hidden behind a typedef. This is because those template
instantiations cannot be expressed in Swift syntax, so the macro
would expand to a function with invalid syntax.

For the std::span parameter itself however, that type is replaced with a
Swift Span in the safe wrapper, so it's not an issue in the type
signature. We would previously use the std::span type name from the
original signature to cast from Span to std::span, which does not work
with the raw std::span type. This uses `.init` instead when this is the
case, which avoids spelling out the type. When possible, we still use
the concrete type name, for clarity and by providing information we have
accessible the typechecker needs to do less work.

Resolves #86339

rdar://167712240
2026-04-29 12:30:44 -07:00
Henrik G. Olsson f674d72f95 Merge pull request #88620 from hnrklssn/swiftify-lifetimebound-implicit-this
[Swiftify] limit lifetimebound on implicit "this" to experimental mode
2026-04-28 16:57:47 -07:00
Mishal Shah 77a92daeed Merge pull request #88692 from swiftlang/jadams/disable-use-std-set-ubi10
[ubi10] disable use-std-set.swift - rdar://87654514
2026-04-28 08:50:19 -07:00
Gabor Horvath 752f1d4867 [cxx-interop] Deserialization failure with namespaces
Namespaces are imported into the __ObjC module. As a result, when a
Swift module is serialized, it refers to the declarations in the __ObjC
namespace. However, during deserialization, these declarations are found
in their original C++ module. The first failure triggered a recovery
that populated the __ObjC module so subsequent lookups succeed.

This PR makes sure we do not consider a declaration from the __ObjC
module that was found in a different clang module problematic.

Fixes #82318
2026-04-28 16:00:32 +01:00
Susana Monteiro 2781a1bd5d Merge pull request #88586 from susmonteiro/susmonteiro/empty-bases-unreferenceable-storage
[cxx-interop] C++ types with empty bases have referenceable storage
2026-04-28 15:53:10 +01:00