File: moveonly_modify_coroutine.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 (101 lines) | stat: -rw-r--r-- 2,848 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// RUN: %target-swift-frontend -DADDRESS_ONLY -emit-sil -verify %s
// RUN: %target-swift-frontend -DLOADABLE -emit-sil -verify %s
// RUN: %target-swift-frontend -DTRIVIAL -emit-sil -verify %s
// RUN: %target-swift-frontend -DEMPTY -emit-sil -verify %s

class X {}

struct NC: ~Copyable {
#if EMPTY
#elseif TRIVIAL
  var x: Int = 0
#elseif LOADABLE
  var x: X = X()
#elseif ADDRESS_ONLY
  var x: Any = X()
#else
#error("pick a mode")
#endif
  deinit { print("destroy") }
}

struct S {
  var data: NC {
    _read { yield NC() }
    _modify { var nc = NC(); yield &nc }
  }
}

struct SNC: ~Copyable {
  private var _data: NC = NC()

  var data: NC {
    _read { yield _data }
    _modify { yield &_data }
  }
}

class C {
  private var _data: NC = NC()

  var data: NC {
    _read { yield _data }
    _modify { yield &_data }
  }
}

protocol P {
  @_borrowed
  var data: NC { get set }
}

func borrow(_ nc: borrowing NC) {}
func take(_ nc: consuming NC) {}
func mod(_ nc: inout NC) {}

func test(c: C) {
  borrow(c.data)
  // TODO: Not an inout parameter, should use a "cannot be consumed" error message
  take(c.data) // expected-error{{field 'c.data' was consumed but not reinitialized; the field must be reinitialized during the access}} expected-note{{consumed here}}
  mod(&c.data)
}
func test(s: S) {
  borrow(s.data)
  take(s.data) // expected-error{{'s.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
}
func test(m_s s: inout S) {
  borrow(s.data)
  take(s.data) // expected-error{{field 's.data' was consumed but not reinitialized; the field must be reinitialized during the access}} expected-note{{consumed here}}
  mod(&s.data)
}
func test(snc: borrowing SNC) {
  borrow(snc.data)
  take(snc.data) // expected-error{{'snc.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
}
func test(m_snc snc: inout SNC) {
  borrow(snc.data)
  take(snc.data) // expected-error{{field 'snc.data' was consumed but not reinitialized; the field must be reinitialized during the access}} expected-note{{consumed here}}
  mod(&snc.data)
}

func test<T: P>(t: T) {
  borrow(t.data)
  take(t.data) // expected-error{{'t.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
}
func test<T: P>(m_t t: inout T) {
  borrow(t.data)
  take(t.data) // expected-error{{field 't.data' was consumed but not reinitialized; the field must be reinitialized during the access}} expected-note{{consumed here}}
  mod(&t.data)
}

func test(p: P) {
  borrow(p.data)
  take(p.data) // expected-error{{'p.data' is borrowed and cannot be consumed}} expected-note{{consumed here}}
}
func test(m_p p: inout P) {
  borrow(p.data)
  take(p.data) // expected-error{{field 'p.data' was consumed but not reinitialized; the field must be reinitialized during the access}} expected-note{{consumed here}}
  mod(&p.data)
}