File: distributed_actor_isolation_and_tasks.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (74 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend-emit-module -emit-module-path %t/FakeDistributedActorSystems.swiftmodule -module-name FakeDistributedActorSystems -disable-availability-checking %S/Inputs/FakeDistributedActorSystems.swift
// RUN: %target-swift-frontend -typecheck -verify -disable-availability-checking -I %t 2>&1 %s
// REQUIRES: concurrency
// REQUIRES: distributed

import Distributed
import FakeDistributedActorSystems

@available(SwiftStdlib 5.5, *)
typealias DefaultDistributedActorSystem = FakeActorSystem

struct SomeLogger {}
struct Logger {
  let label: String
  func info(_: String) {}
}

distributed actor Philosopher {
  let log: Logger
  // expected-note@-1{{access to property 'log' is only permitted within distributed actor 'Philosopher'}}

  var variable = 12
  var variable_fromDetach = 12
  let INITIALIZED: Int
  let outside: Int = 1

  init(system: FakeActorSystem) {
    self.log = Logger(label: "name")
    self.INITIALIZED = 1
  }

  distributed func dist() -> Int {}

  func test() {
    _ = self.id
    _ = self.actorSystem
    Task {
      _ = self.id
      _ = self.actorSystem

      self.log.info("READY!")
      _ = self.variable
      _ = self.dist()
    }

    Task.detached {
      _ = self.id
      _ = self.actorSystem

      // This is an interesting case, since we have a real local `self` and
      // yet are not isolated to the same actor in this detached task...
      // the call to it is implicitly async, however it is NOT implicitly throwing
      // because we KNOW this is a local call -- and there is no system in
      // between that will throw.
      _ = await self.dist() // notice lack of 'try' even though 'distributed func'
      _ = self.variable_fromDetach // expected-error{{expression is 'async' but is not marked with 'await'}}
      // expected-note@-1{{property access is 'async'}}
      _ = await self.variable_fromDetach // okay, we know we're on the local node
    }
  }
}

func test_outside(system: FakeActorSystem) async throws {
  _ = try await Philosopher(system: system).dist()
  _ = Philosopher(system: system).log // expected-error{{distributed actor-isolated property 'log' can not be accessed from a nonisolated context}}

  _ = Philosopher(system: system).id
  _ = Philosopher(system: system).actorSystem
}

func test_outside_isolated(phil: isolated Philosopher) async throws {
  phil.log.info("works on isolated")
}