Commit Graph

617 Commits

Author SHA1 Message Date
Gábor Horváth
c33ce3f029 Merge pull request #82840 from Xazax-hun/unqualified-name-lookup-fix
[cxx-interop] Fix unqualified name lookup failure
2025-07-08 17:53:35 +01:00
Gabor Horvath
4de92656dd [cxx-interop] Fix unqualified name lookup failure
When MemberImportVisibility is enabled we failed to find certain base
methods from the extensions when said base methods are imported from
C++.

rdar://154887575
2025-07-08 12:41:01 +01:00
Joe Groff
8a817da6d8 Merge pull request #82707 from jckarter/afd-clang-imports
Lower imported C structs and unions as addressable-for-dependencies.
2025-07-07 13:23:49 -07:00
Joe Groff
78a5f358b4 Lower imported C structs and unions as addressable-for-dependencies.
C code is highly likely to want to use pointers as references between dependent
structs, and we would like to be able to readily map these to lifetime-dependent
Swift values. Making C types addressable-for-dependencies ensures that any function
producing a dependency on such a value receives a stable in-memory address for that
value, allowing borrows and inout accesses to always be representable as pointers.

rdar://153648393
2025-07-02 08:31:18 -07:00
susmonteiro
b3e228845b [cxx-interop] Allow virtual methods to be renamed with swift_name 2025-07-02 16:09:02 +01:00
Egor Zhdan
01719983dc Merge pull request #82627 from swiftlang/egorzhdan/void-begin-crash
[cxx-interop] Do not crash for `void begin()`
2025-07-01 17:35:16 +01:00
Gábor Horváth
bd3c3024af Merge pull request #82625 from swiftlang/gaborh/nonescapable-noncopyable
[cxx-interop] Add test for move-only non-escapable types
2025-07-01 15:36:00 +02:00
Gábor Horváth
b00ff4568b Merge pull request #82480 from swiftlang/gaborh/addressable_params_copy
[cxx-interop] Avoid copies when accessing pointee
2025-06-30 17:41:39 +02:00
Egor Zhdan
883ff18adb [cxx-interop] Do not crash for void begin()
When importing C++ methods, Swift always assumes that methods named `begin()` and `end()` are unsafe, since these methods commonly return iterator types that are inherently unsafe in Swift.

Some additional logic in Sema tries to diagnose usages of `.begin()` and `.end()` from Swift and suggest safe alternatives. That logic had a null pointer dereference bug.

