Commit Graph

147 Commits

Author SHA1 Message Date
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
Henrik G. Olsson
ef9d2b744d Rename pointer bounds (#78210)
* Make pointer bounds non-experimental

* Rename @PointerBounds to @_SwiftifyImport

* Rename filenames containing PointerBounds

* Add _PointerParam exception to stdlib ABI test

* Add _PointerParam to stdlib API changes

* Rename _PointerParam to _SwiftifyInfo
2024-12-20 11:36:01 +01:00
Philippe Hausler
8462f5c90b [Observation] Optimize the storage of registrar entries, provide KeyPath caching, and uniqueness notification (#78151) 2024-12-16 13:13:19 -08:00
Allan Shortlidge
b1d7ccca0f SwiftMacros: Remove unnecessary .as() cast from DistributedResolvableMacro.
Resolves the following warning:

```
 warning: 'as' is deprecated: This cast will always succeed
```
2024-12-13 11:10:48 -08:00
Konrad `ktoso` Malawski
4da5b64021 [DistributedResolvableMacro] cannot use if expressions 2024-12-03 14:59:16 +09:00
Konrad `ktoso` Malawski
138f145ab9 [Distributed] @Resolvable now handles primary associated types in protocols
Previously we would not propagate those into the generated distributed
actor, making a lot of generic distributed actor protocols impossible to
express.

We indeed cannot handle protocols WITHOUT primary associated types, but
we certainly can handle them with!

This resolves rdar://139332556
2024-12-03 14:59:15 +09:00
Henrik G. Olsson
0678829cf7 Add @PointerBounds macro (#76969)
Add @PointerBounds macro

@PointerBounds is a macro intended to be applied by ClangImporter when
importing functions with pointer parameters from C headers. By
leveraging C attributes we can get insight into bounds, esapability, and
(eventually) lifetimes of pointers, allowing us to map them to safe(r)
and more ergonomic types than UnsafePointer.

This initial macro implementation supports CountedBy and Sizedby, but
not yet EndedBy. It can generate function overloads with and without an
explicit count parameter, as well as with UnsafeBufferPointer or Span
(if marked nonescaping), and any of their combinations. It supports
nullable/optional pointers, and both mutable and immutable pointers.
It supports arbitrary count expressions. These are passed to the macro
as a string literal since any parameters referred to in the count
expression will not have been declared yet when parsing the macro.

It does not support indirect pointers or inout parameters. It supports
functions with return values, but returned pointers can not be bounds
checked yet.

Bounds checked pointers must be of type Unsafe[Mutable]Pointer[?]<T>
or Unsafe[Mutable]RawPointer[?]. Count expressions must conform to
the BinaryInteger protocol, and have an initializer with signature
"init(exactly: Int) -> T?" (or be of type Int).

rdar://137628612

---------

Co-authored-by: Doug Gregor <dgregor@apple.com>
2024-11-11 14:54:25 -08:00
Alejandro Alonso
eed695d679 Add some ASTGen tests 2024-11-06 13:56:08 -08:00
Alejandro Alonso
ba3bc1d5f1 Implement ASTGen changes for integer generics from Swift Syntax
Support negatives
2024-11-06 13:54:59 -08:00
Allan Shortlidge
5257747b1b Macros: Implement expansion(of:providingMembersOf:conformingTo:in:) on MemberMacros.
Resolves a warning about using a deprecated default implementation.
2024-10-16 16:35:54 -07:00
Allan Shortlidge
977ef19ec7 ObservationMacros: Suppress a unreachable default warning. 2024-10-16 16:33:14 -07:00
Dave Lee
b3c59aceda [Debug] Escape LLDB syntax in debugDescription (#75306)
When using `@DebugDescription`, only allow use of [LLDB Summary 
Strings](https://lldb.llvm.org/use/variable.html#summary-strings) syntax from 
`lldbDescription` properties. When `@DebugDescription` is applied to existing 
`debugDescription` properties, escape any `$`, as the output of `debugDescription` is 
never interpreted by LLDB.

Depends on #75305
2024-07-30 10:58:49 -07:00
Dave Lee
abe697ff72 [Debug] Rename DebugDescription fallback property to lldbDescription (#75305)
From feedback, replace the name `_debugDescription`, which was confusing because of the 
underscore, with `lldbDescription`. This new name also indicates that this property may 
contain [LLDB Summary Strings](https://lldb.llvm.org/use/variable.html#summary-strings).
2024-07-29 11:10:43 -07:00
Dave Lee
e0129799fa [Debug] Add user friendly diagnostic when @DebugDescription is atached to a generic (#75307)
Emit a user friendly diagnostic when `@DebugDescription` is attached to a generic 
definition.
2024-07-25 10:06:47 -07:00
Hamish Knight
955c56c7c1 [Macros] Remove SwiftOperators dependency 2024-07-20 15:14:49 +01:00
Rintaro Ishizaki
1fb78394ce [Macros] Remove unused dependency to SwiftOperators
ObservationMacros and SwiftMacros don't use anything in SwiftOperators
2024-06-17 15:39:03 -07:00
Konrad `ktoso` Malawski
c42ecb0908 [Distributed] Remove "Stdlib" in macro as it is not understood in user code (#74417) 2024-06-14 23:03:33 +09:00
Konrad `ktoso` Malawski
0454c1be7a [TaskLocal] avoid some return type, since CI is using old Swift 2024-05-17 16:01:04 +09:00
Konrad `ktoso` Malawski
1c522b0312 [TaskLocal] TL macro must handle force unwrapped optional types
Resolves rdar://128225191
2024-05-17 11:12:02 +09:00
Konrad `ktoso` Malawski
b77685efe0 [Distributed] Remove diagnostic emitting from macro, rely on compiler
The macro cannot diagnose some situations, or rather, would diagnose too
aggressively, because it cannot inspect the type declarations of all
invokved types, and therefore we're unable to reliably report errors
only when necessary.

Originally I thought we don't want to emit macro code that "may fail to
compile" but we don't really have a choice.

This patch removes a manual diagnostic, and enables more correct code to
properly use @Resolvable protocols.
2024-05-15 16:12:50 +09:00
Konrad `ktoso` Malawski
7224178b85 [TaskLocal] Carry access control modifiers into synthesized property (#73475) 2024-05-07 20:13:23 +09:00
Konrad `ktoso` Malawski
dc5e354d69 [Concurrency] Reimplement @TaskLocal as a macro (#73078) 2024-05-01 20:57:20 -07:00
Pavel Yaskevich
e6b445b8d1 [SE-0428] NFC: Rename _DistributedProtocol macro into Resolvable
The proposal was [accepted with modifications](https://forums.swift.org/t/accepted-with-modifications-se-0428-resolve-distributedactor-protocols/71366)

The decision was made to change the spelling of the attached macro
in the proposal from @DistributedProtocol to `@Resolvable`
(or `@Distributed.Resolvable` if disambiguation is needed).
2024-04-22 14:18:11 -07:00
Konrad `ktoso` Malawski
5df0f053d9 [Distributed][Macro] Improve diagnostics, handle edge cases, properties, assoc types (#73046) 2024-04-18 12:38:02 +09:00
treastrain / Tanaka Ryoga
a1c49d0139 @Observable Macro supports properties with the package access modifier (#71061)
* `@Observable` Macro supports properties with the `package` access modifier #71060

* Cherry-pick "Move the tests for package scopes to the module interface tests" 4e274ce0a5

---------

Co-authored-by: Philippe Hausler <phausler@apple.com>
2024-04-09 13:32:03 -07:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
Hamish Knight
780308abe0 Merge pull request #72309 from hamishknight/add-dep
[cmake] Add missing dependency SwiftMacros -> SwiftOperators
2024-03-18 17:28:56 +00:00
Hamish Knight
dd8c7faf3c [cmake] Add missing dependency SwiftMacros -> SwiftOperators 2024-03-13 21:26:49 +00:00
Allan Shortlidge
28b274c6b3 Observation: Stop using deprecated SwiftSyntax APIs.
Addresses a few warnings about use of `as()`:

```
warning: 'as' is deprecated: This cast will always succeed
```
2024-03-13 10:16:49 -07:00