Commit Graph

1876 Commits

Author SHA1 Message Date
Gábor Horváth
16cefca106 Merge pull request #80197 from swiftlang/gaborh/mutating-class-func
[cxx-interop] Do not create mutating properties for classes
2025-03-24 14:17:36 +00:00
Egor Zhdan
689586f41b Merge pull request #80204 from swiftlang/egorzhdan/symbolic-missing-lifetime
[cxx-interop] Avoid diagnosing missing lifetime operations in symbolic mode
2025-03-24 12:06:46 +00:00
Gabor Horvath
51193fa538 [cxx-interop] Do not create mutating properties for classes
In Swift, only value types can have mutating instance member functions
or computed properties. The importer logic was violating this invariant
when generating setters for bit fields of shared references.

Fixes #80182
2025-03-21 19:02:35 +00:00
Gábor Horváth
ebeee4d716 Merge pull request #80163 from swiftlang/gaborh/vector-span-property
[cxx-interop] Add span property to std::vector
2025-03-21 19:00:34 +00:00
Egor Zhdan
7c494800a7 [cxx-interop] Avoid diagnosing missing lifetime operations in symbolic mode
When importing C++ decls in symbolic mode, class templates are not instantiated, which means they might not have a destructor or a move constructor. Make sure we are not trying to diagnose those missing lifetime operations in symbolic mode.

This fixes incorrect diagnostics that were emitted during indexing at the end of compilation:
```
warning: 'import_owned' Swift attribute ignored on type 'basic_string': type is not copyable or destructible
```

As a nice side effect, this moves the logic that emits these diagnostics from the request body, which might be invoked many times, to the importer itself, which is only invoked once per C++ class.

rdar://147421710
2025-03-21 18:07:22 +00:00
Egor Zhdan
77ad256926 Merge pull request #80130 from swiftlang/egorzhdan/submodules-name-for-lookup
[cxx-interop] Determine owning module correctly for C++ decls in nested modules
2025-03-21 10:13:21 +00:00
Guillaume Lessard
d66fde2e47 [test] fix availability 2025-03-20 13:03:54 -07:00
Egor Zhdan
106a6af850 [cxx-interop] Determine owning module correctly for C++ decls in nested modules
This fixes a compiler bug that got exposed by f11abac652.

If a C++ type is declared in a nested Clang submodule, Swift was emitting errors that look like:
```
Type alias 'string' is not available due to missing import of defining module 'fwd’
```

rdar://146899125
2025-03-20 19:14:46 +00:00
Gábor Horváth
ddd36a7cfc Merge pull request #79662 from swiftlang/gaborh/dynamic-self-frt
[cxx-interop] Interpret Self as a static shorthand for FRTs
2025-03-20 17:54:35 +00:00
Gabor Horvath
3d6c714cd6 [cxx-interop] Add span property to std::vector
rdar://146944876
2025-03-20 16:15:06 +00:00
Gábor Horváth
8f58ad67cf Update test/Interop/Cxx/foreign-reference/extensions.swift
Co-authored-by: John Hui <j-hui@apple.com>
2025-03-20 12:35:56 +00:00
Susana Monteiro
a8111e4786 Merge pull request #80094 from swiftlang/susmonteiro/destructor-missing-dbg-info
[cxx-interop] Disable debug info in synthesized C++ methods
2025-03-20 10:26:56 +00:00
Andrew Trick
8432d2e788 Fix C++ interop tests using swift_attr("~Escapable")) 2025-03-19 11:59:05 -07:00
Andrew Trick
64a48d08e1 Update tests for strict @lifetime type checking 2025-03-19 11:59:04 -07:00
Egor Zhdan
02dfe26f8f Merge pull request #80090 from swiftlang/egorzhdan/std-string-interpolation
[cxx-interop] Conform `std::string` to `ExpressibleByStringInterpolation`
2025-03-19 18:28:56 +00:00
susmonteiro
25efb5fc04 [cxx-interop] Disable debug info in synthesized C++ methods
rdar://141167229
2025-03-19 18:02:50 +00:00
Gabor Horvath
7bdd496f61 Address review comments. 2025-03-19 12:56:56 +00:00
Gabor Horvath
b632534528 [cxx-interop] Interpret Self as a static shorthand for FRTs
We do not have dynamic self metadata for foreign reference types.

