Commit Graph

2757 Commits

Author SHA1 Message Date
John Hui
bb5bb97958 [cxx-interop] Move incomplete template specialization check
We were only previously doing this check when we had a typedef,
because that is the scenario where we encountered this issue.

This patch moves the check closer to where we would actually instantiate
the template, so that these cases can be stopped in more situations.
2025-12-11 18:53:47 -08:00
Susana Monteiro
91d1d2e06f Merge pull request #85793 from susmonteiro/susmonteiro/diagnose-swift-attrs
[cxx-interop] Diagnose invalid swift attributes
2025-12-11 11:06:41 +00:00
susmonteiro
fb5bc7282f [cxx-interop] Diagnose invalid copyability, escapability and mutability attributes
Fixes: https://github.com/swiftlang/swift/issues/84559
2025-12-10 17:26:14 +00:00
Egor Zhdan
ad56e061af Merge pull request #85066 from egorzhdan/egorzhdan/std-function-context
[cxx-interop] Allow initializing `std::function` from Swift capturing closures
2025-12-10 13:35:34 +00:00
Gábor Horváth
d97fa92a33 Merge pull request #85650 from Xazax-hun/refcounted-ptr-conversions
[cxx-interop] Introduce SWIFT_REFCOUNTED_PTR
2025-12-09 17:01:08 +00:00
Hamish Knight
45683f6561 [Sema] Exclude private initialized vars from memberwise initializer
Exclude properties with initial values from the memberwise initializer
if they are less accessible than the most accessible property, up to
`internal`. Introduce a compatibility overload that continues to
include the same properties as before until the next language mode.

This is gated behind the `ExcludePrivateFromMemberwiseInit` feature.

rdar://122416579
2025-12-08 23:22:35 +00:00
Egor Zhdan
21440b3fa9 Merge pull request #85867 from egorzhdan/egorzhdan/iterator-invalidation
[cxx-interop] Fix iterator invalidation
2025-12-06 18:31:05 +00:00
John Hui
6c80c68075 [ClangImporter] Do not import function decl if already imported via type signature (#85480)
This is a small incremental patch that is designed to address the issue
discovered by 9aa3c5d17c, which is that we
are importing various Decls multiple times.

rdar://164616956
2025-12-05 17:18:58 -08:00
Henrik G. Olsson
4419ce5bbd Merge pull request #85528 from hnrklssn/diag-verify-fixes
[DiagnosticVerifier] improve output for expansions in clang attributes
2025-12-05 12:51:29 -08:00
Gabor Horvath
77187a04ab [cxx-interop] Introduce SWIFT_REFCOUNTED_PTR
This attribute introduces some conversions between the annotated smart
pointer type and the native swift reference type. In the future we plan
to introduce bridging so the smart pointer type can be hidden in the
signature and we can automatically convert it to the native Swift
reference type on the ABI boundaries.

This PR is using a new way to use swift_attr attributes. Instead of
doing the parsing in the clang importer it falls back to Swift's
attribute parsing and makes sure that the annotation has valid Swift
attribute syntax.

rdar://156521316
2025-12-05 18:19:10 +00:00
Egor Zhdan
3015c39cae [cxx-interop] Fix iterator invalidation
This fixes an assertion failure in ClangImporter that was blocking bootstrapping of the FreeBSD toolchain.

Iterators pointing into `Impl.GetterSetterMap[result]` were being invalidated inside of the for-each loop.

I was unable to minimize the reproducer, so this doesn't include a test case.

rdar://164518108
2025-12-05 17:36:35 +00:00
Egor Zhdan
7fc815e383 [cxx-interop] Allow initializing std::function from Swift capturing closures
This introduces support for converting a Swift closure that captures variables from its surrounding context into an instance of `std::function`, which is useful for working with C++ APIs that use callbacks.

Each instantiation of `std::function` gets a synthesized Swift constructor that takes a Swift closure. Unlike the previous implementation, the closure is _not_ marked as `@convention(c)`. The body of the constructor is created lazily.

Under the hood, the closure is bitcast to a pair of a function pointer and a context pointer, which are then wrapped in a C++ object, `__SwiftFunctionWrapper`, that manages the lifetime of the context object via calls to `swift_retain`/`swift_release` from the copy constructor and the destructor. The `__SwiftFunctionWrapper` class is templated, and is instantiated by ClangImporter.

rdar://133777029
2025-12-05 15:41:59 +00:00
Anthony Latsis
153dd02cd8 Merge pull request #85833 from swiftlang/jepa-main
[NFC] "SwiftVersion" → "LanguageMode" in `DiagnosticEngine::warnUntilSwiftVersion`, etc.
2025-12-05 09:34:30 +00:00
Anthony Latsis
88220a33c3 [NFC] "SwiftVersion" → "LanguageMode" in DiagnosticEngine::warnUntilSwiftVersion, etc. 2025-12-04 15:11:07 +00:00
susmonteiro
fbfcd4d241 [cxx-interop] Implicitly defined copy and move constructors 2025-12-02 09:15:37 +00:00
susmonteiro
791194b0ff Simplify CxxValueSemanticsKind 2025-11-20 18:43:04 +00:00
Henrik G. Olsson
703fc1c8df [ClangImporter] set zero-byte originalSourceRange for AttributeFromClang
This won't affect diagnostic printing, as the diagnostic engine checks
the GeneratedSourceInfoKind and stops expanding if it's an
AttributeFromClang kind. This helps the diagnostic verifier expand to
the original location in source code however.
2025-11-17 23:59:03 -08:00
John Hui
f830b1c665 [ClangImporter] Do not import enum when already imported via DeclContext (#85424)
If we try to import this in ObjC interop mode:

```objc
typedef CF_OPTIONS(uint32_t, MyFlags) {
  ...
} CF_SWIFT_NAME(MyCtx.Flags);

struct MyStruct {
  MyFlags flags;
  ...
} CF_SWIFT_NAME(MyCtx);
```

ClangImporter tries to import `MyCtx/MyStruct` before it imports
`MyFlags` (via `importDeclContextOf()`), which in turn tries to import
`MyFlags` again due to the `flags` field. The existing cycle-breaking
mechanism prevents us from looping infinitely, but leads us to import
two copies of the Swift `EnumDecl`, which can cause errors later during
CodeGen.

~~This patch adds an assertion to catch such issues earlier, and breaks
the cycle by checking the cache again.~~ This patch no longer does so
because that caused issues beyond the scope of this patch.

rdar://162317760
2025-11-17 13:18:59 -08:00
John Hui
b4075fc2a4 [cxx-interop] Refactor importFunctionDecl() (#85475)
This patch re-arranges the implementation of importFunctionDecl() to
delay side-effectful logic as late as possible, to avoid importing or
allocating parts of the AST that we do not need.
2025-11-13 17:41:30 -08:00
Henrik G. Olsson
f1d564ddfe Merge pull request #85123 from hnrklssn/swiftify-obj-c2
[ClangImporter] Attach _SwiftifyImportProtocol to imported protocols with bounds attributes
2025-11-13 12:02:17 -08:00
Slava Pestov
819738c83e AST: Rename mapTypeIntoContext() => mapTypeIntoEnvironment(), mapTypeOutOfContext() => mapTypeOutOfEnvironment() 2025-11-12 14:48:19 -05:00
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
Henrik G. Olsson
af819b9428 [ClangImporter] Attach _SwiftifyImportProtocol to imported protocols
...with bounds attributes

This creates safe overloads for any methods in the protocol annotated
with bounds information. Updates _SwiftifyImportProtocol to make the
added overloads in the protocol public.

rdar://144335990
2025-10-24 13:28:02 -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