Commit Graph

61 Commits

Author SHA1 Message Date
Michael Gottesman
48b253d111 [concurrency] Use the new builtins. 2025-11-05 20:44:50 -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
Kyle
7c6cc140ad Fix TaskLocal macro link issue 2024-12-04 15:17:10 +08:00
Kyle
a3e8d1068f Fix missing curly brace in TaskLocal documentation 2024-12-04 15:16:42 +08:00
Allan Shortlidge
686ef29e55 Concurrency: Avoid string interpolation in argument to fatalError().
In Embedded Swift, the `String` type is unavailable. This means that the
overload of `fatalError()` that takes a `String` is also unavailable. Specify a
`StaticString` in the setter for `TaskLocal.projectedValue` to satisfy this
constraint.
2024-11-01 13:03:29 -07:00
Allan Shortlidge
ba3cd79b6f Concurrency: Remove superflous _SwiftConcurrencyShims imports.
The `_SwiftConcurrencyShims` module was imported `@_implementationOnly` which
was causing warnings to be emitted during the stdlib build. The module
currently serves no purpose; the only declaration it contains is a defunct
`_SwiftContext` struct which is not referenced by anything. The module needs to
continue to exist for source compatibility, though, since it is part of the
toolchain and imported publicly from other modules.
2024-09-09 12:20:13 -07:00
Doug Gregor
a47483c0ec Fix ABI issue with TaskLocal.withValue and update ABI test 2024-07-15 14:37:45 -07:00
Doug Gregor
f6ba1bbeaa Adopt _unsafeInheritExecutor_ workaround on the internal withValueImpl 2024-07-15 14:12:40 -07:00
Doug Gregor
8964436f36 Add _unsafeInheritExecutor_ version of TaskLocal.withValue 2024-07-09 15:38:38 -07:00
Konrad `ktoso` Malawski
0160fc367a [Concurrency] adopt #isolation in last remaining APIs 2024-06-24 11:44:44 +09:00
Konrad `ktoso` Malawski
0c44645832 [Concurrency] Implement defensive copying in task groups, rather than crashing (#73978) 2024-05-31 11:27:03 -07:00
Konrad `ktoso` Malawski
dc5e354d69 [Concurrency] Reimplement @TaskLocal as a macro (#73078) 2024-05-01 20:57:20 -07:00
Konrad `ktoso` Malawski
dfcf1054dd [Concurrency] Remove _unsafeInheritExecutor from public APIs, use #isolation (#72578) 2024-04-05 03:57:54 -07:00
mtj0928
343f8e5df1 [docs] Fix examples for TaskLocal
TaskLocal doesn't support `static let`.
2024-02-10 00:43:31 +09:00
Konrad `ktoso` Malawski
9d3540fd75 [docs] TaskLocal APIs from non-Task code (#70622) 2024-01-15 11:38:01 +09:00
Konrad `ktoso` Malawski
68ccbcee5e [TaskLocal] Remove comment which is true, but is confusing to end users
because it discusses an implementation detail of the type the comment is
on. Instead, when developers read comments they think about what they
can store inside a task local -- so the comment about "task local must
be a class" led them to believe THEIR types must be classes, while the
comment was explaining the implementation detail of `class
TaskLocal<T>`.

Resolves rdar://117982608
2023-11-06 19:02:59 +09:00
Nate Chandler
9583f9c836 [Gardening] Deleted unreachable fatalError.
Silenced a warning.
2023-09-06 17:49:09 -07:00
Allan Shortlidge
5cc7fff4ca Concurrency: Restore consume in TaskLocal.withValueImpl<R>(_:operation:).
It is no longer necessary to avoid using the `consume` keyword in inlinable
function bodies in the standard library in order to support older compilers.

Partially reverts https://github.com/apple/swift/pull/65599.

Resolves rdar://109165937.
2023-08-14 22:19:56 -07:00
Dimitri Bouniol
1544c39989 Added missing documentation to TaskLocal 2023-06-19 14:51:01 -07:00
Allan Shortlidge
860b8cdf48 Concurrency: Revert workarounds for @backDeployed condfails.
All compilers that must be able to compile the standard library's textual
interface accept the following:
- back deployed functions declared without `@available`
- `@inlinable` back deployed functions
- `defer` blocks in back deployed functions

We can therefore revert the workarounds added in
https://github.com/apple/swift/pull/62946/ and
https://github.com/apple/swift/pull/62928/.

Resolves rdar://104085810
2023-05-08 18:21:08 -07:00
Allan Shortlidge
c0ee3ac866 Concurrency: Make _Concurrency module interface parsable by older compilers.
Revert a few unnecessary changes have been made to the _Concurrency module
recently that make its swiftinterface un-parseable by older compilers that we
need to support.

- The `consume` keyword is not understood by older compilers, so including it
  in inlinable code is a problem. It has no actual effect on lifetimes where it
  was used, so just omit it.
- Don't build the _Concurrency module with -enable-experimental-feature
  MoveOnly. The MoveOnly feature is now enabled by default in recent compilers,
  so the flag is superfluous when building the dylib. When emitting the
  interface, having the feature enabled explicitly exposes code guarded by
  `$MoveOnly` to older compilers that do not accept all of the code.

Resolves rdar://108793606
2023-05-02 15:27:05 -07:00
Nate Chandler
4fe988b7c2 [Concurrency] Nest stack traffic in withValue.
Because `_taskLocalValuePush` and `_taskLocalValuePop` can result in calls
to `swift_task_alloc` and `swift_task_dealloc` respectively, and because
the compiler hasn't been taught about that (e.g.
`SILInstruction::isAllocatingStack`,
`SILInstruction::isDeallocatingStack`, etc), calling them (push and pop)
from a function which makes use the stack for dynamically sized
allocations can result in violations of stack discipline of the form

```
swift_task_alloc // allocates %ptr_1
copy_value_witness // copies into %ptr_1
swift_task_localValuePush // calls swift_task_alloc and allocates %ptr_2
swift_task_dealloc // deallocates %ptr_1
swift_task_localValuePop // calls swift_task_dealloc and deallocates %ptr_2
```

Avoid the problem by not allocating dynamically sized stack space in the
function which calls `_taskLocalValuePush` and `_taskLocalValuePop`.
Split the calls to those functions into `withValueImpl` function which
takes its argument `__owned`.  Call that function from `withValue`,
ensuring that the necessary copy (to account for the fact that withValue
takes its argument `__guaranteed` but `_taskLocalValuePush` takes its
`__owned`) and associated stack traffic occur in `withValue`.

Still, allow `withValueImpl` to be inlined.  The stack nesting will be
preserved across it.

rdar://107275872
2023-04-18 19:51:34 -07:00
Allan Shortlidge
087aacc9fd [TaskLocals] Avoid use of defer in back deployed functions in the standard library.
Older versions of the 5.8 compiler have a bug when generating SIL for functions with `@_backDeploy` containing defer blocks (https://github.com/apple/swift/pull/62444) and for now we need the standard library interface to be compatible with those older compilers.

Resolves rdar://104045168
2023-01-10 10:44:15 -08:00
Allan Shortlidge
c7c806f768 [TaskLocals] Remove use of @inlinable with @_backDeploy temporarily. 2023-01-09 10:59:53 -08:00
Konrad `ktoso` Malawski
f9c2bc0402 Apply suggestions from code review
Co-authored-by: Doug Gregor <dgregor@apple.com>
2022-12-13 10:39:46 +09:00
Konrad `ktoso` Malawski
342ce7ea19 cleaning up attributes 2022-12-12 12:02:31 +09:00
Konrad `ktoso` Malawski
d122fd68fa add _backDeploy and availability necessary for it 2022-12-09 17:46:34 +09:00
Konrad `ktoso` Malawski
b3c335798d Better fix, using inheriting executor 2022-12-09 10:22:48 +09:00
Konrad `ktoso` Malawski
2178d9893f [Concurrency] tasklocal withValue inside actor is safe 2022-12-09 10:22:47 +09:00
Doug Gregor
60c9fcbba6 [Concurrency] Use #fileID instead of #file so we don't get full paths.
Fixes rdar://93246032.
2022-05-13 15:07:04 -07: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
51e4fd2342 Address a few more warnings in the concurrency library 2021-08-26 10:56:48 -07:00
Doug Gregor
a8d119a6de [SE-0311] Add Sendable requirement to TaskLocal.
This is part of SE-0311 that was not implemented.
Fixes rdar://77441933.
2021-08-16 14:19:08 -07:00
Konrad `ktoso` Malawski
ea146ac5d4 [Concurrency,docs] Update task local docs to use latest structured concurrency APIs (#38499) 2021-07-21 07:57:11 +09:00
Konrad `ktoso` Malawski
6cbb792f92 [TaskLocals] Propagate task-locals through async{} 2021-05-11 11:06:17 +09:00
Konrad `ktoso` Malawski
82e91b7785 [TaskLocals] Enable sync functions to bind task-locals; Keep Storage in TLS 2021-05-11 11:06:16 +09:00
Alexis Laferrière
10d115bd5c [Concurrency] Use the SwiftStdlib 5.5 macro instead of 9999 versions 2021-05-04 09:30:58 -07:00
Konrad `ktoso` Malawski
1cd7180382 [Concurrency] don't require Sendable (yet) on groups and locals 2021-05-03 10:52:06 +09:00
Konrad `ktoso` Malawski
2fc1ed8f50 [TaskLocals] rename 'do body' to 'operation' parameter 2021-04-30 09:38:31 +09:00
Konrad `ktoso` Malawski
38a43a481e [TaskLocals] Enforce @TaskLocal may only be used on static props 2021-04-29 21:44:48 +09:00
Konrad `ktoso` Malawski
070f9a773b [TaskLocals] review 2: projected value wrapper 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
df5ff42d79 [TaskLocals] set task local value in synchronous function 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
f0781b1f8b [TaskLocals] prettier API thanks to default inits 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
6f3dac190a [TaskLocals] remove TaskLocalInheritance, we'll introduce when needed 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
664936cd03 [TaskLocal] crash on illegal withValue 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
58ea749304 [TaskLocal] Crash on inapropriate use within task group 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
3d96d05546 [TaskLocals] property wrapper keys 2021-04-29 15:48:09 +09:00
Konrad `ktoso` Malawski
e0eb940027 [Concurrency] Remove Task.current because it prevents task-local alloc #36993 2021-04-26 17:37:45 +09:00