rdar://145066864
2025-03-19 12:56:56 +00:00
Egor Zhdan
e32e64353f [cxx-interop] Conform std::string to ExpressibleByStringInterpolation
This adds conformances for C++ string types (`std::string`, `std::u16string`, `std::u32string`) to `Swift.ExpressibleByStringInterpolation`.

These conformances currently implicitly use `DefaultStringInterpolation`. In the future we can provide more performant interpolation mechanisms for C++ strings specifically that avoid the extra conversion between Swift String and C++ string types.

rdar://147249169
2025-03-18 15:54:56 +00:00
Doug Gregor
9c2c4ea07f Merge pull request #79707 from DougGregor/transparent-integer-conversions
[Stdlib performance] Make integer conversion operations transparent
2025-03-17 16:37:16 -07:00
Doug Gregor
bf387c56e2 Merge pull request #80030 from DougGregor/import-macro-in-cpp-class-and-namespace
[Clang importer] Report auxiliary decls from C++ class and namespace lookup
2025-03-14 19:23:00 -07:00
Gábor Horváth
aadf874076 Merge pull request #79951 from swiftlang/gaborh/non-type-alias-span
[cxx-interop] Fix transforming spans that are not behind type aliases
2025-03-14 22:55:01 +00:00
Doug Gregor
84a4a8bedb [Importer] Ensure that we can see macro-expanded declarations in C++ namespaces
Lookup into C++ namespaces uses a different path from C++ record declarations.
Augment the C++ namespace lookup path to also account for the auxiliary
declarations introduced by peer macro expansions.
2025-03-14 14:10:44 -07:00
Doug Gregor
e3618dd797 [Clang importer] Report auxiliary decls from C++ member lookup
When performing name lookup into a C++ record type, make sure that we
also walk through auxiliary declarations (i.e., declarations that can
come from peer macro expansions) to find results.

Fixes rdar://146833294.
2025-03-14 12:23:28 -07:00
Gabor Horvath
11593329ea [cxx-interop] Fix transforming spans that are not behind type aliases
While we expect our users to use type aliases for template
instantiations, there are some contexts when we import instantiations
without aliases. Unfortunately, in case of C++ span we generated a name
for the instantiation that cannot be a syntactically valid Swift type
due to unary negation appearing in the type name. This PR replaces the
unary negation with "Neg" in the type name and also fixed a bug that
ended up printing certain unsigned values as signed. Moreover, this PR
also fixes some other fallouts in the SwiftifyImport macro.

