Commit Graph

2485 Commits

Author SHA1 Message Date
Anthony Latsis
cdb2aaccfd AST: Cut down on DescriptiveDeclKind usage in DiagnosticsClangImporter.def 2025-04-05 12:31:20 +01: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
fahadnayyar
9782eae585 Merge pull request #77522 from fahadnayyar/cxx-frt-inheritance-diagnostics
[cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
2025-02-20 13:46:28 -08:00
fahadnayyar
d8f919778d [cxx-interop] [cxx-interop] Infer SWIFT_SHARED_REFERENCE for types inheriting from a C++ foreign reference type
rdar://97914474
2025-02-20 08:46:57 -08:00
Gábor Horváth
91531554de Merge pull request #79458 from swiftlang/gaborh/unsafe-cleanup 2025-02-18 20:33:08 +00:00
John Hui
bdf22948ce [cxx-interop] Allow Swift to access non-public C++ members (#79093)
This patch introduces an a C++ class annotation, SWIFT_PRIVATE_FILEID,
which will specify where Swift extensions of that class will be allowed
to access its non-public members, e.g.:

    class SWIFT_PRIVATE_FILEID("MyModule/MyFile.swift") Foo { ... };

The goal of this feature is to help C++ developers incrementally migrate
the implementation of their C++ classes to Swift, without breaking
encapsulation and indiscriminately exposing those classes' private and
protected fields.

As an implementation detail of this feature, this patch introduces an
abstraction for file ID strings, FileIDStr, which represent a parsed pair
of module name/file name.

rdar://137764620
2025-02-18 11:22:44 -08:00
Gabor Horvath
c859557f8b [cxx-interop] Remove duplicated logic to import types as unsafe
After PR #79424 was merged the compiler proper is doing inference on
what C++ types should be considered unsafe. Remove the duplicated (and
slightly divergent) logic from the importer as we no longer need it and
we should have a consistent view of what is considered unsafe. The only
divergence left is the old logic that renames some methods to have
"Unsafe" in their names. In the future, we want to get rid of this
behavior (potentially under a new interop version).
2025-02-18 12:22:40 +00:00
Becca Royal-Gordon
d8c8f6577b Merge pull request #79206 from beccadax/this-name-is-not-constructive
Diagnose and forbid invalid Swift names on inits
2025-02-17 20:17:56 -08:00
Gábor Horváth
07b8f0ab32 Merge pull request #79434 from swiftlang/gaborh/escapibility-lookup
[SE-0458] Unify escapability inference for AST and Interop
2025-02-17 20:04:02 +00:00
Gábor Horváth
bc0da49746 Merge pull request #79432 from swiftlang/gaborh/remove-safe-interop-feature-flag
[cxx-interop] Remove a feature flag
2025-02-17 17:27:03 +00:00
Joe Groff
f778170cfb Merge pull request #79351 from jckarter/lifetime-dependence-lowering
SIL: Lower lifetime dependencies when lowering function types.
2025-02-17 07:51:20 -08:00
Gabor Horvath
410341671e [SE-0458] Unify escapibility inference for AST and Interop
Interop is injecting escapability annotations for the STL and doing a
limited inference for aggregates. Let's reuse the same facilities in the
AST when we calculate the safety of the foreign types.
2025-02-17 13:41:40 +00:00
Gabor Horvath
648fd43709 [cxx-interop] Remove a feature flag
SafeInterop was guarding whether we import certain foreign types as
unsafe. Since these attrbutes are only considered when an opt-in strict
language mode is on, this PR removes this feature flag. We still rely on
the presence of the AllowUnsafeAttribute flag to add the unsafe
attributes to the imported types and functions.
2025-02-17 12:37:31 +00:00
Hamish Knight
1e8dc55b7f [AST] NFC: Remove InitRetType
This is no longer used.
2025-02-16 18:52:23 +00:00
Joe Groff
c65475628f SIL: Lower lifetime dependencies when lowering function types.
Map the lifetime dependencies described in terms of the formal AST-level parameters
to the correct parameter(s) in the lowered SIL function type. There can be 0, 1,
or many SIL parameters per formal parameter because of tuple exploding. Also,
record which dependencies are on addressable parameters (meaning that the dependency
includes not only the value of the parameter, but its specific memory location).
2025-02-14 09:47:53 -08:00
Gabor Horvath
068815c2a3 [cxx-interop] Remove some duplicated lookups
Repeatedly lookup up a key from a dictionary can be justified whenever
the content of the dictionary might change between the lookups (so any
references into the dictionary might get invalidated). We had a couple
of instances where as far as I can tell no such modifications should
happen between two lookups with identical keys. This PR simplifies the
code to remove the extra lookups. It also removes a dictionary that was
completely unused.
2025-02-14 11:04:23 +00:00
Becca Royal-Gordon
2b2252b6a6 Gracefully handle incorrect SwiftName arg count
…at least in the specific case of initializers.

Normally, clang validates that a SwiftNameAttr’s specified name seems to have the right number of argument labels for the declaration it’s applied to; if it doesn’t, it diagnoses and drops the attribute. However, this checking isn’t perfect because clang doesn’t know all of Clang Importer’s throwing rules. The upshot is that it’s possible to get a mismatched SwiftNameAttr into Clang Importer if the last parameter looks like an out parameter. This trips an assertion in asserts compilers, but release compilers get past that and don’t seem to notice the mismatch at all.

Add code to the compiler to tolerate this condition, at least in initializers, by modifying the Swift name to have the correct number of argument labels and deprecating the declaration with a message explaining the problem.

Fixes rdar://141124373.
2025-02-11 17:37:25 -08:00
Becca Royal-Gordon
12d0458eb2 Diagnose and forbid invalid Swift names on inits
Initializers should always have Swift names that have the special `DeclBaseName::createConstructor()` base name. Although there is an assertion to this effect in the constructor for ConstructorDecl, ClangImporter did not actually reject custom Swift names for initializers that violated this rule. This meant that asserts compilers would crash if they encountered code with an invalid `swift_name` attribute, while release compilers would silently accept them (while excluding these decls from certain checks since lookups that were supposed to find all initializers didn’t find them).

Modify ClangImporter to diagnose this condition and ignore the custom Swift name.
2025-02-11 12:05:17 -08:00
Gabor Horvath
e79e04c0a6 [cxx-interop] Mark C++ reference parameters @addressable
C++ code can return values that depend on the storage that backs the
references that were passed in as argument. Thus, swift should not
introdue temporary copies of that storage before invoking those
functions as they could result in lifetime issues.
2025-02-11 11:10:51 +00:00
Anthony Latsis
34f9b80cbc Merge pull request #78750 from AnthonyLatsis/oryza-sativa
[Gardening] Fix some set but not used variables
2025-01-31 04:29:05 +00:00
John Hui
be73254cdc [cxx-interop] Import private members (#78942)
This commit removes the guardrails in ImportDecl.cpp:SwiftDeclConverter
that prevent it from importing non-public C++ members. It also
accordingly adjusts all code that assumes generated Swift decls should
be public. This commit does not import non-public inherited members;
that needs its own follow-up patch.

Note that Swift enforces stricter invariants about access levels than C++.
For instance, public typealiases cannot be assigned private underlying types,
and public functions cannot take or return private types. Meanwhile,
both of these patterns are supported in C++, where exposing private types
from a class's public interface is considered feature. As far as I am aware,
Swift was already importing such private-containing public decls from C++
already, but I added a test suite, access inversion, that checks and
documents this scenario, to ensure that it doesn't trip any assertions.
2025-01-30 14:50:15 -08:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
Gábor Horváth
109e2081b6 Merge pull request #78807 from swiftlang/gaborh/generated-ctors
[cxx-interop] Fix spurious lifetime dependence errors
2025-01-29 17:59:03 +00:00
Gábor Horváth
8462105e29 Merge pull request #78947 from swiftlang/gaborh/lifetimebound-this 2025-01-29 09:09:53 +00:00
Gabor Horvath
06e5ead8da [cxx-interop] Support borrowing from self in SwiftifyImport
Support adding safe wrappers for APIs returning std::span depending on
the this object. This also fixes an issue for APIs with 0 parameters.

rdar://139074571
2025-01-28 13:54:10 +00:00