Commit Graph

61 Commits

Author SHA1 Message Date
Allan Shortlidge da5ee4c315 Observation: Suppress a NoUsage warning.
Even though `trackingLists.remove(_:)` returns a discardable result,
`withCriticalRegion` forwards that return value as its own return value, which
the compiler then flags as unused.
2026-05-12 10:56:36 -07:00
Philippe Hausler 9d6a4f17bc [Observation] Partial revert of the dirty bit tracking (#88167)
This is a partial revert to the dirty bit tracking since that has caused
runtime failures for existing applications. It may be resurrected later
once we can isolate the actual impact to behavior.
2026-03-30 16:33:17 -07:00
Philippe Hausler 9fbfc123d8 [Observation] Correct the dirty bit tracking such that only accessed dirty properties contribute to same isolation change events (#88045)
The internal dirty bit tracking state incorrectly flagged all changes as
participation when the mutation during observation occurred. This lead
to un-releated mutations filtering into same-isolation based changes
causing extra events to occur.
2026-03-25 10:04:13 -07:00
Valeriy Van 04a61e36ca Fix typos in Observations.swift 2026-03-07 12:15:42 +01:00
Konrad `ktoso` Malawski 19c914ef3f [Concurrency] nonsending+typed-throw: with(Checked/Unsafe)(Throwing)Continuation (#84944)
This is the minimal set of changes from
https://github.com/swiftlang/swift/pull/80753 to specifically address
the with...Continuation APIs re-enqueueing tasks when they need not have
to.

Resolves rdar://162192512

---

Before, 10 enqueues in total in the task executor case:

```
         1: === foo() async
         2: ---------------------------------------
         3: [executor][task-executor] Enqueue (1)
         4: foo - withTaskExecutorPreference
         5: [executor][task-executor] Enqueue (2)
         6: foo - withTaskExecutorPreference - withCheckedContinuation
         7: [executor][task-executor] Enqueue (3)
         8: foo - withTaskExecutorPreference - withCheckedContinuation done
         9: [executor][task-executor] Enqueue (4)
        10: foo - withTaskExecutorPreference - withUnsafeContinuation
        11: [executor][task-executor] Enqueue (5)
        12: foo - withTaskExecutorPreference - withUnsafeContinuation done
        13: [executor][task-executor] Enqueue (6)
        14: foo - withTaskExecutorPreference - withCheckedThrowingContinuation
        15: [executor][task-executor] Enqueue (7)
        16: foo - withTaskExecutorPreference - withCheckedThrowingContinuation done
        17: [executor][task-executor] Enqueue (8)
        18: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation
        19: [executor][task-executor] Enqueue (9)
        20: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation done
        21: [executor][task-executor] Enqueue (10)
        22: foo - withTaskExecutorPreference done
        23: == Make: actor Foo
        24: ---------------------------------------
        25: [executor][actor-executor] Enqueue (1)
        26: actor.foo
        27: actor.foo - withCheckedContinuation
        28: actor.foo - withCheckedContinuation done
        29: actor.foo - withUnsafeContinuation
        30: actor.foo - withUnsafeContinuation done
        31: actor.foo - withCheckedThrowingContinuation
        32: actor.foo - withCheckedThrowingContinuation done
        33: actor.foo - withUnsafeThrowingContinuation
        34: actor.foo - withUnsafeThrowingContinuation done
        35: actor.foo done
        36: done
```

After, two total enqueues in the task executor:

```
    1: === foo() async
    2: ---------------------------------------
    3: [executor][task-executor] Enqueue (1)
    4: foo - withTaskExecutorPreference
    5: foo - withTaskExecutorPreference - withCheckedContinuation
    6: foo - withTaskExecutorPreference - withCheckedContinuation done
    7: foo - withTaskExecutorPreference - withUnsafeContinuation
    8: foo - withTaskExecutorPreference - withUnsafeContinuation done
    9: foo - withTaskExecutorPreference - withCheckedThrowingContinuation
   10: foo - withTaskExecutorPreference - withCheckedThrowingContinuation done
   11: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation
   12: foo - withTaskExecutorPreference - withUnsafeThrowingContinuation done
   13: [executor][task-executor] Enqueue (2)
   14: foo - withTaskExecutorPreference done
   15: == Make: actor Foo
   16: ---------------------------------------
   17: [executor][actor-executor] Enqueue (1)
   18: actor.foo
   19: actor.foo - withCheckedContinuation
   20: actor.foo - withCheckedContinuation done
   21: actor.foo - withUnsafeContinuation
   22: actor.foo - withUnsafeContinuation done
   23: actor.foo - withCheckedThrowingContinuation
   24: actor.foo - withCheckedThrowingContinuation done
   25: actor.foo - withUnsafeThrowingContinuation
   26: actor.foo - withUnsafeThrowingContinuation done
   27: actor.foo done
   28: done
```
2026-02-27 14:33:57 +09:00
indextrown 3b1c0fb80d [stdlib] Fix documentation typo in Observations.swift 2026-02-13 23:34:21 +09:00
Philippe Hausler a344941374 Advanced observation tracking (#86719)
This is the implementation for
https://github.com/swiftlang/swift-evolution/blob/main/proposals/0506-advanced-observation-tracking.md
and additionally two bug fixes around termination events.

This adds two new entry points for tracking observations. One new
one-shot api that has a new options parameter for controlling events and
one new continuous form that is a callback version of Observations.

Bug fixes:

Previously `Observations` had a very small but still present window of
opportunity during deinitialization to miss an event and leave the
AsyncSequence never emitting a final event but never finishing.
Primarily this could occur when a weakly referenced `@Observable` type
was deinitialized from another isolation than the observation itself.
This current implementation leverages the new options parameter to
account for the deinitailization.

Both `Observations` and `withObservationTracking` where susceptible to a
very small race condition where there was a window of opportunity of a
secondary isolation to mutate a tracked property while the setup of the
observation was being called. Self isolation mutation during the setup
cannot be reported since that would distinctly cause recursive failures
in both observation based code but also code like SwiftUI using it so
the same isolation must be ignored, however external isolation changes
have now been addressed by verifying the tracking lists against the
potential "dirty-ness" of a property. This fixes
https://github.com/swiftlang/swift/issues/83359.
2026-02-12 19:55:21 -08:00
Alex Martini ad13c9ff6a Fix typos in documentation 2026-02-06 13:52:27 -08:00
Doug Gregor 09c71a29f8 Drop extraneous isolation argument 2026-01-14 15:57:39 -08:00
Alastair Houghton 8172fde478 [Observation] We should be importing Android, not Bionic.
Apparently the right module is `Android`, not `Bionic`.  The latter
is the Clang module, with no Swift overlay.
2025-11-12 16:54:10 +00:00
Alastair Houghton 052a1ae2fe [Observation] Add missing dependencies.
There should be additional dependencies listed for Observation,
as it's importing various platform modules.
2025-11-12 13:06:52 +00:00
Michael Gottesman 48b253d111 [concurrency] Use the new builtins. 2025-11-05 20:44:50 -08:00
Philippe Hausler 229c36e476 Revert "[Observation] ensure event triggers on deinitialization passes as if all properties that are being observed have changed (for weak storage) (#79823)" (#83436)
This reverts commit 9d1d917091.
2025-10-06 12:50:27 -07:00
Valeriy Van c53d71bb2a Fix typos 2025-10-04 12:53:44 +02:00
Anthony Latsis a482f37944 Merge pull request #83650 from muukii/patch-1
Fix typo in Observations.swift
2025-08-16 13:23:58 +01:00
Eric Miotto 4da993f368 Observation: do not import all of Darwin to implement locking facilities
At the same time, drop that dependency in the new build system, so that
we don't add an additional linkage for Darwin platforms compared to what
we are doing in the current build system.

Addresses rdar://158313871
2025-08-14 08:35:06 -07:00
Hiroshi Kimura db7717f411 Update Observations.swift 2025-08-12 06:15:56 +09:00
Stephen Canon c24bae70e8 Fixup Observations availability from 9999 to 6.2 (#82198) 2025-06-12 09:10:24 -04:00
Philippe Hausler d9c73d303e [Observation] Initial implementation of Observed for transactional tracked values over time (#79817)
This is an implementation for the feature
https://github.com/swiftlang/swift-evolution/pull/2726
2025-06-10 12:22:23 -07:00
Evan Wilde 1a546a9ccb [FreeBSD]: Optional observation pthread primitives
The pthread APIs on FreeBSD do not include nullability APIs so the
pthread mutex APIs are imported as pointers to optional values.
Other platforms include nullability APIs and import the pthread APIs as
pointers to the mutex. Splitting the locking primitive type based on the
OS.

Fixes: rdar://150880976
2025-05-09 10:08:35 -07:00
Philippe Hausler 3f157ab712 [Observation] Use independent locking since the runtime functions reference a symbol that is not emitted (#81185)
The runtime functions for locking end up referencing a symbol that is
not publicly emitted. This leads to broken linux builds.
Since that interface is not public and does not offer a stable mechanism
and Synchronization cannot be used (due to it being a higher deployment
target) instead it has to revert to implementing locking from scratch
for all platforms.

This is mostly replicated from the swift-async-algorithms package and
should be usable enough to accomplish the goals of this module.

Resolves rdar://150060874
2025-05-01 17:30:50 -07:00
Philippe Hausler 9d1d917091 [Observation] ensure event triggers on deinitialization passes as if all properties that are being observed have changed (for weak storage) (#79823)
* [Observation] ensure event triggers on deinitialziation passes as if all properties that are being observed have changed (for weak storage)

* Add missing deinitialize method for synthetically triggering willSet

* Correct the weak location for tests

* Correct the test to actually test the deinitialization willSet trigger instead of testing weak value deinitialization time

* Refine the tests for deinit triggers to more tightly trigger deinitialization and weak references

* Correct missing trailing closure on deinit replacement

* Ensure all potential ids are triggered at the deinitialization edge trigger
2025-04-07 13:03:56 -07: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
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
Philippe Hausler 54b3a9b64a [Observation] Optimize cancellation path to avoid excessive copies (#73288) 2024-04-30 14:40:02 -07:00
Mishal Shah af112c1591 Update the Swift version to 6.0 from 5.11 2024-02-19 17:47:16 -08:00
Philippe Hausler bd3c15e64d Add a utility method for identifying which property changed for tracking events (#71223) 2024-02-01 09:48:31 -08:00
Eric Miotto 28e2f079b5 CMake: on Darwin be explicit about inclusion in the dyld shared cache (#70856)
This entails passing a linker flags to Apple linkers when the standard
library is not meant for inclusion in such cache.

For this to have effect on every library, propagate link flags when
building _Concurrency and Observation.

This is needed for Apple internal configurations.

Addresses rdar://120653968
2024-01-29 09:42:38 -08:00
Philippe Hausler ed23973e89 [Observation] Add generation for _modify to fields such that we avoid extra CoW (#71122) 2024-01-25 22:51:38 -08:00
Kirby Turner 73ded0f8c4 Update documentation links to the Observable macro and protocol. 2023-07-26 09:52:29 -04:00
Nate Cook 5b9d68b023 Merge branch 'main' into observation_peer_macros 2023-07-19 16:05:43 -05:00
Philippe Hausler 7d37cdf982 [Observation] Tracking adjustments for stability and correctness (#67233)
* [Observation] Correct tracking such that recursive but disperate changes can be tracked without crashing

* [Observation] Adjust the SPI interface for tracking to support deferred cancellation of events and handle both willSet and didSet events
2023-07-18 11:06:43 -07:00
Nate Cook c5fbb4210e [Obs] Switch to peer instead of arbitrary macros
This adopts `@attached(peer)` for generating an observable type's
underscored storage when expanding the `@ObservationTracked` macro,
instead of using `arbitrary` with the member macro on the
observable type.
2023-07-13 14:30:33 -05:00
Nate Cook 003b335351 [Observation] Switch Observable to be a non-marker protocol (#66993)
With support for redundant conformance declarations via macros,
the `Observable` protocol can be a non-marker protocol, which
provides more flexibility for evolution in the future.

rdar://111463883

This change also switches to the new ExtensionMacro protocol,
the requirement for which includes information about whether the
conformance to the Observable protocol has already been added, either
in the declaration or in a superclass to the macro-attributed type.
This allows the @Observable macro to be applied to subclasses of
observable types without redundant-conformance errors.
2023-07-09 13:09:14 -05:00
Nate Cook 40a7396b03 Make the @Observable macro class only (#67033)
* Make ObservationRegistrar Codable/Hashable

These conformances enable automatic Codable synthesis for Observable
types, and smooth the runway for structs being supported by the
Observable macro in the future.

* Limit Observable macro to classes

This removes the ability for the Observable macro to apply to structs,
and adds diagnostic tests for the three disallowed declaration kinds.
2023-07-06 15:01:01 -05:00
Doug Gregor 6265f0c542 [SE-0395] Rename _Observation module to Observation
The review of SE-0395 is down to small details at this point that won't
affect the overall shape of the API much. Rename the model in
anticipation of that.
2023-06-30 11:01:02 -07:00
Philippe Hausler c5018f32c1 [Observation] Ensure lock storage rounding works for non-integral lock types (#66832) 2023-06-22 09:19:00 -07:00
Kirby Turner 367a7f6453 Replace fenced code blocks with indentions. 2023-06-14 17:50:57 -04:00
Kirby Turner 837f6e0999 Fix typo. 2023-06-14 17:40:57 -04:00
Kirby Turner 410b77024a Apply suggested coding style. 2023-06-14 16:37:50 -04:00
Kirby Turner 37e6741dd2 Apply review feedback. 2023-06-14 13:53:59 -04:00
Kirby Turner bf5ecf00ce Add reference documentation to Observation symbols. 2023-06-14 13:26:48 -04:00
Doug Gregor 1209ef89ec Ensure that macros within init accessors are expanded early enough
Now that we've made accessor macro expansion more lazy, ensure that
when querying for init accessors (e.g., to build a memberwise
initializer), we also expand any accessor macros that might produce an
init accessor.

This is a partial step toward the real goal, which is that
`AbstractStorageDecl::getAccessor()` should lazily expand macros if
needed.

Update the Observable macro to document that it produces an `init`
accessor.
2023-06-11 08:48:43 -07:00
Doug Gregor a72fb83034 Requestify AbstractStorageDecl::hasStorage().
The `hasStorage()` computation is used in many places to determine the
signatures of other declarations. It currently needs to expand accessor
macros, which causes a number of cyclic references. Provide a
simplified request to determine `hasStorage` without expanding or
resolving macros, breaking a common pattern of cycles when using
macros.

Fixes rdar://109668383.
2023-06-10 08:28:06 -07:00
Philippe Hausler 4d1d8a9de5 [Observation] Add property definite initialization support (#65984)
* [Observation] Transition to peer macros instead of arbitrary members

* [Observation] Lift the initializer requirement by utilizing init accessors for fully formed definite initialization

* [Observation] Gate enabling of peer macros by flag

* [Observation] Enable feature for InitAccessors in the observation tests

* [Observation] Add tests to validate memberwise and definite initialization
2023-06-09 15:15:39 -07:00
Philippe Hausler d37b6bc074 [Observation] Reduce observation scope to only apply to tracking cases and re-work the macro to utilize more robust emissions (#65528) 2023-05-09 09:28:25 -07:00
Philippe Hausler 7fd2f52af8 [Observation] Refactor for state machine (and behavior robustness) and adjust protocol requirement names (#64414)
* [Observation] Change visibility of observation runtime functions to be hidden

* [Observation] Update API requirements for Observable AsyncSequence names and alter the behavior of value emissions to be based upon transactionality

* [Observation] Slight naming alteration of isolation -> isolatedTo

Note: This re-enables the previously disabled tests since the implementation should be considerably more robust to hangs.
2023-03-16 22:11:23 -07:00
Philippe Hausler 1e4e7f7655 [Observation] Update the SPI entries for SwiftUI for direct tracking access (#64198) 2023-03-08 09:04:54 -08:00
Philippe Hausler 81aa9b51ff [Observation] Add some behaviroal tests for changes, transactions, and tracking (#64179)
* [Observation] Add some behaviroal tests for changes, transactions, and tracking

* Correct transactions to properly suspend when awaiting for changes
2023-03-08 09:04:08 -08:00
Doug Gregor 72357c7aea Disable the _StringProcessing import in the Observation module
Fixes rdar://106374161
2023-03-07 12:39:55 -08:00