Commit Graph

21434 Commits

Author SHA1 Message Date
Doug Gregor
3e726ebc55 Add nextElement() and explicit Failure types for a number of asynchronous iterators 2024-01-25 16:04:44 -08:00
Doug Gregor
909257ecd0 Implement nextElement() on most async sequences that have rethrowing next()
Start implementing `nextElement()` for the various async sequences
provided by the concurrency library, starting with those that
currently depend on `rethrows` with conformances, an undocumented
feature we're trying to stage out of the language.

With the exception of `AsyncFlatMapSequence`, all of these async
sequences have obvious implementations of `nextElement()`. We'll save
that one for later.
2024-01-25 16:04:44 -08:00
Doug Gregor
239f8d8a78 Rename AsyncIteratorProtocol._nextElement -> nextElement 2024-01-25 16:04:43 -08:00
Doug Gregor
b2a5ebe1bd Always emit AsyncIteratorProtocol._nextElement into the client
... this allows us to use the entrypoint when back-deploying code that
uses the async for..in loop.
2024-01-25 16:04:43 -08:00
Doug Gregor
bb7a563e6c Switch async for-each loop over to _nextElement and drop @rethrows.
This couples together several changes to move entirely from
`@rethrows` over to typed throws:

* Use the `Failure` type to determine whether an async for-each loop
will throw, rather than depending on rethrows checking

* Introduce a special carve-out for `rethrows` functions that have a
generic requirement on an `AsyncSequence` or `AsyncIteratorProtocol`,
which uses that requirement's `Failure` type as potentially being part
of the thrown error type. This allows existing generic functions like
the following to continue to work:

    func f<S: AsyncSequence>(_: S) rethrows

* Switch SIL generation for the async for-each loop from the prior
`next()` over to the typed-throws version `_nextElement`.

* Remove `@rethrows` from `AsyncSequence` and `AsyncIteratorProtocol`
entirely. We are now fully dependent on typed throws.
2024-01-25 16:04:43 -08:00
Doug Gregor
a5bdb12b48 Adopt typed throws in AsyncIteratorProtocol and AsyncSequence
Introduce a new associated type `Failure` into the two protocols involved
in async sequences, which represents the type thrown when the sequence
fails. Introduce a defaulted `_nextElement()` operations that throws
`Failure` or produces the next element of the sequence. Provide a
default implementation of `_nextElement()` in terms of `next()` that
force-cases the thrown error to the `Failure` type.

