Commit Graph

136 Commits

Author SHA1 Message Date
Henrik G. Olsson
f5fa481205 [Swiftify] Always remove count parameters when possible (#81585)
Previously we did not remove count parameters if any count parameters
were shared between count expressions, or if any count expression
contained operations. Buffer sizes were also just checked to be larger
than or equal than the given count.

We now extract the count from Spans/BufferPointers whenever possible,
and store that value in a variable at the start of the function. If
multiple parameters share the same count, a bounds check is emitted to
make sure that they have the same size. Subspans can be used if one span
is larger than necessary.

The message in the bounds check is changed so that it includes the
expected and actual value, to aid in debugging.

This patch also fixes some incorrect indentation, and adds the
Whitespace.swift test case to act as a regression test in case the
indentation changes, since the other test cases don't use significant
whitespace.

rdar://151488820
rdar://151511090
rdar://146333006
rdar://147715799
2025-05-24 22:08:51 -07:00
Henrik G. Olsson
0f312adb92 [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
2025-05-23 21:21:49 -07:00
Henrik G. Olsson
ebe2c60f43 [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
2025-05-23 12:33:58 -07:00
Henrik G. Olsson
262a53f599 [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
2025-05-22 15:36:19 -07:00
Henrik G. Olsson
526c68333c [Swiftify] Fix __sized_by and nullable return values (#81693)
Nullable return Spans did not include __swiftifyOverrideLifetime,
resulting in a lifetime error when returning the Span. Meanwhile return
values for __sized_by did not use the correct label for the call to the
RawSpan initializer, using `count` instead of `byteCount`.

rdar://151804085
rdar://151799287
2025-05-22 15:34:58 -07:00
Henrik G. Olsson
6534b9b14f [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
2025-05-20 08:06:20 -07:00
Henrik G. Olsson
8e27947b2a [Swiftify] Handle anonymous parameters (#81384)
_SwiftifyImport would expand with syntax errors if applied to a function
with anonymous parameters, because it would try to refer to parameters
using the name `_`. Detect these cases and create names for unnamed
parameters.

rdar://150955944
2025-05-08 23:28:14 -07:00
Henrik G. Olsson
59d7d3160f [Swiftify] Emit @availability when expansions contain Span (#81320)
This prevents errors when compiling for older targets using a newer
compiler.

rdar://150740330
2025-05-08 16:13:24 -07:00
Henrik G. Olsson
bd233ea26e [Swiftify] Don't use count from Span inside withUnsafeBufferPointer call (#81267) 2025-05-04 01:52:05 -07:00
Doug Gregor
fed18d58dc [Observation] Create nonisolated conformances to Observable
This is needed to make default-main-actor mode work well with
Observable. Fixes rdar://150419628.
2025-05-01 22:01:41 -07:00
Philippe Hausler
2381d394db [Observation] forward line numbers to didSet and other property observers from their spelled location to the code-generated duplicated storage location (#80661)
* [Observation] forward line numbers to didSet and other property observers from their spelled location to the code-generated duplicated storage location

* Only emit source locations for the prolog of the body and emit an empty location for the epilog

* Add missing attribute member

* Use the fixed version of the locations since swift-syntax 6.0.1 reports sub-nodes correctly in all contexts
2025-04-18 13:41:21 -07:00
Henrik G. Olsson
4d28cc61c9 [Swiftify] clean up (NFC) (#80400)
* [Swiftify] Extract static methods to free functions (NFC)

This will make the diff smaller with introducing
_SwiftifyImportProtocol.

* [Swiftify] Run swift-format (NFC)

* [Swiftify] Remove `try` from non-throwing expression (NFC)
2025-04-02 21:24:00 -07:00
Allan Shortlidge
82e4e50c93 Distributed: Fix some deprecation warnings in DistributedResolvableMacro. 2025-04-01 13:11:36 -07:00
Allan Shortlidge
119b38e13a Swiftify: Fix 'no calls to throwing function' warnings. 2025-04-01 13:11:28 -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
Henrik G. Olsson
d1737b9c20 [Swiftify] Add MutableSpan support for std::span, and disable it (#80315)
__counted_by already had MutableSpan support, so add it for std::span
for parity. But since MutableSpan hasn't landed in the standard library
yet, disable emitting it to prevent compilation errors in expansions.

rdar://147882736
2025-03-27 16:37:31 -07:00
Henrik G. Olsson
160bb41ec2 [Swiftify] Fix return value transformation being applied last
Casting the return value to Span must be done outside
withUnsafeBufferPointer, to prevent returning a ~Escapable type from a
function without lifetime info. To do this we sort the transformations
so that the return value transformation is performed last. There was
a bug in the comparison, so the sorting was not always done correctly.

rdar://147934170
2025-03-26 13:49:22 -07:00
Henrik G. Olsson
c7af94ba07 [Swiftify] Don't drop bounds checks when mixing std::span and counted_by
When an imported function combines std::span and __counted_by,
std::span would override bounds checks emitted for __counted_by (if it
occurred later in the the parameter list) resulting in no bounds checks
being emitted.

rdar://147883384
2025-03-26 13:48:50 -07:00
Konrad `ktoso` Malawski
8430749e81 [Distributed] Allow @Resolvable to work with available on specific func (#80296)
resolves rdar://147892011
2025-03-26 05:30:51 -07:00
Holly Borla
bac0a10ae2 [Macros] Update the name and argument list for the function body macro
that wraps a function or closure body in a new top-level task.
2025-03-21 06:21:45 -07:00
Holly Borla
be670da373 Merge pull request #79980 from hborla/closure-body-macro
[Macros] Implement support for function body macros on closures.
2025-03-21 06:19:36 -07:00
Andrew Trick
955d089a90 CxxSpanReturnThunkBuilder: use _cxxOverrideLifetime(_:copying:)
Do not rely on the @_unsafeNonescapableResult attribute. That attribute is only
for temporarily working around bugs! And it only affects lifetime diagnostics within
the function. It has no affect on the caller's diagnostics, so it won't solve
this problem:

func macroGeneratedThunk() -> CxxSpan<Int> {
  return _unsafeRemoveLifetime(Span...)
}

We cannot simply add @_unsafeRemoveLifetime to the thunk, because SwiftSyntax
does not natively support the attribute. We don't want to add SwiftSyntax
support because this attribute will never be supported syntax!

Instead, use `_overrideLifetime` copying the `Void` type to remove a dependency:

func macroGeneratedThunk() -> CxxSpan<Int> {
  return _cxxOverrideLifetime(Span..., copying: ())
}
2025-03-19 11:59:05 -07:00
Andrew Trick
339cd56dd4 Fix SwiftifyImportMacro to emit @lifetime(copy). 2025-03-19 11:59:04 -07:00
Holly Borla
31d4f661fc [Macros] Support function body macros on closures. 2025-03-18 20:50:47 -07:00
Konrad `ktoso` Malawski
39687f1e83 [Distributed] Carry SPI attribute to stub types (#79987)
* [Distributed] Carry SPI attribute to stub types

* Cleaner identifier checking
2025-03-18 04:39:04 -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
John Hui
5b4252a9ed [cxx-interop] Remove code from SwiftifyImport that drops _const-suffix (#79756)
This was introduced in 140552054c in an
attempt to be accommodate existing interfaces with the _const scheme,
but we shouldn't need to support such textual interfaces anyway since
the _const suffix only appears in C++ code (which shouldn't be part of
a resilient interface).

For newly generated interfaces, dropQualifierSuffix doesn't do anything
since the code that generates the suffix is now gone.

rdar://143769901
2025-03-05 09:06:46 -08:00
Holly Borla
8be41edf6f [Concurrency] Add a macro for wrapping a function body in a new task. 2025-03-03 20:51:14 -08:00
John Hui
140552054c [cxx-interop] Fix template parameter printing scheme for const types (#79237)
This patch changes the class template printer to disambiguate const-qualified template arguments by wrapping them with __cxxConst<>, rather than suffixing them with _const.

This is necessary to accommodate template arguments that aren't just identifiers (i.e., Foo<Int_const> is ok, but Foo<Bar<T>_const> and Foo<((Bar) -> Baz)_const> are not syntactically valid). With this patch, we would produce Foo<__cxxConst<Int>>, Foo<__cxxConst<Bar<T>>, and Foo<__cxxConst<((Bar) -> Baz)>> instead.

This patch also disambiguates volatile-qualified template arguments with __cxxVolatile<>, and changes the printing scheme for std::nullptr_t from nil to __cxxNullPtrT (since nil is not a syntactically valid type name).

rdar://143769901
2025-02-28 19:41:55 -08:00
Gabor Horvath
ae7e14c329 [cxx-interop] Avoid warnings in strict memory safe mode on in generated code
The generated safe wrappers need to call unsafe code. Make sure those
calls are qualified with unsafe to avoid warnings that are not
actionable (users cannot fix them).
2025-02-28 11:27:21 +00:00
Henrik G. Olsson
df05addb44 Merge pull request #79552 from hnrklssn/swiftify-span-with-counted-by
[ClangImporter][Swiftify] merge importBoundsAttributes with importSpanAttributes and support lifetime bounds for __counted_by

 - [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.

 - [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.

 - [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-24 13:49:17 -08: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
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
Philippe Hausler
a3d699241c [Observation] Reduce the scope of the observation shouldNotifyObservers to only apply to observation and not side effects of accessors like willSet or didSet (#79518) 2025-02-21 11:32:19 -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
Allan Shortlidge
1675098e8c Observation: Fix warnings about unused variables. 2025-02-01 10:28:27 -08:00
Philippe Hausler
854fa5c4e3 [Observation] Disable caching of KeyPaths (#78853)
* [Observation] Disable caching of KeyPaths

* Remove the cached keypath annottion for the ObservationTracked peer macro
2025-01-31 13:50:05 -08: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
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
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
Gábor Horváth
4d24b7d888 Merge pull request #78709 from swiftlang/gaborh/span-lifetimebound-overload
[cxx-interop] Support transforming lifetimebound spans
2025-01-22 14:21:57 +00:00
Philippe Hausler
c6a2437a10 [Observation] Exclude generic class types from pre-caching their keypaths (#78790)
* [Observation] Exclude generic class types from pre-caching their keypaths

* Account for multiple nesting levels of generics for observation cached keypaths
2025-01-21 23:49:37 -08:00
Gabor Horvath
8c03a31d65 [cxx-interop] Support transforming lifetimebound spans
This PR adds basic support for storing lifetime dependence information,
transform Span return types, and generate lifetime annotations.

rdar://139074571
2025-01-21 10:51:04 +00:00
Henrik G. Olsson
d7bd76e9f1 [Swiftify] Add return pointer support (#78571)
* Import __counted_by for function return values

Instead of simply passing a parameter index to _SwiftifyInfo, the
_SwiftifyExpr enum is introduced. It currently has two cases:
 - .param(index: Int), corresponding to the previous parameter index
 - .return, corresponding to the function's return value.

ClangImporter is also updated to pass this new information along to
_SwiftifyImport, allowing overloads with buffer pointer return types to
be generated. The swiftified return values currently return Span when
the return value is marked as nonescaping, despite this not being sound.
This is a bug that will be fixed in the next commit, as the issue is
greater than just for return values.

* Fix Span variant selection

There was an assumption that all converted pointers were either
converted to Span-family pointers, or UnsafeBufferPointer-family
pointers. This was not consistently handled, resulting in violating the
`assert(nonescaping)` assert when the two were mixed. This patch removes
the Variant struct, and instead each swiftified pointer separately
tracks whether it should map to Span or UnsafeBufferPointer.
This also fixes return pointers being incorrectly mapped to Span when
marked as nonescaping.
2025-01-13 08:08:36 -08: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
Allan Shortlidge
67a9b81e3e SwiftifyImport: Fix warnings.
Fixes the following warnings:

```
warning: no calls to throwing functions occur within 'try' expression
warning: default will never be executed
```
2025-01-07 08:12:41 -08:00
Henrik G. Olsson
e252cbbaeb [SwiftifyImport] Add sizedBy support for OpaquePointer (#78315)
This makes it possible to mark a pointer with __sized_by when the
pointee type definition is not included. The wrapper function has the
same interface as if the parameter were a void pointer, since the stdlib
has no `OpaqueBufferPointer` type.

* use swift-ide-test for checking interop signatures
* add xfail test for Span + Optional combo (Optional requires Escapable)
2025-01-03 10:09:49 -08:00
Gábor Horváth
6e84b8f507 Merge pull request #78352 from swiftlang/gaborh/cxx-span-overload 2025-01-03 15:32:39 +01:00
Gabor Horvath
b0bb995209 [cxx-interop] Estend _SwiftifyImport with basic std::span support
This is a preliminary PR to transform nonescaping std::span parameters
to Swift's Span type in safe wrappers. To hook this up with
ClangImporter, we will need generalize the noescape attribute to
non-pointer types (PR is already in review). To transform potentially
escaping spans and spans in the return position, a follow-up PR will
add lifetime annotation support. This is a building block towards
rdar://139074571.
2024-12-23 14:08:10 +00:00
Henrik G. Olsson
e87c1c33e1 Add missing colon in _SwiftifyImport function call (#78316) 2024-12-20 17:51:58 +01:00