Commit Graph

2508 Commits

Author SHA1 Message Date
Egor Zhdan
b51cfa5c76 [cxx-interop] Remove symbolic import mode
Importing C++ class templates in symbolic mode has proven to be problematic in interaction with other compiler features, and it isn't used widely. This change removes the feature.

rdar://150528798
2025-05-02 18:43:09 +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
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
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
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
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
Pavel Yaskevich
3906980f58 [AST] NFC: Add a convenient way to create implicit NonisolatedAttrs 2025-04-11 15:59:25 -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
Gabor Horvath
e0a431ec98 [cxx-interop] Diagnose Escapable C++ types with non-escapable fields
rdar://148899224
2025-04-09 13:01:31 +01: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
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
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
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
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
John Hui
bbf92fd6e1 [cxx-interop] Import non-public members of SWIFT_PRIVATE_FILEID-annotated classes (#80320) 2025-03-26 22:41:38 -07: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
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
Andrew Trick
d41c4d4cc9 ClangImporter: fix C++ memberwise inits to use @lifetime(immortal)
_unsafeNonEscapableResult no longer dsiables diagnostics at the call site. We
need to use a proper lifetime annotation for that.
2025-03-19 11:59:05 -07:00
Gabor Horvath
7bdd496f61 Address review comments. 2025-03-19 12:56:56 +00:00
Allan Shortlidge
e96b44be31 ClangImporter: Import clang::DomainAvailabilityAttr.
Import `__attribute__((availability(domain:MyDomain, ...)))` as
`@available(SomeDomain, ...)`.

Resolves rdar://138441266.
2025-03-15 07:44:37 -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
94ca6fa86a Merge pull request #79793 from swiftlang/gaborh/remove-feature-flag 2025-03-10 17:57:26 +01:00
susmonteiro
51357a952c [cxx-interop] Prevent usage in Swift of C++ move constructor with default args 2025-03-07 16:02:38 +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
Gabor Horvath
2f46d55b85 [cxx-interop] Support importing static factories as initializers
This PR adds a feature to import static factory functions returning
foreign reference types to be imported as initializers using the
SWIFT_NAME annotation.
2025-03-03 11:25:05 +00:00
Gabor Horvath
bbd60c81d4 [cxx-interop] Fix circular reference, remove feature flag check
It turns out the query to check the reference semantics of a type had
the side effect of importing some functions/types. This could introduce
circular reference errors with our queries. This PR removes this side
effect from the query and updates the test files. Since we do the same
work later on in another query, the main change is just the wording of
the diagnostics (and we now can also infer immortal references based on
the base types). This PR also reorders some operations, specifically now
we mark base classes as imported before we attempt to import template
arguments.

After these changes it is possible to remove the last feature check for
strict memory safe mode. We want to mark types as @unsafe in both
language modes so we can diagnose redundant unsafe markers even when the
feature is off.
2025-02-28 15:05:02 +00:00
Doug Gregor
bad5adbf36 Merge pull request #79680 from DougGregor/clang-importer-always-unsafe
[Clang importer] Infer @unsafe independent of strict safety checking mode
2025-02-27 20:57:08 -08:00
Doug Gregor
06e61161f4 [Clang importer] Infer @unsafe independent of strict safety checking mode 2025-02-27 15:00:16 -08:00
Gábor Horváth
ed9eef234e Merge pull request #79324 from swiftlang/gaborh/redundant-lookups
[cxx-interop] Remove some duplicated lookups
2025-02-27 17:22:53 +00:00
Doug Gregor
b7b5a2a19d [SE-0458] Enable unsafe expressions / attributes / for..in effects by default
With the acceptance of SE-0458, allow the use of unsafe expressions, the
@safe and @unsafe attributes, and the `unsafe` effect on the for..in loop
in all Swift code.

Introduce the `-strict-memory-safety` flag detailed in the proposal to
enable strict memory safety checking. This enables a new class of
feature, an optional feature (that is *not* upcoming or experimental),
and which can be detected via `hasFeature(StrictMemorySafety)`.
2025-02-26 12:30:07 -08:00
John Hui
66c2e2c52b [cxx-interop] Import non-public inherited members (#79348)
This patch is follow-up work from #78942 and imports non-public members,
which were previously not being imported. Those members can be accessed
in a Swift file blessed by the SWIFT_PRIVATE_FILEID annotation.

As a consequence of this patch, we are also now importing inherited members
that are inaccessible from the derived classes, because they were declared
private, or because they were inherited via nested private inheritance. We
import them anyway but mark them unavailable, for better diagnostics and to
(somewhat) simplify the import logic for inheritance.

Because non-public base class members are now imported too, this patch
inflames an existing issue where a 'using' declaration on an inherited member
with a synthesized name (e.g., operators) produces duplicate members, leading
to miscompilation (resulting in a runtime crash). This was not previously noticed
because a 'using' declaration on a public inherited member is not usually
necessary, but is a common way to expose otherwise non-public members.
This patch puts in a workaround to prevent this from affecting the behavior
of MSVC's std::optional implementation, which uses this pattern of 'using'
a private inherited member. That will be fixed in a follow-up patch.

Follow-up work is also needed to correctly diagnose ambiguous overloads
in cases of multiple inheritance, and to account for virtual inheritance.

rdar://137764620
2025-02-25 01:03:16 -08:00
Henrik G. Olsson
afe86b61aa address review comments 2025-02-24 08:56:20 -08:00
Henrik G. Olsson
957db2c959 [ClangImporter] Merge paths for std::span and __counted_by
importBoundsAttributes and importSpanAttributes are merged into a single
function named swiftify. This allows us to not have to duplicate the
effort of attaching _SwiftifyImport macros, but is also necessary to
allow importing a function with both __counted_by and std::span types.
2025-02-21 20:08:59 -08:00