Introduce special associated type inference logic for the `Failure`
type of an `AsyncIteratorProtocol` conformance when there is no
specific _nextElement()` witness. This inference logic looks at the
witness for `next()`:
* If `next()` throws nothing, `Failure` is inferred to `Never`.
* If `next()` throws, `Failure` is inferred to `any Error`.
* If `next()` rethrows, `Failure` is inferred to `T.Failure`, where
`T` is the first type parameter with a conformance to either
`AsyncSequence` or `AsyncIteratorProtocol`.

The default implementation and the inference rule, together, allow
existing async sequences to continue working as before, and set us up
for changing the contract of the `async for` loop to use
`_nextIterator()` rather than `next()`.

Give `AsyncSequence` and `AsyncIteratorProtocol` primary associated
types for the element and failure types, which will allow them to be
used more generally with existential and opaque types.
2024-01-25 16:04:42 -08:00
Serena758
6151d4f23d changed sanity to soundness 2024-01-25 14:22:46 -08:00
Holly Borla
7bcade6cd3 Merge pull request #71143 from hborla/isolation-inheritance
[Concurrency] More precise modeling of `ActorIsolation` for isolated arguments and captures.
2024-01-25 10:11:50 -08:00
Kavon Farvardin
b8cd7effeb Merge pull request #70866 from kavon/ncgenerics-stdlib-building-v4
Build Stdlib with Noncopyable Generics (Part 4)
2024-01-25 07:09:38 -08:00
Mike Ash
4341102c92 [Tools] Add a library to build specialized generic metadata out of process.
This library uses GenericMetadataBuilder with a ReaderWriter that can read data and resolve pointers from MachO files, and emit a JSON representation of a dylib containing the built metadata.

We use LLVM's binary file readers to parse the MachO files and resolve fixups so we can follow pointers. This code is somewhat MachO specific, but could be generalized to other formats that LLVM supports.

rdar://116592577
2024-01-24 20:45:50 -05:00
Holly Borla
f7326618ae [Concurrency] Model actor isolation to an arbitrary actor instance using
a VarDecl or Expr.

This generalization exposed a bug where distributed actor isolation checking
was skipped in some cases, including for the isolated call in `whenLocal`.
The `whenLocal` implementation violated distributed actor isolation because
despite the `__isLocal` dynamic check, the `self` value passed to the `body`
function argument is still not statically local. To workaround this, I
applied the `_local` modifier explicitly to `self` before the call, which
also necessitated allowing `_local` to be written explicitly in the Distributed
library.
2024-01-24 16:11:09 -08:00
Kuba (Brecka) Mracek
302e106e2d Merge pull request #70458 from kubamracek/embedded-random
[embedded] Make RNG APIs available on embedded Swift
2024-01-24 13:14:47 -08:00
Kavon Farvardin
25aa49be55 [NCGenerics] Sendable doesn't require Copyable 2024-01-23 22:42:38 -08:00
Kavon Farvardin
fc9575cda9 [NCGenerics] enable new associated type inference
The experimental-associated-type-inference flag is needed to correctly
build programs when NoncopyableGenerics is enabled.
2024-01-23 22:41:59 -08:00
Kavon Farvardin
b92fe5f2e9 [build-script] fix NoncopyableGenerics option
This patch introduces `--enable-experimental-noncopyable-generics` for
the build script. It replaces
`--swift-stdlib-experimental-noncopyable-generics`

The old build option only enables the feature when building the
stdlib, but if we've built the stdlib with NoncopyableGenerics, the
compiler should be hardwired to have that feature enabled, too.

This patch also introduces the `noncopyable_generics` lit parameter, so
that tests assuming the system was built with the feature can live
in-tree and be tested, if they specify `REQUIRES: noncopyable_generics`.
2024-01-23 22:41:59 -08:00
Doug Gregor
8c448a5b21 Merge pull request #71043 from DougGregor/distributed-actor-to-actor
Introduce a builtin and API for getting the local actor from a distributed one
2024-01-23 13:02:04 -08:00
Doug Gregor
97ea19d191 Introduce a builtin and API for getting the local actor from a distributed one
When an actual instance of a distributed actor is on the local node, it is
has the capabilities of `Actor`. This isn't expressible directly in the type
system, because not all `DistributedActor`s are `Actor`s, nor is the
opposite true.

Instead, provide an API `DistributedActor.asLocalActor` that can only
be executed when the distributed actor is known to be local (because
this API is not itself `distributed`), and produces an existential
`any Actor` referencing that actor. The resulting existential value
carries with it a special witness table that adapts any type
conforming to the DistributedActor protocol into a type that conforms
to the Actor protocol. It is "as if" one had written something like this:

    extension DistributedActor: Actor { }

which, of course, is not permitted in the language. Nonetheless, we
lovingly craft such a witness table:

* The "type" being extended is represented as an extension context,
rather than as a type context. This hasn't been done before, all Swift
runtimes support it uniformly.

* A special witness is provided in the Distributed library to implement
the `Actor.unownedExecutor` operation. This witness back-deploys to the
Swift version were distributed actors were introduced (5.7). On Swift
5.9 runtimes (and newer), it will use
`DistributedActor.unownedExecutor` to support custom executors.

* The conformance of `Self: DistributedActor` is represented as a
conditional requirement, which gets satisfied by the witness table
that makes the type a `DistributedActor`. This makes the special
witness work.

* The witness table is *not* visible via any of the normal runtime
lookup tables, because doing so would allow any
`DistributedActor`-conforming type to conform to `Actor`, which would
break the safety model.

* The witness table is emitted on demand in any client that needs it.
In back-deployment configurations, there may be several witness tables
for the same concrete distributed actor conforming to `Actor`.
However, this duplication can only be observed under fairly extreme
circumstances (where one is opening the returned existential and
instantiating generic types with the distributed actor type as an
`Actor`, then performing dynamic type equivalence checks), and will
not be present with a new Swift runtime.

All of these tricks together mean that we need no runtime changes, and
`asLocalActor` back-deploys as far as distributed actors, allowing it's
use in `#isolation` and the async for...in loop.
2024-01-22 17:27:31 -08:00
Kuba Mracek
2c30b0a0e2 [embedded] Resolve ptrauth crashes by signing HeapObjects's isa pointers in embedded Swift 2024-01-22 16:45:07 -08:00
Tim Kientzle
62f8780bb0 Revert "Obj-C class metatypes should never satisfy Obj-C existentials"
This reverts commit aed05b9a36.