rdar://146833480
2025-03-14 17:59:09 +00:00
Florian Hahn
dff5da4b32 [Test] Use -disable-llvm-merge-function-pass to unblock MergeFunc fix.
To unblock CI for https://github.com/swiftlang/llvm-project/pull/10112.
2025-03-14 09:49:14 +00:00
John Hui
76a1742aca [cxx-interop] Use formal C++ interop mode to fix name lookup in module interfaces (#79984)
It is possible for a module interface (e.g., ModuleA) to be generated
with C++ interop disabled, and then rebuilt with C++ interop enabled
(e.g., because ModuleB, which imports ModuleA, has C++ interop enabled).

This circumstance can lead to various issues when name lookup behaves
differently depending on whether C++ interop is enabled, e.g., when
a module name is shadowed by a namespace of the same name---this only
happens in C++ because namespaces do not exist in C. Unfortunately,
naming namespaces the same as a module is a common C++ convention,
leading to many textual interfaces whose fully-qualified identifiers
(e.g., c_module.c_member) cannot be correctly resolved when C++ interop
is enabled (because c_module is shadowed by a namespace of the same
name).

This patch does two things. First, it introduces a new frontend flag,
-formal-cxx-interoperability-mode, which records the C++ interop mode
a module interface was originally compiled with. Doing so allows
subsequent consumers of that interface to interpret it according to the
formal C++ interop mode. Note that the actual "versioning" used by this
flag is very crude: "off" means disabled, and "swift-6" means enabled.
This is done to be compatible with C++ interop compat versioning scheme,
which seems to produce some invalid (but unused) version numbers. The
versioning scheme for both the formal and actual C++ interop modes
should be clarified and fixed in a subsequent patch.

The second thing this patch does is fix the module/namespace collision
issue in module interface files. It uses the formal C++ interop mode to
determine whether it should resolve C++-only decls during name lookup.
For now, the fix is very minimal and conservative: it only filters out
C++ namespaces during unqualified name lookup in an interface that was
originally generated without C++ interop. Doing so should fix the issue
while minimizing the chance for collateral breakge. More cases other
than C++ namespaces should be added in subsequent patches, with
sufficient testing and careful consideration.

rdar://144566922
2025-03-13 23:24:18 -07:00
Egor Zhdan
ae44f9b6c3 Merge pull request #79962 from swiftlang/egorzhdan/libcxx-exec
[cxx-interop] Fix libc++ tests
2025-03-13 12:25:18 +00:00
Guillaume Lessard
04545d328d Merge pull request #79961 from glessard/rdar146792412-span-test-availability
[gardening] adjust Span availability in more tests
2025-03-12 19:37:30 -07:00
Egor Zhdan
8ef468c5f1 [cxx-interop] Fix libc++ tests
These two tests require execution privileges in order to run `check-libcxx-version`, which is used to restrict the tests to a range of libc++ versions. They were failing on `non_executable` CI jobs because of missing `// REQUIRES: executable_test`.

rdar://145821727
2025-03-12 19:51:58 +00:00
Guillaume Lessard
aecaae3004 [gardening] adjust Span availability in more tests 2025-03-12 12:44:17 -07:00
Doug Gregor
c2417e0a14 Update tests to account for transparent on integral conversions 2025-03-12 07:40:52 -07:00
Gabor Horvath
f8873658af [cxx-interop] Remove last LifetimeDependence guard in C++ interop
We now accept @lifetime annotations in the import macro generated code
so no longer need to guard the emission of these attributes with this
feature flag.
2025-03-12 10:45:19 +00:00
Gábor Horváth
2386508718 Merge pull request #79909 from swiftlang/gaborh/ctor-value-type
[cxx-interop] Test static factory backed initializers with value types
2025-03-12 00:12:36 +01:00
Susana Monteiro
7f1792a12f Merge pull request #79791 from swiftlang/susmonteiro/enums-same-gen-name
[cxx-interop] Use qualified name in import info of clang declarations
2025-03-11 20:34:45 +00:00
Egor Zhdan
9560b86099 Merge pull request #79890 from swiftlang/egorzhdan/nsoptions-name-based
[cxx-interop] Remove workarounds for `CF_OPTIONS` default arguments
2025-03-11 18:18:27 +00:00
Gabor Horvath
14d92ff7a1 [cxx-interop] Test static factory backed initializers with value types
We can use swift_name to import a static factory function as a Swift
initializer. This was tested with foreign reference types but not with
value types. This PR adds a test case for the latter.

rdar://117531428
2025-03-11 17:36:54 +00:00
Egor Zhdan
5b7c595b2e [cxx-interop] Remove workarounds for CF_OPTIONS default arguments
ClangImporter has logic that infers default arguments of certain C/C++ types, such as the types declared via `CF_OPTIONS`/`NS_OPTIONS` macros.

There were some workarounds in place which triggered for C++ interop mode specifically. The workarounds were applying a heuristic based on the name of the type, which tried to match the behavior to non-C++ interop mode for certain types from the OS SDK. That was not working well for user-defined types, causing source compatibility breakages when enabling C++ interop.

This change replaces the name-based heuristic with a more robust criteria.

See also 3791ccb6.

rdar://142961112
2025-03-11 12:47:21 +00:00
Gábor Horváth
94ca6fa86a Merge pull request #79793 from swiftlang/gaborh/remove-feature-flag 2025-03-10 17:57:26 +01:00
susmonteiro
18ee0ee6f1 [cxx-interop] Use qualified name in import info of clang declarations 2025-03-10 12:39:32 +00:00
Egor Zhdan
3169cdaef2 Merge pull request #79837 from swiftlang/egorzhdan/pthread-test-reenable
[cxx-interop] Re-enable a test for Obj-C compatibility
2025-03-08 14:07:31 +00:00
Egor Zhdan
c7ddbeeec9 Merge pull request #79822 from swiftlang/egorzhdan/NSUnderlineStyle
[cxx-interop] Allow AppKit & UIKit to be rebuilt with C++ interop enabled
2025-03-07 23:07:03 +00:00
susmonteiro
51357a952c [cxx-interop] Prevent usage in Swift of C++ move constructor with default args 2025-03-07 16:02:38 +00:00
Egor Zhdan
83f3a2dc56 [cxx-interop] Re-enable a test for Obj-C compatibility
rdar://128544755
2025-03-07 15:00:41 +00:00
Egor Zhdan
3791ccb6e6 [cxx-interop] Allow AppKit & UIKit to be rebuilt with C++ interop enabled
This removes a workaround from the module interface loader, which was forcing AppKit and UIKit to be rebuilt from their textual interfaces with C++ interop disabled, even if the current compilation explicitly enables it.

The workaround was previously put in place because of a compiler error:
```
error: type 'AttributeScopes.AppKitAttributes.StrikethroughStyleAttribute' does not conform to protocol 'AttributedStringKey'
note: possibly intended match 'AttributeScopes.AppKitAttributes.StrikethroughStyleAttribute.Value' (aka 'NSUnderlineStyle') does not conform to 'Hashable'
```

`NSUnderlineStyle` is a C/C++ type from AppKit that is declared using `NS_OPTIONS` macro. `NS_OPTIONS`/`CF_OPTIONS` macros have different expansions in C vs C++ language modes. The C++ expansions weren't handled correctly by ClangImporter, resulting in two distinct Swift types being created: a `typealias NSUnderlineStyle` which was marked as unavailable in Swift, and `enum NSUnderlineStyle`. This mostly worked fine, since the lookup logic was picking the enum during regular name lookup. However, this silently broke down when rebuilding the explicit conformance from `AppKit.swiftinterface`:
```
extension AppKit.NSUnderlineStyle : Swift.Hashable {}
```
Swift was picking the (unavailable) typealias when rebuilding this extension, which means the (available) enum wasn't getting the conformance.

This is verified by an existing test (`test/Interop/Cxx/objc-correctness/appkit-uikit.swift`).

rdar://142961112
2025-03-07 13:27:11 +00:00
Gabor Horvath
6b24af65f0 [cxx-interop] Always import the lifetime annotations
Importing these annotations were behind the LifetimeDependence
experimental flag. However, this feature flag is intended to guard the
use of @lifetime annotations on the Swift side and lifetime inference.
The checking of imported lifetime contracts should work even when this
flag is off. Removing the flag from the importer caused some fallout.
This was mostly due to calling getInterfaceType functions before the
import of some Swift declarations were fully done so the code was
slightly improved to make decisions only based on the C++ types.
There was also a crash when on-member functions imported as methods into
Swift. That is worked around in this PR.

There is one last feature check that we cannot remove yet, we generate
@lifetime annotations in the SwiftifyImport macro.
2025-03-05 15:36:59 +00:00
John Hui
edc742013d [cxx-interop] Make experimental flag ImportNonPublicCxxMembers (#79728)
ClangImporter can now import non-public members as of be73254cdc and 66c2e2c52b, but doing so triggers some latent ClangImporter bugs in projects that don't use or need those non-public members.

This patch introduces a new experimental feature flag, ImportNonPublicCxxMembers, that guards against the importation of non-public members while we iron out those latent issues. Adopters of the SWIFT_PRIVATE_FILEID feature introduced in bdf22948ce can enable this flag to opt into importing private members they wish to access from Swift.

rdar://145569473
2025-03-04 13:31:46 -05:00
Gábor Horváth
3b412ae0c6 Merge pull request #79697 from swiftlang/gaborh/do-not-import-nonesc-pointees
[cxx-importer] Do not import pointers to non-escapable types
2025-03-04 17:26:38 +00:00
Susana Monteiro
593f320460 Merge pull request #79325 from swiftlang/susmonteiro/copy-constructor-default-args
[cxx-interop] Prevent usage in Swift of C++ copy constructor with default args
2025-03-04 15:40:22 +00:00
Gabor Horvath
d0c167762c [cxx-importer] Do not import pointers to non-escapable types
Unfortunately, Unsafe*Pointer types do not support non-escapable
pointees so we do not really have anything to map these types to at the
moment. Previously, importing such code resulted in crashes.

rdar://145800679
2025-03-04 12:27:34 +00:00