Commit Graph

2757 Commits

Author SHA1 Message Date
Gábor Horváth
90a5e49c2c Merge pull request #82725 from Xazax-hun/dependence-on-sharedobj-is-unsafe
[cxx-interop] Lifetime dependence on a class is unsafe
2025-10-14 15:12:15 +01:00
Hamish Knight
7fe655de7b Merge pull request #84854 from hamishknight/wrapping-paper
[NameLookup] Prefer property wrapper over attached macro on local var
2025-10-14 11:57:35 +01:00
Henrik G. Olsson
a8fec6f39b [Swiftify] Always skip safe wrappers when building the stdlib
_SwiftifyImport assumes types like Swift.Int, Swift.UnsafePointer<T> and
Swift.Span<T> are available. This is not the case when building the
stdlib itself. Disable safe interop in the stdlib to prevent errors.
This currently has no effect, but will when this feature is enabled by
default, which I have manually tested.
2025-10-13 19:11:57 -07:00
John Hui
d5473feb5a [cxx-interop] Do not import template type arguments
Prior to this patch, we eagerly imported template type arguments.
A consequence of doing so is that we over-instantiate (potentially
unused) type arguments, which causes unnecessary errors.

The only apparent use we have for these type arguments are to check
whether they are unsafe, so that we can mark the instantiated type as
unsafe as well... even if the instantiated type does not use its unsafe
template type argument at all.

Note that using un-instantiatable types in template type arguments is
supported in C++. The test case included in this patch validates this
behavior, for both missing member and incomplete type errors.

Note, also, that as of this patch, dumping the module interface of
CxxModule actually causes swift-ide-test to emit compiler errors, since
it tries to instantiate the invalid types MissingMember<Empty> and
IncompleteField<Incomplete>. However, these errors are simply swallowed
by swift-ide-test, so they should be harmless, though we should probably
get rid of them entirely in future work.

rdar://145238539
2025-10-13 17:03:05 -07:00
Hamish Knight
2b8a1cccfd [AST] Store owning Decl/DeclContext on CustomAttr
Introduce CustomAttrOwner that can store either a Decl for an
attached attribute, or a DeclContext for e.g a type or closure
attribute. Store this on CustomAttr such that we can query it from
the name lookup requests.
2025-10-13 13:37:29 +01:00
Henrik G. Olsson
ad866356bf Move registerStdSpanTypeMapping to SwiftifyInfoPrinter (NFC)
`registerStdSpanTypeMapping` used to be a lambda inside `swiftify`.
By moving it, along with the `typeMapping` state, inside
`SwiftifyInfoPrinter` we will simplify changes necessary to support
`_SwiftifyImportProtocol` in ClangImporter.
2025-10-11 14:51:20 -07:00
Gabor Horvath
f08868f95a [cxx-interop] Lifetime dependence on a class is unsafe
Swift cannot guarantee exclusivity of a class. On the other hand,
lifetimebound annotations on the C++ side do not guarantee exclusivity.
To resolve this issue, we ignore lifetime dependency annotations that
introduce exclusive dependence on classes and import functions with ignored
annotations as unsafe. Currently, Swift has no language feature to
support these scenarios but it might get new features in the future to
help people work around this problem.

rdar://153747746
2025-10-09 11:55:47 +01:00
Gábor Horváth
7159b12936 Merge pull request #83752 from Xazax-hun/fix-inline-namespaces
[cxx-interop] Support calling functions in inline namespaces
2025-10-08 18:41:38 +01:00
Gabor Horvath
0e56809834 [cxx-interop] Support calling functions in inline namespaces
The overload resolution generated a constraint that tried to bind
outer.inline_inner to outer. This constraint failed. This PR attempts to
recognize this scenario and make the constraint succeed.

rdar://158401346
2025-10-07 11:39:07 +01:00
Henrik G. Olsson
5519be8b7c Move span to conditional header in libstdc++ module map
`span` is not available in all versions of libstd++, so make it a
conditional header. Also adds other missing c++20 headers.

Fixing this triggered an assert when importing a constant initialized
`wchar_t` variable, so that is also fixed. The reason is that `wchar_t`
is mapped to `Unicode.Scalar`, which cannot be directly initialized by
integer literals in Swift, triggering an assert when looking up the
protocol conformance for `_ExpressibleByBuiltinIntegerLiteral`.

