Commit Graph

6196 Commits

Author SHA1 Message Date
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
Yeoul Na
2d7b7a73f8 [Swiftify] SafeInteropWrapper crashes with 'cgImage.width' (#80948)
* [Swiftify] Fix crash calling cgImage.width

cgImage.width calls the C function, CGImageGetWidth(CGImageRef).
Swift representation and Clang representation of this function
seem to have a parameter number mismatch, causing swiftify function
to crash.

rdar://149691207

* [NFC] Fix test/Interop/ObjC/swiftify-import/getter.swift
2025-04-30 08:53:22 -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
Gábor Horváth
c2f560e329 Merge pull request #81030 from swiftlang/gaborh/span-ctor-crash 2025-04-23 12:46:21 -07:00
Gabor Horvath
5238c3fc9b [cxx-interop] Work around crash in codegen when initializing C++ span
When initializing span with an UnsafePointer<Element>? we call into the
generic initializer that we imported from the C++ templated constructor
instead of the concrete initializer we have in the overlay that takes an
UnsafePointer<Element> (non-optional). We cannot properly codegen for
this generic initializer at the moment, so let's stop importing them
since the user probably wanted to call the initializer from the overlay.

We should come back later and fix the root cause.

rdar://148961349
2025-04-23 16:12: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
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
6325f05c9f [ClangImporter] Stop setting accessor's access level in SwiftDeclSynthesizer::createConstant 2025-04-21 12:50:15 -07:00
Kuba Mracek
f0094c24e4 [ClangImporter] Don't import accessors of constants as 'open' if they're static 2025-04-20 19:57:54 -07:00
Kuba Mracek
d70498957a [ClangImporter] Handle nullptr from importTypeIgnoreIUO() in constant values importing logic 2025-04-18 17:19:31 -07:00
Kuba Mracek
26cc36b8b0 [ClangImporter] Handle swift_wrapper annotated typedefs, skip CGFloats when importing const values 2025-04-14 15:03:18 -07: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
Kuba Mracek
936e91130a [ClangImporter] Fix importing values for constant globals of enum types 2025-04-14 10:01:23 -07:00
Kuba Mracek
a39f60b9ad [ClangImporter] Import constant values for 'const' globals 2025-04-14 10:01:23 -07:00
Egor Zhdan
6a6380bb56 Merge pull request #80763 from swiftlang/egorzhdan/rvalue-arg-operator
[cxx-interop] Do not import arithmetic operators with rvalue reference parameters
2025-04-14 11:26:30 +01:00
Pavel Yaskevich
3906980f58 [AST] NFC: Add a convenient way to create implicit NonisolatedAttrs 2025-04-11 15:59:25 -07:00
Egor Zhdan
eb7adc794d [cxx-interop] Do not import arithmetic operators with rvalue reference parameters
Currently those operators are imported with a `consuming:` label, which isn't valid in Swift.

We could just remove the label from these parameters, but that introduces a source breakage due to name lookup ambiguity.

So, to avoid ambiguity, let's not import such operators into Swift.

rdar://149020099
2025-04-11 17:07:37 +01: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
Steven Wu
1194b59fce Merge pull request #80512 from cachemeifyoucan/eng/PR-148538787
[BridgingHeaderChaining] Bind bridging header module when load module
2025-04-09 10:24:11 -07:00
Gabor Horvath
e0a431ec98 [cxx-interop] Diagnose Escapable C++ types with non-escapable fields
rdar://148899224
2025-04-09 13:01:31 +01:00
Allan Shortlidge
94a8526526 Merge pull request #80616 from tshortli/availability-domain-serialization
Serialization: Encode custom availability domains
2025-04-08 22:39:03 -07:00
Ahmed Bougacha
1b5a94e219 [ClangImporter] Let clang pick default target CPU. (#80451)
Currently, ClangImporter has some logic to pick the default arm64
target CPU (when -target-cpu isn't passed) based on the target OS
and arch variant.

This was originally done long ago in 3cf3f42e98, and later
maintained through 49a6c8eb7b, because it was necessary when
clang (targeting darwin) relied on -arch to set this sort of default.

Clang has migrated to full -target triples for a while now, and has
sophisticated logic for picking default target CPUs and features based
on the target triple.

Currently, we override that by passing our -mcpu explicitly.
Instead, allow clang to pick its defaults, and only pass -mcpu
when asked via -target-cpu.

This is visible in the test, with arm64 macOS now defaulting to M1.
The same applies to simulators, per Triple::isTargetMachineMac.

rdar://148377686
2025-04-08 17:34:23 -07:00
fahadnayyar
56746e3456 [cxx-interop] Relax error when using SWIFT_RETURNS_(UN)RETAINED on templated return types resolving to non-frts (#78968)
We should not complain about usage of SWIFT_RETURNS_(UN)RETAINED for templated C++ APIs when instantiated with a non SWIFT_SHARED_REFERENCE types

rdar://143732201
2025-04-08 10:39:38 -07:00
fahadnayyar
5d1ce01eb9 [cxx-interop] Import parameterized public ctors of C++ foreign ref types as Swift Initializer (#80449)
Extends PR #79986 by adding support for calling parameterized C++ initializers from Swift. This patch synthesizes static factory methods corresponding to C++ parameterized constructors, allowing Swift to call them as Swift initializers (e.g., init(_:), init(_:_:), etc.). This patch also aded tests and logic to make sure that we emit no additional diagnostics when a C++ foreign ref type is just referred from Swift and its initializer is not explicitly called.

rdar://148285251
2025-04-08 08:58:11 -07:00
Allan Shortlidge
abac42b759 Serialization: Encode custom availability domains.
When serializing `@available` attributes, if the attribute applies to a custom
domain include enough information to deserialize the reference to that domain.

Resolves rdar://138441265.
2025-04-08 08:53:42 -07:00
Allan Shortlidge
6527a068eb ClangImporter: Remove custom AvailabilityDomain serialization.
Partially revert https://github.com/swiftlang/swift/pull/80035 now that Clang
has its own APIs for querying serialized modules for the decl representing the
availability domain with a given name.
2025-04-08 08:53:06 -07:00
Becca Royal-Gordon
274c4d9d22 Merge pull request #80557 from beccadax/the-case-of-the-missing-member
[ClangImporter] Fix import of aliased enum cases
2025-04-07 16:25:53 -07:00
Gábor Horváth
fe98abb10b Merge pull request #80598 from swiftlang/gaborh/nested-foreign-type-metadata
[cxx-interop] Fix a rare compilation error in reverse interop header
2025-04-08 00:02:04 +01:00
Gabor Horvath
b3b20310f5 [cxx-interop] Fix a rare compilation error in reverse interop header
To trigger this error one needs to import a nested type from C++, use it
in a generic context in Swift, and export it back to C++. We were
inconsisent in what namespace did we declare the functions to get the
type metadata for types. It was in the swift namespace for foreign types
and in the module namespace for Swift types. This PR standardizes on how
the metadata function is declared and called to fix the issue.

Fixes #80538.

rdar://148597079
2025-04-07 17:50:19 +01:00
Crazy凡
c05d1fc135 [c++ interop] Swift should allow multiple SWIFT_CONFORMS_TO_PROTOCOL attributes on a C++ class. 2025-04-06 22:24:14 +08:00
Anthony Latsis
cdb2aaccfd AST: Cut down on DescriptiveDeclKind usage in DiagnosticsClangImporter.def 2025-04-05 12:31:20 +01:00
Becca Royal-Gordon
86704e35e1 [ClangImporter] Fix import of aliased enum cases
When a C enum has multiple constants with the same value, ClangImporter selects one of them to import as an `EnumElementDecl` and imports the others as `VarDecl` “aliases” of that one. This helps preserve the invariant that each case of an enum has a unique raw value and is distinct for exhaustiveness checking.

However, a bug in that logic could sometimes cause the canonical case to be imported *twice*—once as an `EnumElementDecl` and again as a `VarDecl` alias. In this situation, the `EnumElementDecl` was not added to the enum’s member list, resulting in a malformed AST. This bug has apparently been present since early 2017 (!), but it seems to have been harmless until recently, when the `ComputeSideEffects` SIL pass recently became sensitive to it (probably because of either #79872 or #80263).

Correct this problem by modifying the memoization logic to retrieve the canonical case’s clang-side constant so the subsequent check will succeed. Additionally change a variable name and add comments to help clarify this code for future maintainers.

In lieu of adding new unit tests, this commit adds a (slightly expensive) conditional assertion to catch this kind of AST malformation. There are actually about twenty tests that will fail with just the assertion and not the fix, but I’ve updated one of them to enable the assertion even in release mode.

Fixes rdar://148213237. Followup to #80487, which added related assertions to the SIL layer.
2025-04-04 18:54:36 -07:00
Gábor Horváth
20f73556f2 Merge pull request #80500 from swiftlang/gaborh/not-imported-return-type
[cxx-interop] Fix not importing return type for certain functions
2025-04-04 10:35:15 +01:00
Steven Wu
02ee2f4d62 [BridgingHeaderChaining] Bind bridging header module when load module
When loading a module with embedded bridging header, bind the bridging
header module in the context when bridging header auto chaining is used.
This is because all the bridging header contents are chained into a PCH
file so binary module with bridging header should reference the PCH file
for all declarations.

rdar://148538787
2025-04-03 15:04:03 -07:00
Gabor Horvath
d254e83376 [cxx-interop] Fix not importing return type for certain functions
C++ namespaces are imported as enums. The importer triggered different
code path for functions in C++ namespaces and functions in the global
scope. As a result, we occasionally did not import the return type of
certain C++ functions in namespace scope.
2025-04-03 18:12:36 +01:00
John Hui
50f5221962 [NFC] promote getOperatorName() to a non-static function (#80466) 2025-04-03 10:42:37 -04:00
Doug Gregor
7418b2593c Merge pull request #80480 from DougGregor/imported-union-member-unsafe
[Strict memory safety] Union member accessors are always unsafe
2025-04-03 03:30:25 -07:00
Doug Gregor
c522138987 [Strict memory safety] Union member accessors are always unsafe
Union member accessors have no way to know what the "active field" is,
so consider them to always be unsafe.
2025-04-02 16:59:17 -07:00
Gabor Horvath
77e037d5f5 [cxx-interop] Fix invalid source range for some template instantiations
Apparently, clang will pick the source range of the canonical
declaration for template specializations. This might be in a different
buffer than the definition triggering various issues. Fortunately, Clang
also has the source locations we want, we just need to use a different
API to get them. Unfortunately, I haven't figured it out yet how to
trigger a failing test.

rdar://148250404
2025-04-01 15:18:32 +01:00
John Hui
e8bcc52356 [cxx-interop] Fix access check for nested private C++ enums (#80366)
This patch fixes the access check for nested private C++ enums to look for the SWIFT_PRIVATE_FILEID of the enclosing C++ class, if any. Previously, the check was looking at for SWIFT_PRIVATE_FILEID on the enum decl itself (which is meaningless); that prevented nested private enum members from being accessible in Swift.

This patch also specializes the type signature of getPrivateFileIDAttrs to clarify the fact that SWIFT_PRIVATE_FILEID is not a meaningful annotation on anything other than CXXRecordDecl, because that is the only kind of decl that can assign access specifiers to its members.

rdar://148081340
2025-03-31 22:23:22 -07:00
fahadnayyar
9694cc8d70 [cxx-interop] Import default public ctor of C++ foreign ref types as Swift Initializer (#79986)
Building on top of PR #79288, this update synthesizes a static factory method using the default new operator, with a call to the default constructor expression for C++ foreign reference types, and imports them as Swift initializers.

rdar://147529406
2025-03-31 20:33:53 -07:00
John Hui
3b18849dcc [NFC] Remove stale FIXME about importing enum variants (#80358)
This comment is from 940a65a994, but is now stale.
2025-03-31 16:05:48 -07:00
Alastair Houghton
b1c345f1be Merge pull request #80266 from al45tair/custom-executors-take2
[Concurrency] Provide a Swift interface for custom main and global executors.
2025-03-31 09:53:48 +01:00
Hiroshi Yamauchi
42fe7d90cb Merge pull request #79926 from hjyamauchi/explicit-module-build
Propagate vfs overlays and -fbuiltin-headers-in-system-modules
2025-03-28 21:58:40 -07:00
Alastair Houghton
47fa71787f Revert "Merge pull request #80224 from glessard/revert-79789-custom-executors"
This reverts commit 06f6358067, reversing
changes made to 033f6679e8.
2025-03-28 10:15:07 +00:00
Hiroshi Yamauchi
8312f7ad9a Propagate vfs overlays and -fbuiltin-headers-in-system-modules
This fixes explicit module builds for a hello world program on Windows
as well as the ucrt import build failure as in the included test.
2025-03-27 20:57:13 -07:00
John Hui
bbf92fd6e1 [cxx-interop] Import non-public members of SWIFT_PRIVATE_FILEID-annotated classes (#80320) 2025-03-26 22:41:38 -07:00
Becca Royal-Gordon
a5a84f188e Merge pull request #79466 from beccadax/abi-inspected-your-appearance
@abi checking
2025-03-26 15:28:39 -07:00
Becca Royal-Gordon
1bb2186377 Inherit @objc, dynamic, @implementation in @abi
ABI-only declarations now query their API counterpart for things like `isObjC()`, their ObjC name, dynamic status, etc. This means that `@objc` and friends can simply be omitted from an `@abi` attribute.

No tests in this commit since attribute checking hasn’t landed yet.
2025-03-26 10:47:13 -07:00
Gabor Horvath
ff42f2ee79 [cxx-interop] Fix a regression making std::string unsafe
Currently, we only get warnings for using unsafe types in expressions
but not in the function signature. The tests did not use the std::string
object in the function body. As a result, we regressed and std::string
was considered unsafe.

The reason is that the annotation only mode for calculating escapability
of a type did not do what we intended. std::basic_string is
conditionally escapable if the template argument is escapable. We
considered 'char' to have unknown escapability in annotation only mode.
The annotation only mode was introduced to avoid suddenly importing
certain types as not escapable when they have pointer fields and break
backward compatibility.

The solution is to make annotation only mode to still consider char and
co as escapable types and only fall back to unknown when the inference
otherwise would have deduced non-escapable (for unannotated typed).
2025-03-26 15:25:37 +00:00