rdar://153814676 / resolves https://github.com/swiftlang/swift/issues/82361
2025-06-30 16:26:56 +01:00
Gabor Horvath
8af85a37a5 [cxx-interop] Add test for move-only non-escapable types
rdar://153658277
2025-06-30 15:20:56 +01:00
finagolfin
55cee0144d [test] Fix or disable tests for 32-bit platforms (#82501)
Fix two IRGen tests that are failing on Android armv7 and disable eight ClangImporter, C++ Interop, and SILOptimizer tests, two of which that were already failing on other 32-bit platforms.
2025-06-30 10:39:26 +05:30
Gabor Horvath
7474a51691 [cxx-interop] Avoid copies when accessing pointee
Previously, we would get two copies, one accessing the pointee and one
when we pass the pointee as a method as the implicit self argument.
These copies are unsafe as they might introduce slicing. When
addressable paramaters features are enabled, we no longer make these
copies for the standard STL types. Custom smart pointers can replicate
this by making the lifetime dependency between the implicit object
parameter and the returned reference of operator* explicit via a
lifetime annotation.

rdar://154213694&128293252&112690482
2025-06-25 17:09:55 +01:00
Egor Zhdan
4b39802f63 Merge pull request #82299 from swiftlang/egorzhdan/old-runtime-tests
[cxx-interop] Disable test with an older runtime
2025-06-18 13:14:23 +01:00
Egor Zhdan
62d56067c8 [cxx-interop] Disable test with an older runtime
848fad00 introduced support for printing foreign reference types. It changes both the compiler and the runtime, and having the runtime change applied is required for the test to pass. Let's not try to run it with an old runtime.

This change also splits up a test for printing of value types from a test for printing of foreign reference types, since we don't have any runtime restrictions for value types.

rdar://153205860
2025-06-17 16:27:16 +01:00
Gabor Horvath
ddacdf416d [cxx-interop] Shared references are considered safe
This patch makes sure we don't get warnings in strict memory safe mode
when using shared references. Those types are reference counted so we
are unlikely to run into lifetime errors.

rdar://151039766
2025-06-13 15:41:51 +01:00
Susana Monteiro
6164af2f46 Merge pull request #81832 from swiftlang/susmonteiro/class-metadata-private-fields
[cxx-interop] Support for printing C++ foreign references
2025-06-11 11:58:45 +01:00
susmonteiro
848fad0021 [cxx-interop] Support for printing C++ foreign references 2025-06-10 12:15:21 +01:00
Gabor Horvath
6eb7057e75 [cxx-interop] Avoid swiftifying private and protected methods
The generated overloads do not get seralized and later on the compiler
crashes due to the missing body of the SILFunction. This PR works this
problem around by not generating these overloads. We plan to address the
serialization issue later.

rdar://152181531
2025-06-06 11:07:01 +01:00
Susana Monteiro
e910d9b2f8 Merge pull request #81709 from swiftlang/susmonteiro/ambiguous-use-of-method
[cxx-interop] Fix ambiguous methods in long chains of inheritance
2025-06-03 15:50:28 +01:00
Pavel Yaskevich
e1e9f04398 Merge pull request #81863 from xedin/using-for-default-isolation-in-file-context
[AST/Sema] SE-0478:  Implement `using` declaration under an experimental flag
2025-06-02 09:56:29 -07:00
susmonteiro
79227e7a09 [cxx-interop] Fix ambiguous methods in long chains of inheritance 2025-06-02 16:51:58 +01:00
Gabor Horvath
64babf5ce6 [cxx-interop] Fix compilation failure in rebranch
It looks like in the latest version of clang needs parameters to be
named for the lifetimebound attribute to be applicable.

rdar://151918181
2025-06-02 13:00:55 +01:00
Pavel Yaskevich
c019b669a1 [Tests] NFC: Rename a few Cxx tests to use .cpp extension instead of .swift
These are split-file C++ tests, this is a problem for swift-syntax
because `.swift` tests get parsed for round-trip testing if
swift-syntax is located near swift.
2025-05-31 10:49:50 -07:00
Egor Zhdan
284c3716c2 Merge pull request #81844 from swiftlang/egorzhdan/zero-sized-no-unique-address
[cxx-interop] Do not crash when emitting layout of `std::string`
2025-05-30 22:14:37 +01:00
Egor Zhdan
a7c174431f [cxx-interop] Do not crash when emitting layout of std::string
If a `[[no_unique_address]]` field has zero size according to Clang, and field has a type that isn't representable in Swift, Swift would previously try to add an opaque field of size 1 for it.

This is wrong and was causing crashes for `std::string` while emitting a padding field:
```
_LIBCPP_NO_UNIQUE_ADDRESS ::std::__compressed_pair_padding<T1> _LIBCPP_CONCAT3(__padding1_, __LINE__, _);
```

rdar://151941799
2025-05-30 17:32:15 +01:00
Erik Eckstein
198d4ab0bb Optimizer: run TempRValueElimination also at Onone
Introduce a new pass MandatoryTempRValueElimination, which works as the original TempRValueElimination, except that it does not remove any alloc_stack instruction which are associated with source variables.

Running this pass at Onone helps to reduce copies of large structs, e.g. InlineArrays or structs containing InlineArrays.
Copying large structs can be a performance problem, even at Onone.

rdar://151629149
2025-05-23 18:56:56 +02:00
Egor Zhdan
a91dc9e322 Merge pull request #81704 from swiftlang/egorzhdan/rm-symbolic-remnants
[cxx-interop] Remove remnants of symbolic import mode
2025-05-22 18:26:04 +01:00
Egor Zhdan
4487cd4c10 [cxx-interop] Remove remnants of symbolic import mode
Symbolic import mode was removed in b51cfa5c. This removes a reference to it from a test.
2025-05-22 13:39:32 +01:00
Andrew Trick
6cc1a8d880 Diagnostic message: suppress printing "requires LifetimeDependence"
Adopters of the new Span and MutableSpan types should not encounter information
about an experimental feature when they attempt to use these types in
unsupported ways, such as simply returning them from a function.

Fixes rdar://151788740 (Diagnostic message: suppress printing "requires
'-enable-experimental-feature LifetimeDependence'")
2025-05-21 22:56:57 -07:00
Susana Monteiro
e805d93d95 Merge pull request #81035 from swiftlang/susmonteiro/metadata-private-fields
[cxx-interop] Fix metadata mismatch regarding fields of structs
2025-05-21 19:25:08 +01:00
susmonteiro
72b13b3b48 [cxx-interop] Fix metadata mismatch regarding fields of structs 2025-05-20 10:15:14 +01:00
finagolfin
35ee368bb9 [android][test] Fix or disable the remaining failing tests on the Android CI (#81398)
Also, fix and enable `IRGen/lto_autolink` for all non-Wasm targets and
`IRGen/static_initializer` for aarch64.


This should get [the community Android
CI](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-build/)
green
[again](https://ci-external.swift.org/job/oss-swift-RA-linux-ubuntu-24.04-android-arm64/).
2025-05-17 13:27:33 +05:30
Anthony Latsis
55e5618eab [test] Match nocapture to succeed both on main and rebranch
Both the syntax and relative order of the LLVM `nocapture` parameter
attribute changed upstream in 29441e4f5fa5f5c7709f7cf180815ba97f611297.
To reduce conflicts with rebranch, adjust FileCheck patterns to expect
both syntaxes and orders anywhere the presence of the attribute is not
critical to the test. These changes are temporary and will be cleaned
up once rebranch is merged into main.
2025-05-08 23:52:43 +01:00
Anthony Latsis
4517a6e26b Merge pull request #81279 from AnthonyLatsis/fix-rebranch-on-main
Fix some rebranch test failures on main
2025-05-06 22:20:09 +01:00
Allan Shortlidge
24fa9c8703 ClangImporter: Introduce the #ClangDeclarationImport diagnostic group.
The warnings that ClangImporter emits about issues it encounters while
importing declarations from Clang modules should all belong to a diagnostic
group so that users of `-warnings-as-errors` can control their behavior using
the compiler flags introduce with SE-0443. It's especially important that these
diagnostics be controllable since they are often caused by external
dependencies and therefore the developer may not have any control over whether
they are emitted.

The `#ClangDeclarationImport` diagnostic group is intentionally broad so that
developers have a way to control all of these diagnostics with a single
`-Wwarning` flag. I fully expect that we'll introduce finer-grained diagnostic
groups for some of these diagnostics in the future, but those groups should be
hierarchically nested under `#ClangDeclarationImport`, which is supported by
SE-0443.

Resolves rdar://150524204.
2025-05-05 14:16:53 -07:00
Anthony Latsis
17fc00f8a7 [test] IRGen: Adjust FileCheck patterns for new nuw attribute in upstream LLVM
This attribute was introduced in
7eca38ce76d5d1915f4ab7e665964062c0b37697 (llvm-project).

Match it using a wildcard regex, since it is not relevant to these
tests.

This is intended to reduce future conflicts with rebranch.
2025-05-04 03:28:56 +01:00
John Hui
34be8f988e Merge pull request #81228 from j-hui/fix-ast-layout-dependent-types 2025-05-01 08:40:32 -07:00
John Hui
c2f904caa4 Merge pull request #81188 from j-hui/fix-recursive-inheritance
[cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
2025-05-01 02:00:46 -07:00
John Hui
1fdb239cea [cxx-interop] Avoid querying layout of dependent types during symbolic import
In #80786, we started importing certain padded fields as opaque blobs.
Part of this logic involved querying those fields' ASTRecordLayout.
However, dependent types (which are imported symbolically) do not have
an ASTRecordLayout, so calling Clang's getASTRecordLayout() would lead
to an assertion error for class templates where a no_unique_address
field is some kind of dependent C++ record type.

This patch avoids the field padding check during symbolic import mode
because that check is only relevant for codegen anyway.

rdar://150067288
2025-04-30 18:36:29 -07:00
John Hui
5fdbb44041 [NFC] Add test where the padding of a [[no_unique_address]] field with a custom type is reused 2025-04-30 18:15:27 -07:00
John Hui
9b0ab9095e [NFC] Rename types to mention optional 2025-04-30 15:48:10 -07:00
John Hui
1f2107f357 [cxx-interop] Avoid unchecked recursion when importing C++ classes with circular inheritance
It is possible for a C++ class template to inherit from a specialization
of itself. Normally, these are imported to Swift as separate (unrelated)
types, but when symbolic import is enabled, unspecialized templates are
imported in place of their specializations, leading to circularly
inheriting classes to seemingly inherit from themselves.

This patch adds a check to guard against the most common case of
circular inheritance, when a class template directly inherits from
itself. This pattern appears in a recent version of libc++,
necessitating this patch. However, the solution here is imperfect as it
does not handle more complex/contrived circular inheritance patterns.

This patch also adds a test case exercising this pattern. The
-index-store-path flag causes swift-frontend to index the C++ module
with symbolic import enabled, without the fix in this patch, that test
triggers an assertion failure due to the circular reference (and can
infinitely recurse in the StorageVisitor when assertions are disabled).

rdar://148026461
2025-04-30 13:33:33 -07:00
Gabor Horvath
939ee49dcb [cxx-interop] Fix a crash with [[no_unique_address]]
Swift does not support storing fields in the padding of the previous
fields just yet, so let's not import fields like that from C++.
Represent them as opaque blobs instead.

Fixes #80764

rdar://149072458
2025-04-25 12:46:31 +01:00
Gabor Horvath
26e672e4e0 [cxx-interop] Fix runtime crash passing FRTs as const FRT*
We should pass foreign references as Direct_Unowned calling convention,
this happened for non-const pointers. Unfortunately, we passed the const
variants as Indirect_In. This resulted in SILGen trying to insert a
non-sensical conversion. This triggered an assertion failure or a
miscompilation depending on how the compiler was compiled.

rdar://149398905
2025-04-24 14:32:58 +01:00
Gábor Horváth
8c84ee2675 Merge pull request #80986 from swiftlang/gaborh/namespace-scope-lifetime-annotations
[cxx-interop] Properly import annotations from functions in namespace scope
2025-04-23 08:02:42 -07:00
John Hui
8e19f3f860 [cxx-interop] Enable -verify-additional-file tests on Windows (#80694)
These previously failed because they used '/' as the path separator
instead of '\'. While both are accepted on Windows, the latter is
preferred and used to construct new paths, such as the path of imported
header files. This inconsistency led to distinct buffer ID numbers being
created (since paths are not canonicalized in BufferIdentIDMap), leading
-verify to fail to associate the error emitted from the included file
with the error expected in the -verify-additional-file file.

This patch re-enables these tests but uses %{fs-sep} in place of '/', to
ensure the appropriate path separator is used for the platform where the
test is run.

rdar://148928101
2025-04-22 15:32:18 -07:00
Gabor Horvath
4dd35404b7 [cxx-interop] Properly import annotations from functions in namespace scope
C++ namespaces are imported as Swift enums. This confused the logic that
skips importing namespaces for free functions that are imported as
member functions via the SWIFT_NAME attribute.

rdar://149756644
2025-04-22 13:03:33 +01:00
Kuba Mracek
8e6071ecbc [ClangImporter] Skip value importing on partial/dependent C++ decls, adjust C++ lit tests 2025-04-14 13:20:51 -07:00
fahadnayyar
0ae0528434 [cxx-interop] convert CXXForeignReferenceTypeInitializers into SuppressCXXForeignReferenceTypeInitializers to be an opt-out flag
Turning the feature "ctor of C++ foreign reference types is callable as Swift Initializers" on by default.

rdar://148285972
2025-04-10 00:37:50 -07:00
John Hui
dd2f465025 [cxx-interop] Do not get private discriminator for private Clang types (#80485)
When generating debug symbols for private Clang types (which we started
importing recently), the compiler crashes due to an assertion failure
from ClangModuleUnit::getDiscriminatorForPrivateDecl(), which is called
by getFilePrivateScope().

This patch fixes the issue crash by not calling getFilePrivateScope()
for Clang types. A discriminator is usually needed to disambiguate
private Swift types declared in different files, but Clang types follow
different scoping conventions that make this discriminator unnecessary.

rdar://148481025
2025-04-09 22:32:12 -07:00