rdar://162074714
2025-10-06 16:02:35 -07:00
Henrik G. Olsson
ed4f058c01 Merge pull request #84507 from hnrklssn/swiftify-import-as-method
[Swiftify] Add support for free functions imported as instance methods
2025-10-04 00:40:00 -07:00
Henrik G. Olsson
663ba2efb4 [Swiftify] properly forward inout parameters using &
While we handled prepending & to MutableSpan parameters, regular
parameters that were unchanged during the transformation were not
checked for inout-ness.
2025-10-02 16:37:43 -07:00
Henrik G. Olsson
87642d499a [Swiftify] Add support for free functions imported as instance methods
This adds support for attaching safe interop wrappers to functions
explicitly imported as instance methods using swift_name.

rdar://156288883
2025-10-02 16:37:40 -07:00
swift-ci
b5a3eee645 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-30 10:55:17 -07:00
fahadnayyar
cc8f060555 [cxx-interop] Fix crash when importing C++ forward-declared template specializations in typedefs (#84186)
Swift crashes when importing C++ typedefs to forward-declared explicit
template specializations. In assert build we see this assertion failure
happening in clang:
`Assertion failed: (!T->isDependentType() && "should not see dependent
types here"), function getTypeInfoImpl, file TypeNodes.inc, line 79.`

  Example that crashes:
  ```cpp
  template <typename T> struct MyTemplate { T value; };
template <> struct MyTemplate<int>; // Forward-declared specialization
  typedef MyTemplate<int> MyIntTemplate;
  ```

In this patch, I propose detecting forward-declared explicit
specializations in typedef imports. Instead of crashing, these
specializations should be blocked from being imported with a diagnostic
similar to the forward declared (but not defined) structs.

rdar://147595723
2025-09-30 10:41:40 -07:00
swift-ci
75d80ab258 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-25 07:33:55 -07:00
Egor Zhdan
1ac8cbe61d [cxx-interop] Do not freeze when printing libc++ module interface
Follow-up to `fed7373f`.
2025-09-24 18:38:58 +01:00
swift-ci
65bba7d138 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-23 11:57:20 -07:00
Egor Zhdan
4ef848701f Merge pull request #84354 from egorzhdan/egorzhdan/revert-extern-c
Revert "[cxx-interop] Import decls in extern blocks within namespaces"
2025-09-23 19:46:12 +01:00
swift-ci
ef534479d5 Merge remote-tracking branch 'origin/main' into rebranch 2025-09-22 18:21:46 -07:00
Egor Zhdan
b250ef7be3 Merge pull request #83751 from egorzhdan/egorzhdan/many-specializations
[cxx-interop] Allow many specializations of a class template
2025-09-22 22:01:51 +01:00
Egor Zhdan
6b4c7fcd80 Merge pull request #84343 from egorzhdan/egorzhdan/retain-release-methods
[cxx-interop] Allow retain/release operations to be methods
2025-09-22 21:53:28 +01:00
Egor Zhdan
fed7373f30 [cxx-interop] Allow many specializations of a class template
An old piece of logic in ClangImporter was trying to limit the number of instantiations for each C++ class template to prevent long compile times. Unfortunately this started causing hard-to-reproduce deserialization errors on large projects which use many different instantiations of `std::vector` and `std::allocator`.

The instantiation limit was arbitrary, it serves no real purpose and causes issues. This change removes it.

rdar://158397914
2025-09-22 16:17:29 +01:00
Egor Zhdan
e78ce6165f [cxx-interop] Allow retain/release operations to be methods
Some foreign reference types such as IUnknown define retain/release operations as methods of the type.

Previously Swift only supported retain/release operations as standalone functions.

The syntax for member functions would be `SWIFT_SHARED_REFERENCE(.doRetain, .doRelease)`.

rdar://160696723
2025-09-22 12:01:17 +01:00
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
Egor Zhdan
c3a56fdc91 Revert "[cxx-interop] Import decls in extern blocks within namespaces"
This reverts commit 844787fd

Reverting because of a modularization issue in host libc++.

rdar://160536819
rdar://160536878
2025-09-17 19:17:14 +01: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