Commit Graph

2735 Commits

Author SHA1 Message Date
John Hui
d5b3079567 Merge pull request #85379 from j-hui/dont-import-template-type-arguments-round-2
[cxx-interop] Make ClangDeclExplicitSafety request non-recursive
[cxx-interop] Do not import template type arguments
[cxx-interop] Check template argument safety in ClangDeclExplicitSafety
2025-11-11 13:44:07 -08:00
Anthony Latsis
bda6edb85c AST: Rename GenericContext::isGeneric to hasGenericParamList
`isGeneric` is a misleading name because this method checks for the
existence of a `GenericParamList`, which is not implied by genericity.
2025-11-11 15:55:16 +00:00
Pavel Yaskevich
5f91e49c9c Merge pull request #85105 from xedin/rdar-140928937
[AST/Sema] Allow `Sendable` suppression on Objective-C class declarations
2025-11-07 15:09:35 -08:00
John Hui
3255ffbd74 [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.

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-11-07 00:12:44 -08:00
susmonteiro
2c9bb7a6eb Revert "[cxx-interop] Implicitly defined copy constructors"
This reverts commit 1c5bbe0290.
2025-11-05 10:42:05 +00:00
susmonteiro
dd921adc05 Revert "[cxx-interop] Implicitly defined move constructors"
This reverts commit c3cd9939c1.
2025-11-05 10:41:38 +00:00
Kavon Farvardin
758e67f82d Merge pull request #85296 from j-hui/revert-template-arg-import
[cxx-interop] Revert changes to template argument import/safety check
2025-11-04 18:25:56 -08:00
John Hui
ae934749cb Revert "[cxx-interop] Do not import template type arguments"
This reverts commit d5473feb5a.
2025-11-03 18:03:49 -08:00
John Hui
4389cc9bea Revert "[cxx-interop] Mark class templates with unsafe type arguments as unsafe"
This reverts commit afaa499e3a.
2025-11-03 18:03:16 -08:00
John Hui
6607ae9c56 Revert "[cxx-interop] Check template argument safety in ClangDeclExplicitSafety"
This reverts commit d2632d21af.
2025-11-03 14:37:05 -08:00
susmonteiro
c3cd9939c1 [cxx-interop] Implicitly defined move constructors 2025-11-03 16:16:06 +00:00
susmonteiro
1c5bbe0290 [cxx-interop] Implicitly defined copy constructors 2025-10-30 14:17:19 +00:00
John Hui
90c0509b16 Merge pull request #85095 from j-hui/template-arg-safety-in-req 2025-10-27 11:40:09 -07:00
Pavel Yaskevich
65599ce1f1 [AST/Sema] Allow Sendable suppression on Objective-C class declarations
Expressed as `__swift_attr__("~Sendable")` this acts like `: ~Sendable`
on Swift type declarations and supersedes `@_nonSendable(_assumed)`.

Resolves: rdar://140928937
2025-10-24 20:36:24 +09:00
Henrik G. Olsson
6d8d09ee03 [ClangImporter] move swiftify to SwiftifyDecl.cpp (NFC)
This code doesn't interact that much with the rest of ClangImporter, and
will continue to grow. Keeping it in a separate file makes it easier to
navigate.
2025-10-23 20:37:24 -07:00
Henrik G. Olsson
0a8314b9c4 convert swiftifyImpl to a free function
This will make it more straightforward to later templatize it.
2025-10-23 20:37:15 -07:00
Henrik G. Olsson
4ab1d5db5e [ClangImporter] refactor swiftify before swiftifyProtocol (NFC)
This is a pure refactor to make it easier to follow the upcoming
addition of swiftifyProtocol.
2025-10-23 19:31:03 -07:00
John Hui
d2632d21af [cxx-interop] Check template argument safety in ClangDeclExplicitSafety
Checking this upon import was overlooking annotations that explicitly
mark a type as safe (or unsafe). Instead, we consolidate this logic in
the ClangDeclExplicitSafety request.

rdar://163196609
2025-10-23 13:05:57 -07:00
Pavel Yaskevich
2c1329e861 [ClangImporter] Add "suppressed" bit to SynthesizedProtocolAttr
Makes it possible to support `~Sendable` and potentially other
suppressible conformances.
2025-10-23 23:56:05 +09:00
Gabor Horvath
2b7dcbac60 [cxx-interop] Do not generate Span wrappers that depend on a reference type
We currently do not have sound enforcement of exclusivity in those cases
regardless of mutability. Let's not generate these methods for now until
we figure out how to make them safer.

rdar://163068618
2025-10-21 08:10:33 -07:00
Mads Odgaard
c92e5b47f3 Merge pull request #84574 from madsodgaard/android-availability 2025-10-20 10:40:37 +09:00
Hamish Knight
73710e3eef [AST] Introduce Decl::addAttribute
Introduce a convenience entrypoint that also calls `attachToDecl` on
the attribute, and migrate all existing uses of `getAttrs().add` onto
it.
2025-10-16 11:21:54 +01:00
Hamish Knight
0358e1eadd [AST] Consolidate attribute attachment logic
Introduce `DeclAttribute::attachToDecl` which is the now the main
entry point for associating an attribute with a decl. Different
attributes can implement `attachToDeclImpl` to add their custom logic.
Move DifferentiableAttr, DerivativeAttr, CustomAttr, and ABIAttr over
to this new logic.
2025-10-16 11:21:54 +01:00
John Hui
dc4ff24d5d Merge pull request #84866 from j-hui/do-not-import-template-type-arguments
[cxx-interop] Do not import template type arguments
2025-10-15 18:57:58 -07:00
Egor Zhdan
b6dda90e87 Merge pull request #84881 from egorzhdan/egorzhdan/remove-opt-out-flag
[cxx-interop] Remove opt-out flag for constructors of foreign reference types
2025-10-15 12:17:23 +01:00
John Hui
afaa499e3a [cxx-interop] Mark class templates with unsafe type arguments as unsafe
Doing so preserves ClangImporter's behavior first introduced in
f12b48aa86, but do so without relying on
importing the type argument (since that can lead to template
over-instantiation).

This patch also promotes the logic of hasUnsafeType() to a standalone
SimpleRequest, to provide a QualType-typed entry point for evaluating
the safety of a Clang entity.
2025-10-14 15:13:24 -07:00
Egor Zhdan
4e6e462c02 [cxx-interop] Remove opt-out flag for constructors of foreign reference types
The opt-out flag was never used or tested, let's remove it.

rdar://148285972
2025-10-14 18:11:12 +01:00
Henrik G. Olsson
89b31cfcf9 Merge pull request #84872 from hnrklssn/disable-safe-wrappers-in-stdlib
[Swiftify] Always skip safe wrappers when building the stdlib
2025-10-14 09:36:20 -07:00
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