Commit Graph

2682 Commits

Author SHA1 Message Date
swift-ci
80f659cce2 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-19 12:18:05 -07:00
fahadnayyar
7b8b33eda0 [cxx-interop] Allow SWIFT_RETURNS_(UN)RETAINED on template class member functions returning T* (#84375)
When importing C++ template classes like`Ref<T>` that have methods
returning `T*`, we face the following situation when `T` is a
`SWIFT_SHARED_REFERENCE` type:

1. Without `SWIFT_RETURNS_(UN)RETAINED` annotation: Swift compiler would
emit a warning (currently under experimental-feature flag
`WarnUnannotatedReturnOfCxxFrt`) _"cannot infer the ownership of the
returned value" when T is a SWIFT_SHARED_REFERENCE type_
2. With annotation: Compiler rejects it with this error: _"cannot be
annotated... not returning a SWIFT_SHARED_REFERENCE type"_

This affects WebGPU's smart pointer types (`WTF::Ref<T>,
WTF::RefPtr<T>`) and similar patterns in other C++ codebases.

In this patch I am fixing the logic for diagnostic
`returns_retained_or_returns_unretained_for_non_cxx_frt_values`. I'm
also making it a warning instead of an error to minimize the risk, as
this diagnostic has been a hindrance to the adoption of these
annotations in real codebases when templated functions and types are
involved. (Refer to
[PR-78968](https://github.com/swiftlang/swift/pull/78968))

rdar://160862498
2025-09-19 12:09:05 -07:00
swift-ci
b08d731e34 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-16 19:16:11 -07:00
susmonteiro
2640c71ec0 [cxx-interop] Refactor copyability out of CxxRecordSemantics 2025-09-16 14:40:16 +01:00
swift-ci
503672fd94 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-15 02:12:55 -07:00
Egor Zhdan
99e9ed8213 Merge pull request #84274 from egorzhdan/egorzhdan/rm-dead-partial-var
[cxx-interop] Remove dead logic in const import
2025-09-15 10:06:02 +01:00
Egor Zhdan
fba1f340ca [cxx-interop] Remove dead logic in const import
We no longer try to import partial specializations of variables after 77975524.
2025-09-13 17:12:14 +01:00
swift-ci
edf0c5fffd Merge remote-tracking branch 'origin/main' into rebranch 2025-09-12 21:14:20 -07:00
Henrik G. Olsson
8e802be2b8 Merge pull request #81663 from hnrklssn/swiftify-debug-logging
[Swiftify] add debug logs for safe interop
2025-09-12 18:40:55 -07:00
Henrik G. Olsson
ce6664f943 [Swiftify] add debug logs 2025-09-11 18:46:13 -07:00
swift-ci
2a761f83a7 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-10 12:56:44 -07:00
Gábor Horváth
4fabc61c82 Merge pull request #84199 from Xazax-hun/no-reflection-anon-types
[cxx-interop] Restrict the uses of anonymous types
2025-09-10 20:40:03 +01:00
swift-ci
6897a310c7 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-10 10:36:55 -07:00
Egor Zhdan
30fc84cbdf Merge pull request #84103 from egorzhdan/egorzhdan/std-optional-noncopyable
[cxx-interop] Avoid trying to instantiate copy constructor of `std::optional<NonCopyable>`
2025-09-10 18:36:10 +01:00
Egor Zhdan
124055a51a [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-09-10 12:33:38 +01:00
Gabor Horvath
1cde254dbe [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-09-10 12:12:14 +01:00
swift-ci
1e13c47707 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-05 01:16:18 -07: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
swift-ci
f22cb818d1 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-02 03:16:50 -07:00
susmonteiro
1bfa16a419 [cxx-interop] Prevent crash when importing a std::optional of nonescapable 2025-09-01 17:50:49 +01:00
swift-ci
94f8b0d0e3 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-25 17:03:36 -07: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
swift-ci
b250e19174 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-21 05:22:19 -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
swift-ci
ef5ceba464 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-20 07:57:21 -07: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
Anthony Latsis
44c480e83b Mnually merge main -> rebranch
Conflicts:
* `lib/ClangImporter/ClangModuleDependencyScanner.cpp`:
  https://github.com/swiftlang/swift/commit/5e09125
*	`utils/build.ps1`: https://github.com/swiftlang/swift/pull/83523
2025-08-19 21:51:34 +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
swift-ci
fd22790987 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-18 05:56:12 -07: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
swift-ci
ab9176fa4b Merge remote-tracking branch 'origin/main' into rebranch 2025-08-16 02:24:41 -07:00
Gábor Horváth
16fbfec493 Merge pull request #83754 from Xazax-hun/reenable-addressable-params 2025-08-16 09:17:46 +01:00
swift-ci
c0223fb423 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-15 12:15:24 -07: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
swift-ci
e93ab979eb Merge remote-tracking branch 'origin/main' into rebranch 2025-08-14 05:15:47 -07: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
swift-ci
690e762311 Merge remote-tracking branch 'origin/main' into rebranch 2025-08-12 08:15:48 -07: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
swift-ci
5c840b6e1a Merge remote-tracking branch 'origin/main' into rebranch 2025-08-07 18:54:54 -07: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