Commit Graph

2609 Commits

Author SHA1 Message Date
Egor Zhdan
cc23c32058 [cxx-interop] Avoid trying to instantiate copy constructor of std::optional<NonCopyable>
In libc++, `std::optional` inherits from several mixin base types. Some of those base types do not declare a deleted copy constructor for non-copyable value types, which works fine because clients are not supposed to use those base types directly from C++.

Swift, however, imports all of the transitive base types when importing a C++ type. As part of this, ClangImporter will attempt to instantiate e.g. `std::__optional_copy_assign_base<NonCopyable>`, and will fail with a hard error while doing so.

rdar://152718041
2025-10-10 17:46:22 +01:00
Gabor Horvath
4af3992bb4 [cxx-interop] Restrict the uses of anonymous types
Make sure they are excluded from the reflection metadata (although in
the future we want to make sure indirect fields are included). Make sure
the users cannot refer to the anonymous field, only its members.
2025-10-10 17:46:22 +01:00
Gabor Horvath
0c909a8395 [cxx-interop] Basic support for anonymous structs with non-copyable fields
Anonymous structs cannot be copied or moved, these operations only can
happen to their enclosing non-anonymous types. Stop trying to emit
special member functions and value witness tables for these structs.
2025-09-04 15:20:17 +01:00
susmonteiro
1bfa16a419 [cxx-interop] Prevent crash when importing a std::optional of nonescapable 2025-09-01 17:50:49 +01:00
fahadnayyar
720406fdd6 Diagnose unannotated C++ APIs returning SWIFT_SHARED_REFERENCE at Swift call sites (#83025)
This patch improves the warning for C++ APIs returning
`SWIFT_SHARED_REFERENCE` types but not annotated with either
`SWIFT_RETURNS_RETAINED` or `SWIFT_RETURNS_UNRETAINED` in the following
ways:

1. The warning for missing `SWIFT_RETURNS_(UN)RETAINED` annotations is
now emitted on Swift use sites, rather than while importing the API
(func/method decls).
- This logic is now implemented as a Misl Diagnostic in function
`diagnoseCxxFunctionCalls` in file lib/Sema/MiscDiagnostics.cpp.
- The warning is now triggered only when the API is actually used, which
reduces noise in large C++ headers.
- These warnings are still gated behind experimental-feature-flag `WarnUnannotatedReturnOfCxxFrt`

rdar://150800115
2025-08-25 14:44:25 -07:00
Egor Zhdan
d89333f85c Merge pull request #83825 from egorzhdan/egorzhdan/constexpr-in-namespace
[cxx-interop] Import constexpr globals in a namespace
2025-08-21 12:19:21 +01:00
Egor Zhdan
7c9bd160c1 Merge pull request #83813 from egorzhdan/egorzhdan/partial-spec-var
[cxx-interop] Do not import partial specializations of variables
2025-08-20 15:55:04 +01:00
Egor Zhdan
6bdd44e78e [cxx-interop] Import constexpr globals in a namespace
rdar://150756970 / resolves https://github.com/swiftlang/swift/issues/81327
2025-08-20 12:57:58 +01:00
Egor Zhdan
77975524be [cxx-interop] Do not import partial specializations of variables
They do not get imported correctly and trigger linker errors.

rdar://149232900 / resolves https://github.com/swiftlang/swift/issues/80802
2025-08-19 18:41:07 +01:00
Kuba (Brecka) Mracek
4ca136a9b9 Merge pull request #83791 from kubamracek/const-objcbool
[ClangImporter] Skip importing values for ObjCBool declarations
2025-08-19 07:37:19 -07:00
Egor Zhdan
5a6219cbf7 Merge pull request #83793 from egorzhdan/egorzhdan/c-frt-validation
[cxx-interop] Validate C foreign reference types
2025-08-19 15:05:35 +01:00
Egor Zhdan
c73f528b82 [cxx-interop] Validate C foreign reference types
Swift validates the retain/release operations for foreign reference types to check for obvious errors, e.g. a wrong parameter type or return type.

That logic was only running for C++ foreign reference types. This patch enables it for C foreign reference types as well.

rdar://158609723
2025-08-18 19:18:18 +01:00
Kuba Mracek
7b0eff8616 [ClangImporter] Skip importing values for ObjCBool declarations 2025-08-18 10:16:51 -07:00
Gábor Horváth
d2691dcb61 Merge pull request #83370 from Xazax-hun/addressable-param-copy-reapply
Reapply [cxx-interop] Avoid copies when accessing pointee
2025-08-18 16:41:27 +01:00
Egor Zhdan
d8edd86cfb Merge pull request #83753 from egorzhdan/egorzhdan/retain-release-unsigned
[cxx-interop] Allow retain/release operations to return an unsigned integer
2025-08-18 11:52:33 +01:00
Gabor Horvath
66d15a1813 Reapply [cxx-interop] Avoid copies when accessing pointee
After #83289 and #82879 landed we should no longer get deserialization
failures and this feature is no longer behind a flag. This patch also
changes how we query if a function's return value depends on self.
Previously, we queried the lifetime dependencies from the Swift
declaration. Unfortunately, this is problematic as we might have not
finished fully importing the types in the function signature just yet
and the compiler might end up populating the conformance tables
prematurely. To work this around, I store functions with self-dependent
return values where lifetimes are computed in the importer for later
use.

The PR also adds a test to make sure the addressable dependency feature
will not result in deserialization errors.

rdar://155319311&154213694&112690482&128293252
2025-08-18 10:51:43 +01:00
Gábor Horváth
16fbfec493 Merge pull request #83754 from Xazax-hun/reenable-addressable-params 2025-08-16 09:17:46 +01:00
Egor Zhdan
f96a5e5b2b Merge pull request #83589 from egorzhdan/egorzhdan/reland-extern-namespace
Revert "Revert "[cxx-interop] Import decls in extern blocks within namespaces""
2025-08-15 18:30:51 +01:00
Egor Zhdan
3887ed0637 [cxx-interop] Allow retain/release operations to return an unsigned integer
Release/retain functions for C++ foreign reference types might return a reference count as an integer value.

Swift previously emitted an error for such functions, saying that the retain/release functions need to return void or a reference to the value.

rdar://157853183
2025-08-15 17:30:53 +01:00
Gabor Horvath
2a9f6f89e3 Revert "Merge pull request #83483 from Xazax-hun/turn-off-addressable"
Afteri #83712 landed, let's make another try enabling addressable
parameters by default.

This reverts commit 61d60eb6ad, reversing
changes made to 670f69eadc.
2025-08-15 15:28:20 +01:00
Gabor Horvath
f6d5e50f89 [cxx-interop] Do not define inherited copy/move operations
When inheriting constructors, we define the inherited ctors in the
derived class. We should not do that for copy and move operations as
these operations can never be invoked (the implicit/defined/deleted ctor
in the derived class always takes precedence). This fixes an issue where
the lifetime parameters are not getting inferred for these spurious
constructor definitions. This should also save us from doing some
redundant work in the importer. Fixes #82183.

rdar://153081347
2025-08-13 15:34:00 +01:00
Egor Zhdan
ab5fc57f66 Merge pull request #83568 from egorzhdan/egorzhdan/nsfunctionkeys-workaround
[cxx-interop] Allow old spelling of AppKit constants
2025-08-12 15:57:27 +01:00
Susana Monteiro
d237e2ea41 Merge pull request #83543 from susmonteiro/susmonteiro/overrides-of-pure-virtual-methods
[cxx-interop] Support overriding pure virtual methods from value types
2025-08-07 16:52:25 +01:00
Egor Zhdan
a5b4975204 Revert "Revert "[cxx-interop] Import decls in extern blocks within namespaces""
This reverts commit 8f60840551.
2025-08-07 15:03:08 +01:00
Allan Shortlidge
8f60840551 Revert "[cxx-interop] Import decls in extern blocks within namespaces"
This reverts commit 844787fddb.
2025-08-06 21:50:55 -07:00
susmonteiro
5dd038fcc0 [cxx-interop] Support overriding pure virtual methods from value types 2025-08-06 16:46:33 +01:00
Egor Zhdan
37c418275c [cxx-interop] Allow old spelling of AppKit constants
AppKit defines certain constants in Objective-C, and then renames them into different constants in Swift, e.g. `NSUpArrowFunctionKey` is renamed into `NSEvent.SpecialKey.upArrow.rawValue`.

In addition to that, AppKit also re-defines these constants in pure-Swift, which isn't the intended mechanism for renaming such constants.

Prior to https://github.com/llvm/llvm-project/pull/145947, Clang was silently dropping the `SwiftName` API Notes attributes on these constants due to a bug in the name validation mechanism. Clients of AppKit relied on that behavior and continued to use the old spelling in Swift. To preserve source compatibility and avoid a deprecation error, let's continue dropping the `SwiftName` attribute on select constants from AppKit.

rdar://157485334
2025-08-06 16:28:15 +01:00
Egor Zhdan
4adccbdd90 Merge pull request #83516 from egorzhdan/egorzhdan/extern-within-namespace
[cxx-interop] Import decls in extern blocks within namespaces
2025-08-06 12:27:45 +01:00
Gábor Horváth
035ede372d Merge pull request #83487 from Xazax-hun/multiple-param-subscript 2025-08-06 06:05:41 +01:00
Gabor Horvath
9b0acdb885 [cxx-interop] Diagnose missing type witness instead of crashing
rdar://157473792
2025-08-05 16:29:45 +01:00
Gabor Horvath
e4a120b63c [cxx-interop] Support importing subscript operators with multiple params
Previously, the compiler would crash on them.

rdar://133541125
2025-08-05 15:27:53 +01:00
Egor Zhdan
844787fddb [cxx-interop] Import decls in extern blocks within namespaces
This teaches ClangImporter to import C++ decls that are declared within `extern "C" { ... }`/`extern "C++" { ... }` blocks which are nested in namespaces.

rdar://139067788
2025-08-04 19:42:43 +01:00
Gábor Horváth
330326a227 Merge pull request #83461 from Xazax-hun/default-associated-type
[cxx-interop] Support conforming to protocols with default assoc types
2025-08-04 14:58:06 +01:00
Gabor Horvath
a97d3304b6 [cxx-interop] Move back addressability behind a flag
There was another crash found by the bootstrap build bots.

This reverts commit 37ae8a22f4, reversing
changes made to 6c8da764e2.
2025-08-01 16:24:32 +01:00
Gábor Horváth
37ae8a22f4 Merge pull request #83430 from Xazax-hun/revert-addressable-flag
Revert "[cxx-interop] Move addressable parameters in interop behind a flag"
2025-08-01 08:37:25 +01:00
Gabor Horvath
922c30386d [cxx-interop] Support conforming to protocols with default assoc types
In case the type does not define a type alias with the same name fall
back to the default type of the associated type in the protocol.
Previously, the compiler crashed.

Unfortunately, we are still crashing when we do not find the right
conformance. In a follow-up PR I plan to add more graceful handling of
the case when the lookup fails.

rdar://154098495
2025-07-31 16:17:56 +01:00
Anthony Latsis
bbd84429a0 Merge pull request #83400 from swiftlang/jepa-main
Address `llvm::PointerUnion::{is,get}` deprecations
2025-07-30 17:19:58 +01:00
Gabor Horvath
254f72fdb4 Revert "[cxx-interop] Move addressable parameters in interop behind a flag"
This reverts commit f270e36aa5.
2025-07-30 15:24:45 +01:00
Egor Zhdan
9bc16e3dc0 Merge pull request #83402 from egorzhdan/egorzhdan/honor-non-copyable-attr
[cxx-interop] Avoid trying to instantiate copy constructors of explicitly non-copyable structs
2025-07-30 12:16:54 +01:00
Doug Gregor
9194fa0ab7 Merge pull request #83364 from DougGregor/noncopyable-imported-c-struct-destroy
[Clang importer] Allow noncopyable C structs to define "destroy" operation
2025-07-29 18:16:05 -07:00
Egor Zhdan
a07da0fe44 [cxx-interop] Avoid trying to instantiate copy constructors of explicitly non-copyable structs
If a C++ struct is annotated with `__attribute__((swift_attr("~Copyable")))`, Swift should not try to instantiate the copy constructor of the struct. This provides an escape hatch for C++ types that are designed to be non-copyable, but do not explicitly define a deleted copy constructor, and instead trigger complex template instantiation failures when a copy is attempted.

rdar://157034491
2025-07-29 19:03:04 +01:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Gabor Horvath
f270e36aa5 [cxx-interop] Move addressable parameters in interop behind a flag
This unblocks the CI while we figure out some SILGen issues. We cannot
reuse AddressableParameters because that is already used in some
projects and consuming modules where the AddressableParameters flag was not
propagated to results in deserialization errors.
2025-07-28 23:29:22 +01:00
Doug Gregor
63135e8929 [Clang importer] Diagnose various issues with the new "destroy:"
When we cannot respect the "destroy:" annotation, mark the type as
deprecated with a message thst says why there is a problem. There are
various potential problems:

* Multiple conflicting destroy functions
* Destroy functions that don't meet the pattern
* Type isn't imported as a move-only type
* Type has a non-trivial destructor (in C++)
2025-07-28 13:52:50 -07:00
Doug Gregor
6ba560fb4b [Clang importer] Allow noncopyable C structs to define "destroy" operation
A C struct can be imported as noncopyable, but C doesn't have
destructors, so there is no way to provide user-defined logic to
perform the destruction. Introduce a new swift_attr that applies to
imported noncopyable types and which provides such a "destroy"
operation. It can be used like this:

    typedef struct __attribute__((swift_attr("~Copyable")))
                   __attribute__((swift_attr("destroy:wgpuAdapterInfoFreeMembers")))
      WGPUAdapterInfo { /*...*/ } WGPUAdapterInfo;

    void wgpuAdapterInfoFreeMembers(WGPUAdapterInfo adapterInfo);

This will bring the WGPUAdapterInfo struct in as a noncopyable type
that will be cleaned up by calling wgpuAdapterInfoFreeMembers once it
is no longer in use.

Implements rdar://156889370.
2025-07-28 00:02:31 -07:00
Allan Shortlidge
c4e242e7db Merge pull request #83349 from tshortli/check-custom-domain-availability-for-conformances
Sema: Check custom domain availability during conformance checking
2025-07-26 11:54:17 -07:00
Gabor Horvath
9525aeec18 [cxx-interop] Enable addressable-self unconditionally
This was behind a feature flag. Unfortunately, this flag is viral, if
the module we consume and the consuming module had inconsistent settings
that could lead to deserialization errors. To avoid this, we need to
move this out of the flag and apply the attribute unconditionally. This
PR moves addressable-self out of the experimental flag and addresses a
couple of the fallouts:
* This attribute should not be applied to types with reference semantics
  like foreign reference types or Obj-C classes.
* There was a SILGen assertion failure which is solved by pealing off
  the @lvalue specifier from the type behind a load expression.

This fixes part of rdar://155971658
2025-07-26 08:37:03 +01:00
Allan Shortlidge
aa199aaf81 Correct misspelling of "availability" in a few places. 2025-07-25 21:30:53 -07:00
John Hui
9b95242ac4 [cxx-interop] Import const-qualified indirect fields with private setters (#83219)
We already make imported computed properties' setters private in
VisitFieldDecl(), but not in VisitIndirectFieldDecl(), which handles
things like the members of an anonymous union in another struct.
2025-07-24 19:04:58 -07:00
Artem Chikin
b60bc39ee5 [APINotes] Add support for handling Clang modules carrying all versions of APINotes
Controlled from Swift with '-version-independent-apinotes', which, for the underlying Clang invocation enables '-fswift-version-independent-apinotes', results in PCMs which aggregate all versioned APINotes wrapped in a 'SwiftVersionedAttr', with the intent to have the client pick and apply only those that match its current Swift version, discarding the rest.

This change introduces the configuration flags for this mode as well as the corresponding logic at the beginning of `importDeclImpl` to canonicalize versioned attributes, i.e. select the appropriate attributes for the current target and discard the rest.
2025-07-24 09:08:31 -07:00