Commit Graph

40 Commits

Author SHA1 Message Date
Alex Martini
903e9c4d7c Refer to checked APIs in CheckedContinuation overview 2025-11-03 14:53:30 -08:00
Gabor Horvath
402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
Doug Gregor
050a514588 [Strict memory safety] Update standard library for unsafe treated as a call effect 2025-04-25 21:54:23 -07:00
Doug Gregor
177d16ced8 Enable strict memory safety in the Concurrency module 2025-02-26 14:28:24 -08:00
Kuba Mracek
ed441eb1bf [Concurrency] Include more pieces of Concurrency in Embedded (AsyncStream, continuations)
We have been only including a subset of files and functionality on Embedded Concurrency, let's instead include all the
source files, and have a fine grained opt out on things that don't yet work. Namely, this is still avoiding clocks, task
sleeping and custom executors.

Add a test for AsyncStream and continuations on Embedded Concurrency.
2024-10-02 09:22:36 -07:00
Konrad `ktoso` Malawski
f300164acc [Concurrency] Improve crash message on continuation misuse (#75613) 2024-08-02 00:46:45 -07:00
Doug Gregor
a2038f92f8 Address code review feedback and fix rebase typo in a test 2024-07-09 08:13:24 -07:00
Doug Gregor
a2b2324e7f Use @_unsafeInheritExecutor forms of with*Continuation from @_unsafeInheritExecutor functions
The move from `@_unsafeInheritExecutor` to `#isolation` for the
with*Continuation breaks code that is using `@_unsafeInheritExecutor` and
calling these APIs. This originally caused silent breakage (which manifest
as runtime crashes), and is now detected by the compiler as an error.

However, despite `@_unsafeInheritExecutor` being an unsafe,
not-intended-to-be-user-facing feature, it is indeed being used, along
with these APIs. Introduce _unsafeInheritExecutor_-prefixed versions of
the `with*Continuation` and `withTaskCancellationHandler` APIs into
the _Concurrency library that use `@_unsafeInheritExecutor`. Then,
teach the type checker to swap in these
_unsafeInheritExecutor_-prefixed versions in lieu of the originals
when they are called from an `@_unsafeInheritExecutor` function. This
allows existing code using `@_unsafeInheritExecutor` with these APIs
to continue working as it has before, albeit with a warning that
`@_unsafeInheritExecutor` has been removed.

Fixes rdar://131151376.
2024-07-08 23:35:37 -07:00
Alexander Cyon
c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +02:00
Michael Gottesman
ce1dd43395 [sending] Change CheckedContinuation/AsyncThrowingStream.Continuation APIs to use sending parameters and results.
Specifically:

1. CheckedContinuation.resume now takes a sending parameter.
2. Async{Throwing,}Stream.yield takes a sending parameter.
3. withCheckedContinuation returns a transferring parameter.

Importantly due to the previous changes around mangling, this is a mangling
neutral change.

rdar://120420024
2024-06-01 23:25:16 -07:00
Konrad `ktoso` Malawski
dfcf1054dd [Concurrency] Remove _unsafeInheritExecutor from public APIs, use #isolation (#72578) 2024-04-05 03:57:54 -07:00
Kuba Mracek
90e1d2006f [embedded] Add support for actors and async let into the embedded Concurrency runtime 2023-10-09 22:43:50 -07:00
Pavel Yaskevich
6e48d449e8 [SILGen] NFC: Introduce intrinsics for construction and resumption of CheckedContinuation 2023-09-07 17:35:38 -07:00
Alex Martini
2676e261fd Fix typo; add missing word. 2023-08-07 11:35:42 -07:00
Konrad `ktoso` Malawski
c446f4cb12 review followup 2023-03-03 13:37:57 +09:00
Konrad `ktoso` Malawski
8b2253cfbe [Concurrency] Fix continuation documentation about execution semantics 2023-03-03 09:45:55 +09:00
nate-chandler
9a7c0b9869 Revert "[Freestanding] Disable checked continuations." 2022-09-14 17:29:49 -07:00
Nate Chandler
c20bd86007 [Freestanding] Disable checked continuations.
In the task-to-thread model, there are no threading mechanisms by which
work could be offloaded onto another thread.  As such, callback based
asynchronous APIs which are not Swift async do not make sense.

rdar://99047747
2022-08-25 16:55:45 -07:00
Doug Gregor
ee4f6b1f75 Make CheckedContinuation and UnsafeContinuation unconditionally Sendable.
The continuation types were conditionally `Sendable` based on whether
the result type of the continuation was `Sendable`. However,
conceptually, the return value is never leaving the current task, so
it is never actually crossing concurrency boundaries. Therefore, we
can make the continuation types unconditionally `Sendable`.

Fixes rdar://98462858.
2022-08-19 13:06:41 -07:00
John McCall
84c74941dc Make with*Continuation inherit the caller's executor.
Not inheriting the caller's executor is a major problem for
these functions.  Under SE-0338, treating them as non-isolated
means that it's illegal to pass them anytthing non-Sendable
from a different isolation context; since the function is
declared to take a non-sendable function parameter, effectively,
SE-0338 means that these functions can only be safely called
from non-isolated contexts.  That's not really acceptable, but
it gets worse: since we haven't implemented the sendability
rule for that yet, we're immediately bypassing isolation safety
when using these functions with no warning.

The `withCheckedContinuation` functions are not
`@_alwaysEmitIntoClient` (an oversight in the initial release),
and so we need to use the unsafe attribute on them.  When we
eventually implement a safe mechanism for this, we should make
`@_alwaysEmitIntoClient` variants of these functions which use
the new feature, and we can demote the existing functions to
`internal @availableFromInline`.

I've gone ahead and made `withChecked*Continuation` `@inlinable`.
2022-02-14 20:46:03 -05:00
Šimon Javora
6c0c39ec44 [Docs] withCheckedThrowingContinuation body fix
`withCheckedThrowingContinuation`'s `body` says that it takes an `UnsafeContinuation`... seems like a copy/paste error.
2021-12-20 12:28:26 +01:00
najacque
57ad1f1c7d Merge pull request #40115 from amartini51/concurrency_docs_to_main
Cherry-pick concurrency docs
2021-12-07 11:59:56 -08:00
Doug Gregor
5a99ae485e Make (Unsafe|Checked)Continuation's Sendable conformance conditional.
Make the continuation type's conformances to the `Sendable` protocol
conditional on the sendability of the result yielded when the
resumption is performed. This ensures that one cannot silently escape
a continuation's result out of a task or actor, closing a safety hole
in Sendable checking.

Fixes rdar://85419546.
2021-11-15 16:41:07 -08:00
Doug Gregor
353daabf8d Replace UnsafeSendable with @unchecked Sendable in the standard library. 2021-11-12 07:56:10 -08:00
Alex Martini
c7aec3d6b7 Cherry pick Swift 5.5 doc changes in CheckedContinuation.swift 2021-11-09 17:23:38 -08:00
Karoy Lorentey
47956908b7 [Concurrency] SwiftStdlib 5.5 ⟹ SwiftStdlib 5.1 (usages)
The concurrency runtime now deploys back to macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, which corresponds to the 5.1 release of the stdlib.

Adjust macro usages accordingly.
2021-10-28 14:36:36 -07:00
Doug Gregor
60763a07e2 CheckedContinuation, UnsafeContinuation, and UnownedJob are Sendable
Fixes rdar://82909334.
2021-09-29 09:51:44 -07:00
Robert Widmann
15a552f11e [Gardening] Match Parameter Names To Doc Comments
Keep these names in sync so the documentation for these parameters
actually shows up in the IDE.
2021-06-24 20:30:44 -07:00
Alexis Laferrière
10d115bd5c [Concurrency] Use the SwiftStdlib 5.5 macro instead of 9999 versions 2021-05-04 09:30:58 -07:00
Mike Ash
1173b737aa [Concurrency] Add availability to Concurrency APIs.
This allows programs to target older OSes while using Concurrency behind an availability check. When targeting older OSes, the symbols are weak-linked and the compiler will require the use of Concurrency features to be guarded by an availability check.

rdar://75850003
2021-04-01 10:42:08 -04:00
Nate Cook
86b69b98cc Fix license headers in concurrency source files 2021-03-25 10:12:02 -05:00
Jonathan Grynspan
7e7215459f Remove _Continuation protocol and duplicate the relevant code. 2021-02-11 12:54:19 -05:00
Jonathan Grynspan
d8f7f20c4b [Concurrency] Simplify the type story for Continuation by eliminating Throwing variants
The goal of doing this is to reduce the amount of boilerplate and repeated code w.r.t. Continuation. Having just added `resume()` in four places, I got the sense that there was a lot of common code that was being duplicated. I removed the Throwing variants of these types (they can be expressed as Continuation<T, E:Error> instead of ThrowingContinuation<E>) and I broke out a significant amount of common code between CheckedContinuation and UnsafeContinuation into an implementation-only protocol to avoid repeating it. D.R.Y.

This change resolves rdar://74154769.
2021-02-10 12:09:33 -05:00
Jonathan Grynspan
6bddba2272 Merge pull request #35829 from grynspan/main
[Concurrency] Add `*Continuation.resume()` for continuations returning `Void`.

This change resolves rdar://74031110.
2021-02-09 12:16:05 -05:00
Jonathan Grynspan
49da0783ef Mark new function inlinable since it's trivial 2021-02-08 18:01:12 -05:00
Jonathan Grynspan
5db55aad1e Add *Continuation.resume() for continuations returning Void.
This change implements the changes proposed in swift-evolution PR #1264.
Existing test coverage should be sufficient here since the added function
simply calls into the existing `resume(returning:)` function.

This change resolves rdar://74031110.
2021-02-08 14:37:27 -05:00
Joe Groff
35802bfa36 CheckedContinuation: Add resume(with: Result) for parity with UnsafeContinuation 2021-02-05 11:52:45 -08:00
Joe Groff
49d4299ae0 Concurrency: Have CheckedContinuation trap on double resume. 2021-01-12 16:08:41 -08:00
Joe Groff
5f0974515d Make CheckedContinuation thread-safe and resilient. 2021-01-08 14:54:27 -08:00
Joe Groff
c151f4b02b Concurrency: Introduce a CheckedContinuation adapter.
To help catch runtime issues adopting `withUnsafeContinuation`, such as callback-based APIs that misleadingly
invoke their callback multiple times and/or not at all, provide a couple of classes that can take ownership of
a fresh `UnsafeContinuation` or `UnsafeThrowingContinuation`, and log attempts to resume the continuation multiple times
or discard the object without ever resuming the continuation.
2021-01-08 13:50:42 -08:00