Commit Graph

2595 Commits

Author SHA1 Message Date
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
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
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
Doug Gregor
49febcd468 Merge pull request #83252 from DougGregor/import-conforms-to-newtype
[Clang importer] Handle Swift conformances on typedefs and C types
2025-07-23 15:38:01 -07:00
Doug Gregor
858383c71d Merge pull request #83173 from DougGregor/shared-reference-of-incomplete
[Clang importer] Import incomplete SWIFT_SHARED_REFERENCE types
2025-07-23 13:11:56 -07:00
Kuba (Brecka) Mracek
407ef9a302 Merge pull request #83238 from kubamracek/const-cgfloat-typedef
[ClangImporter] Skip importing values for CGFloat typedefs on top of CGFloats direct
2025-07-23 10:43:24 -07:00
Gábor Horváth
d7bdbcb1c0 Merge pull request #83231 from Xazax-hun/addressable-params-copy-revert
Revert [cxx-interop] Avoid copies when accessing pointee
2025-07-23 12:31:14 +01:00
Doug Gregor
c5db5cced5 [Clang importer] Handle Swift conformances on typedefs and C types
The code for handling Swift conforms_to attributes was specific to
C++ record types. Generalize it to work on typedefs imported as
nominal types, also in C.

Fixes rdar://156290361.
2025-07-22 20:20:23 -07:00
Doug Gregor
72be0e0851 [Clang importer] Import incomplete SWIFT_SHARED_REFERENCE types
Normally, Swift cannot import an incomplete type. However, when we are
importing a SWIFT_SHARED_REFERENCE type, we're always dealing with
pointers to the type, and there is no need for the underlying type to
be complete. This permits a common pattern in C libraries where the
actual underlying storage is opaque and all APIs traffic in the
pointer, e.g.,

    typedef struct MyTypeImpl *MyType;
    void MyTypeRetain(MyType ptr);
    void MyTypeRelease(MyType ptr);

to use SWIFT_SHARED_REFERENCE to import such types as foreign
references, rather than as OpaquePointer.

Fixes rdar://155970441.
2025-07-22 20:20:23 -07:00
Kuba Mracek
6ec280d4e1 [ClangImporter] Skip importing values for CGFloat typedefs on top of CGFloats direct 2025-07-22 10:10:15 -07:00
Gabor Horvath
db4b4c8abe Revert "Merge pull request #82480 from swiftlang/gaborh/addressable_params_copy"
Unfortunately, addressable parameters are viral, the whole dependency
chain needs to be consistent otherwise we get deserialization errors
when loading a module. The solution is to universally enable addressable
parameters for C++ interop but there are some blockers at the moment
that need to be solved first. Temporarily revert these changes until
those blockers are resolved.

This reverts commit b00ff4568b, reversing
changes made to 396379ecbf.
2025-07-22 14:03:24 +01:00
Anthony Latsis
2920ea84d1 Address llvm::(Mutable)ArrayRef ctor deprecations
See:
- https://github.com/llvm/llvm-project/pull/146113
- https://github.com/llvm/llvm-project/pull/146011
2025-07-21 12:36:53 +01:00
Susana Monteiro
0337f7e64a Merge pull request #83067 from susmonteiro/rename-virtual-methods
[cxx-interop] Adding swift_name attributes to virtual methods overrides
2025-07-18 11:57:46 +01:00
susmonteiro
585ca5e2da [cxx-interop] Adding swift_name attributes to virtual methods overrides 2025-07-17 16:23:32 +01:00
fahadnayyar
a4eeae2a01 [cxx-interop] Re-enable warnings for unannotated C++ APIs returning SWIFT_SHARED_REFERENCE types under an experimental feature flag (#82488)
This patch re-enables diagnostics for unannotated C++ functions or
methods returning `SWIFT_SHARED_REFERENCE` types. These warnings now
fire only **once per source location**, even in the presence of multiple
template instantiations. This avoids diagnostic duplication that was a
key source of noise in the compilation of larger codebases.

These warnings were previously disabled starting in **Swift 6.2** via
[PR-#81411](https://github.com/swiftlang/swift/pull/81411) due to
concerns around false positives and excessive duplication in projects
adopting C++ interop. This patch addresses the duplication issue by
adding source-location-based caching, which ensures that a warning is
emitted only once per source location, even across template
instantiations with different types.

However, the false positive issue remains to be investigated and will be
addressed in a follow-up patch. Until that happens, the warnings are
gated behind a new experimental feature flag:
`WarnUnannotatedReturnOfCxxFrt`. This feature will be enabled by default
only after thorough qualification and testing on large C++ codebases.

rdar://154261051
2025-07-15 02:56:34 -07:00
Allan Shortlidge
3d722e1919 ClangImporter: Generate and call custom availability domain predicates.
When importing custom availability domains with dynamic predicates from Clang
modules, synthesize predicate functions for `if #available` queries and call
them when generating SIL.

Resolves rdar://138441312.
2025-07-10 08:15:01 -07:00
Egor Zhdan
569ca005e8 Merge pull request #82680 from swiftlang/egorzhdan/enable-c-frt
[cxx-interop] Enable foreign reference types in C interop
2025-07-07 19:42:06 +01:00