Commit Graph

31 Commits

Author SHA1 Message Date
Henrik G. Olsson
6a9ac22cd2 Merge pull request #84507 from hnrklssn/swiftify-import-as-method
[Swiftify] Add support for free functions imported as instance methods

(cherry picked from commit ed4f058c01)
2025-10-07 20:12:44 -07:00
Gábor Horváth
99f9c318ca [6.2][StrictMemorySafety] Check the safety of return types of calls
Explanation: There were some scenarios where we could call an unsafe
function without marking the expression as unsafe. These affect mostly
cases where the function's result is passed to another function or
returned. This PR makes sure we always flag functions with unsafe return
types, even if their result is not stored anywhere for later use.
Issues: rdar://157237301
Original PRs: #83520
Risk: Low, worst case scenario the user has to add redundant unsafe
keywords in strict memory safe mode.
Testing: Added a compiler test.
Reviewers: @DougGregor
2025-08-13 11:49:22 +01:00
Doug Gregor
5568fb25fc Update tests for Span back-deployment 2025-07-07 23:02:55 -07:00
Tim Kientzle
3bacc327e9 Merge pull request #82186 from finagolfin/droid
[6.2][android] Fix the remaining tests and add a build flag that enables standalone cross-compilation SDKs
2025-06-23 11:41:14 -07:00
Gábor Horváth
4590d5551b [6.2][cxx-interop] Only swiftify template instantiations behind type aliases
Explanation: C++ template instantiations that are not behind type
aliases don't have corresponding Swift names that are both syntactically
and semantically valid types. This PR prevents generating swiftified
overloads for those types.
Issue: rdar://151422108
Risk: Low, we swiftify functions less often.
Testing: Regression test added.
Original PR: #81973
Reviewer: @hnrklssn
2025-06-12 19:05:36 +01:00
Meghana Gupta
8d3f38623d [NFC] Update tests and diagnostics 2025-06-11 13:15:22 -07:00
finagolfin
6611ea97e9 [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-06-11 21:52:04 +05:30
Henrik G. Olsson
f148b8d73b [Swiftify] Always annotate overloads with @_disfavoredOverload (#81579)
Previously we would only add @_disfavoredOverload if the only type
changed was the return type, because in any other case it is unambiguous
which overload to call. However it is still ambiguous when storing the
function as a value rather than calling the function, unless explicit
type annotations are used.

To avoid breaking any existing code, this patch adds
@_disfavoredOverload to every overload generated by @_SwiftifyImport.

rdar://151206394
(cherry picked from commit 0f312adb92)
2025-05-30 18:20:02 -07:00
Henrik G. Olsson
5c735c93a8 [Swiftify] Escape param decl refs (#81550)
Parameters can be named with keywords without escaping, because it's
unambiguous in the grammar that they are parameters. They still need to
escaped when referred to inside the function body however. This escapes
all references to parameters using backticks.

Parameter names are also checked for clashes with the function name - in
such cases the parameter is renamed in the same way as unnamed
parameters.

rdar://151024645
(cherry picked from commit ebe2c60f43)
2025-05-30 18:20:00 -07:00
Henrik G. Olsson
8584d343cb [Swiftify] Update availability for CxxSpan<->Span, fix lifetimebound on parameters with reference type (#81634)
Update availability for CxxSpan<->Span, fix lifetimebound on parameters
with reference type

Because swift-ide-test doesn't care about typechecking,
std-span-interface.swift passed despite containing 2 separate errors.
This updates the test file to properly exercise the entire compilation
pipeline for the macro expansions, by running swift-frontend
-emit-module and calling each macro expansion.

The first issue was that CxxSpan initializers taking [Mutable]Span still
had their availability set to Swift 6.2+, even after back-deploying
caused [Mutable]Span to have availability back to Swift 5.0. Since
_SwiftifyImport expansions copy the availbility of Span, this resulted
in the macro expansions calling unavailable initializers. Interestingly
enough, this manifested itself in the form of a tripped assert in SIL
verification, because although we do now typecheck the expansions from
_SwiftifyImport, the compilation can still keep going after
`shouldEmitFunctionBody` returns false: the macro expansion declaration
is still there, but is now missing its definition, despite not being
external.

The second issue was when parameters with C++ reference types were
annotated with `[[clang::lifetimebound]]`. For parameters with a type
that is `Escapable`, this is normally done using `@lifetime(borrow
foo)`. However C++ reference parameters are imported as `inout`, which
requires the `@lifetime(&foo)` syntax.

rdar://151493400
rdar://151678415
(cherry picked from commit 262a53f599)
2025-05-30 18:19:57 -07:00
Henrik G. Olsson
341ee5124a [Swiftify] Copy doc comment from clang node (#81584)
Swift nodes imported from clang don't have doc comments carried over,
but IDEs are clever enough to fetch the comments from the associated
clang node. The swift node in the macro expansion from _SwiftifyImport
doesn't have a clang node directly associated with it however.

This patch adds the same comment from the clang node to the
_SwiftifyImport macro invocation node. Since the macro has access to
this node, it can easily copy over its leading trivia.

For now the comment is not altered at all, meaning @param still remains
even if the parmeter is removed.

rdar://151346977
(cherry picked from commit 6534b9b14f)
2025-05-29 00:20:07 -07:00
Henrik G. Olsson
f70676a340 [Swiftify] Emit @availability when expansions contain Span (#81320)
This prevents errors when compiling for older targets using a newer
compiler.

rdar://150740330
(cherry picked from commit 59d7d3160f)
2025-05-08 16:40:20 -07:00
Henrik G. Olsson
87f5309144 [Swiftify] enable mutable span (#80387)
* [Swiftify] Emit Mutable[Raw]Span when possible

Previously wrappers would use UnsafeMutable[Raw]Pointer for mutable
pointers, and Span for non-const std::span, to prevent the compiler from
complaining that MutableSpan didn't exist.

Now that MutableSpan has landed we can finally emit MutableSpan without
causing compilation errors. While we had (disabled) support for MutableSpan
syntax already, some unexpected semantic errors required additional
changes:
 - Mutable[Raw]Span parameters need to be inout (for mutation)
 - inout ~Escapable paramters need explicit lifetime annotations
 - MutableSpan cannot be directly bitcast to std::span, because it is
   ~Copyable, so they need unwrapping to UnsafeMutableBufferPointer

rdar://147883022

* [Swiftify] Wrap if-expressions in Immediately Called Closures

When parameters in swiftified wrapper functions are nullable, we use
separate branches for the nil and nonnil cases, because
`withUnsafeBufferPointer` (and similar) cannot be called on nil.
If-expressions have some limitations on where they are allowed in the
grammar, and cannot be passed as arguments to a function. As such, when
the return value is also swiftified, we get an error when trying to
pass the if-expression to the UnsafeBufferPointer/Span constructor.
While it isn't pretty, the best way forward seems to be by wrapping the
if-expressions in Immediately Called Closures.

The closures have the side-effect of acting as a barrier for 'unsafe':
unsafe keywords outside the closure do not "reach" unsafe expressions
inside the closure. We therefore have to emit "unsafe" where unsafe
expressions are used, rather than just when returning.

rdar://148153063
2025-03-29 05:05:01 -07:00
Andrew Trick
64a48d08e1 Update tests for strict @lifetime type checking 2025-03-19 11:59:04 -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
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
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
Henrik G. Olsson
ad426fbc47 [Swiftify] Emit Span for counted_by return values with lifetime info
__counted_by return values with .lifetimeDependence are now mapped to
Span instead of UnsafeBufferPointer. Also fixes bug where std::span
return values would map to Span even if lifetime dependence info was
missing.
2025-02-21 20:30:58 -08:00
Henrik G. Olsson
d43ff2db15 [ClangImporter] Enable parsing bounds safety attributes in C++
This allows combining __counted_by and std::span for safe interop.
Previously we disabled this in C++ mode due to issues when bounds
attributes occurred directly or indirectly in templated contexts, but
this has now been resolved on the clang side.
2025-02-21 20:13:29 -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
Gábor Horváth
788d0f9f64 Merge pull request #79396 from swiftlang/gaborh/fix-lifetime-errors-in-macros
[cxx-interop] Work around lifetime errors in SwiftifyImport generated code
2025-02-15 15:29:12 +00:00
Doug Gregor
3c8ef82963 Enable usable of Span by default
Usage of Span was temporarily behind an experimental feature flag. Now
that SE-0447 has been accepted, remove the experimental feature flag and
allow Span usage everywhere.

Implements rdar://144819992.
2025-02-14 10:45:47 -08:00
Gabor Horvath
df27b79579 [cxx-interop] Work around lifetime errors in SwiftifyImport generated code
Unfortunately, this was not discovered earlier as swift-ide-test is not
invoking the SIL passes that produce this diagnostic. When creating
Swift spans from C++ spans we have no lifetime dependency information to
propagate as C++ spans are modeled as escapable types. Hence, this PR
introduces a helper function to bypass the lifetime checks triggered by
this discepancy. Hopefully, the new utility will go away as the lifetime
analysis matures on the Swift side and we get standardized way to deal
with unsafe lifetimes.
2025-02-14 16:40:43 +00:00
Gábor Horváth
7eada7dd86 Merge pull request #79012 from swiftlang/gaborh/disfavoredOverload
[cxx-interop] Avoid generating ambiguous wrapper functions
2025-01-29 19:28:42 +00:00
Gabor Horvath
a57aff04c9 [cxx-interop] Avoid generating ambiguous wrapper functions
When we generate a safe wrapper that only differs in the return type we
might introduce ambiguities as some callers might not have enough
information to disambiguate between the overloads. This PR makes sure
the newly generated declarations are marked as @_disfavoredOverload so
the compiler can keep calling the old functions without a source break
when the feature is turned on.

rdar://139074571
2025-01-29 15:19:30 +00:00
Gabor Horvath
dd3db51f60 [cxx-interop] Make borrowing specifiers more precise
We do not need to borrow from view objects passed by value but we need
to borrow from owners taken by const reference regardless of whether it
was annotated using lifetimebound or lifetime_capture_by.
2025-01-29 11:49:32 +00:00
Gábor Horváth
44fa6506c3 Merge pull request #78945 from swiftlang/gaborh/wrapper-method
[cxx-interop] Add safe wrapper test with a member function
2025-01-29 11:46:35 +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
Gabor Horvath
da3be5d99d [cxx-interop] Add safe wrapper test with a member function 2025-01-27 12:45:58 +00:00
Gabor Horvath
042b108e6f [cxx-interop] Make ClangImporter support lifetimebound annotated spans
Generate safe Swift Span wrappers using the new SwiftifyImport macro.

rdar://139074571
2025-01-22 15:07:02 +00:00
Gabor Horvath
4846c56795 [cxx-interop] Generate safe overloads for non-escapable spans
A previous PR already added support to the SwiftifyImport macro to
generate safe wrappers. This PR makes ClangImporter emit the macro to do
the transformation.
2025-01-08 11:19:35 +00:00