This needs some additional work.  Reverting out until it can be fixed.
2024-01-22 11:27:42 -08:00
Guillaume Lessard
50b83c8e69 Merge pull request #68423 from glessard/se0405-take2
[se-0405] rename `String.init(validatingUTF8:)`
2024-01-22 09:29:21 -08:00
Guillaume Lessard
d8c809c0ad Merge pull request #68419 from glessard/se0405-part1
[se-0405] Implement API additions
2024-01-22 09:27:27 -08:00
Alastair Houghton
3a511e0dc5 Merge pull request #71005 from al45tair/eng/PR-120941628
[RemoteInspection] Pass through null returns from TypeRefVisitor.
2024-01-22 11:25:05 +00:00
Allan Shortlidge
b5a36cfdba Merge pull request #71030 from tshortli/resolve-even-more-warnings
Resolve even more warnings
2024-01-19 21:03:03 -08:00
Konrad `ktoso` Malawski
1dec00a420 [TaskExecutors] Task initializer and withTaskExecutor parameter changes (#70783) 2024-01-20 11:03:26 +09:00
Allan Shortlidge
ca555c8aac NFC: Ignore -Wexit-time-destructors warning in TaskAlloc.cpp. 2024-01-19 15:43:35 -08:00
Allan Shortlidge
64d0f72d98 NFC: Address a warning in swift_task_asyncMainDrainQueueImpl().
The function is marked `noreturn` but the compiler was not able to reason
statically about whether this constraint is met. From code inspection it's
clear that the call to `swift_task_donateThreadToGlobalExecutorUntil()` does
not return, so `swift_unreachable()` can be used to suppress the warning.
2024-01-19 15:43:35 -08:00
Kuba (Brecka) Mracek
e06bbf79aa Merge pull request #70971 from kubamracek/tt-withunsafeptr
Adopt typed throws in one withUnsafePointer() API
2024-01-19 14:23:27 -08:00
nate-chandler
1da24ece36 Merge pull request #71009 from nate-chandler/bitwise-copyable-gating
[BitwiseCopyable] Remove gating from protocol declaration.
2024-01-19 12:39:31 -08:00
Nate Chandler
c49738eecf [BitwiseCopyable] Remove gating from protocol decl
It's harmless to have the protocol defined in the swiftinterface for
older compilers and removing the gating permits the gating to be omitted
from conformances
2024-01-19 07:25:15 -08:00
Alastair Houghton
0bbb270d27 [RemoteInspection] Pass through null returns from TypeRefVisitor.
If `TypeRefVisitor<DemanglingForTypeRef, Demangle,NodePointer>::visit`
returns `nullptr`, we should pass it through.  The callers of
`TypeRef::getDemangling()` are already prepared to cope with `nullptr`
returns, so this should be fine and avoids hitting an assertion
failure in that case.

rdar://120941628
2024-01-19 10:22:02 +00:00
Doug Gregor
8d67a37231 Only declare isolation macro when the compiler supports macros 2024-01-18 18:47:26 -08:00
Doug Gregor
24c2d1a49c Switch #isolation from AnyActor to Actor
We are currently lacking the ability to describe "normal or distributed
actor" with a single protocol in a manner that allows hopping to it,
because `AnyActor` is a marker protocol. Use `Actor` while we sort
this out.
2024-01-18 18:46:47 -08:00
Tim Kientzle
4ea5ed1309 Revert #68952 [Casting] Make more casts look inside __SwiftValue
Turns out that our Obj-C bridging relies on this inconsistent behavior,
so it can quite likely never be actually fixed.
2024-01-17 18:14:57 -08:00
Allan Shortlidge
c06d3ced6a Merge pull request #70953 from tshortli/fragile-regex-parser-module
[Regex] Really build _RegexParser without resilience
2024-01-17 15:28:37 -08:00
Kuba Mracek
43554c4da4 [embedded] Also enable RNG APIs on SIMD types 2024-01-17 14:53:24 -08:00
Kuba Mracek
bd5913a176 [embedded] Make RNG APIs available on embedded Swift 2024-01-17 14:53:24 -08:00
Kuba Mracek
6d2de26691 Adopt typed throws in one withUnsafePointer() API 2024-01-17 14:19:16 -08:00
Alejandro Alonso
72ad6c5c33 Merge pull request #70945 from Azoy/fix-unmanaged-opaque
[stdlib] Avoid materializing strong references in potential dangling unmanaged opaque functions
2024-01-17 09:30:50 -08:00
Kuba (Brecka) Mracek
cf440011cb Merge pull request #70947 from kubamracek/embedded-unicode-scalar
[embedded] Add Unicode.Scalar to embedded stdlib
2024-01-17 09:30:08 -08:00
Allan Shortlidge
8d1b578275 [Regex] Really build _RegexParser without resilience.
This was originally attempted with https://github.com/apple/swift/pull/58810
but `_RegexParser` still ended up being built with library evolution enabled
because the `-enable-library-evolution` flag is added to the command line after
calling `add_swift_target_library` and therefore stripping the flag out of
`SWIFT_COMPILE_FLAGS` does nothing.

The `IS_FRAGILE` flag was introduced to support building C++ interop overlay
modules without library evolution and it can now be used to prevent
`_RegexParser` from being built with library evolution.

Resolves rdar://93067204
2024-01-17 08:03:54 -08:00
Allan Shortlidge
92a0867f08 Merge pull request #70948 from tshortli/resolve-concurrency-runtime-warnings
Resolve warnings in the Concurrency runtime
2024-01-16 18:59:50 -08:00
Kuba (Brecka) Mracek
10ea278590 Merge pull request #70762 from kubamracek/embedded-dict
[embedded] Add Dictionary to embedded stdlib
2024-01-16 18:56:17 -08:00
Allan Shortlidge
05dbd059e1 Merge pull request #70943 from tshortli/resolve-more-warnings
Resolve more warnings
2024-01-16 17:19:51 -08:00
Alejandro Alonso
f0a74ecb0d Apply suggestions from code review
Co-authored-by: Guillaume Lessard <glessard@users.noreply.github.com>

fix test
2024-01-16 17:14:18 -08:00
Allan Shortlidge
8343e7a292 NFC: Suppress -Wreturn-type-c-linkage warnings in GlobalExecutor.cpp. 2024-01-16 16:21:06 -08:00
Allan Shortlidge
f18b196ed8 NFC: Suppress a -Wunreachable-code warning in GlobalExecutor.cpp.
The `swift_unreachable()` is currently statically unreachable but we'd like to
leave it in place in case the control flow before it changes in the future.
2024-01-16 16:21:06 -08:00
Allan Shortlidge
00da82ddd7 NFC: Remove a superfluous if #available from Executor.swift.
Confirmed with Konrad that this was copypasta.
2024-01-16 16:21:06 -08:00
Kuba Mracek
c12d067eac [embedded] Add Unicode.Scalar to embedded stdlib 2024-01-16 16:01:48 -08:00
Alejandro Alonso
e6e87881b8 Take doc suggestion from Karoy #2
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2024-01-16 16:00:04 -08:00
Alejandro Alonso
f28ebc9668 Take doc suggestion from Karoy #1
Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2024-01-16 15:58:34 -08:00