File: explicit_lifetime_dependence_specifiers1.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 (234 lines) | stat: -rw-r--r-- 8,201 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// RUN: %target-typecheck-verify-swift -disable-availability-checking -enable-experimental-feature NonescapableTypes
// REQUIRES: asserts

struct Container {
  let ptr: UnsafeRawBufferPointer
}

struct AnotherBufferView : ~Escapable {
  let ptr: UnsafeRawBufferPointer
  @_unsafeNonescapableResult
  init(_ ptr: UnsafeRawBufferPointer) {
    self.ptr = ptr
  }
}

struct BufferView : ~Escapable {
  let ptr: UnsafeRawBufferPointer
  @_unsafeNonescapableResult
  init(_ ptr: UnsafeRawBufferPointer) {
    self.ptr = ptr
  }
  init(_ ptr: UnsafeRawBufferPointer, _ arr: borrowing Array<Int>) -> dependsOn(arr) Self {
    self.ptr = ptr
    return self
  }
  init(_ ptr: UnsafeRawBufferPointer, _ arr: borrowing Array<Double>) -> dependsOn(arr) Self {
    self.ptr = ptr
  }
  // TODO: Once Optional is ~Escapable, the error will go away
  init?(_ ptr: UnsafeRawBufferPointer, _ arr: borrowing Array<Float>) -> dependsOn(arr) Self? { // expected-error{{lifetime dependence can only be specified on ~Escapable results}}
    if (Int.random(in: 1..<100) == 0) {
      return nil
    }
    self.ptr = ptr
  }
  init?(_ ptr: UnsafeRawBufferPointer, _ arr: borrowing Array<String>) -> dependsOn(arr) Self? { // expected-error{{lifetime dependence can only be specified on ~Escapable results}}
    if (Int.random(in: 1..<100) == 0) {
      return nil
    }
    self.ptr = ptr
    return self
  }
  init(_ ptr: UnsafeRawBufferPointer, _ arr: borrowing Array<Character>) -> dependsOn(self) Self { // expected-error{{invalid lifetime dependence on self in an initializer}}
    self.ptr = ptr
  }
  init(_ ptr: UnsafeRawBufferPointer, _ arr: Array<Bool>) -> dependsOn(scoped arr) Self { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
    self.ptr = ptr
  }
  init(_ ptr: UnsafeRawBufferPointer, _ arr: Array<UInt>) -> dependsOn(arr) Self { // expected-error{{invalid use of lifetime dependence on an Escapable parameter with consuming ownership}}
    self.ptr = ptr
  }
  init(_ ptr: UnsafeRawBufferPointer, _ abv: AnotherBufferView) -> dependsOn(abv) Self {
    self.ptr = ptr
  }

  consuming func consume() -> dependsOn(scoped self) BufferView { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
    return BufferView(self.ptr)
  }

  func get() -> dependsOn(self) Self { // expected-note{{'get()' previously declared here}}
    return self
  }

  func get() -> dependsOn(scoped self) Self { // expected-error{{invalid redeclaration of 'get()'}}
    return self
  }
}

struct MutableBufferView : ~Escapable, ~Copyable {
  let ptr: UnsafeMutableRawBufferPointer
  @_unsafeNonescapableResult
  init(_ ptr: UnsafeMutableRawBufferPointer) {
    self.ptr = ptr
  }
}

class Klass {}

struct WrapperStruct {
  let k: Klass
}

func invalidLifetimeDependenceOnEscapableResult(_ w: borrowing WrapperStruct) -> dependsOn(w) Klass { // expected-error{{lifetime dependence can only be specified on ~Escapable results}}
  return w.k
}

func incorrectSelfInvalidLifetimeDependence(_ x: borrowing BufferView) -> dependsOn(self) BufferView { // expected-error{{invalid lifetime dependence specifier on non-existent self}}
  return BufferView(x.ptr)
}

func incorrectParamNameInvalidLifetimeDependence(_ x: borrowing BufferView) -> dependsOn(y) BufferView { // expected-error{{invalid parameter name specified 'y'}}
  return BufferView(x.ptr)
}

func duplicateParamInvalidLifetimeDependence1(_ x: borrowing BufferView) -> dependsOn(x, x) BufferView { // expected-error{{duplicate lifetime dependence specifier}}
  return BufferView(x.ptr)
}

func duplicateParamInvalidLifetimeDependence2(_ x: borrowing BufferView) -> dependsOn(x) dependsOn(x) BufferView { // expected-error{{duplicate lifetime dependence specifier}}
  return BufferView(x.ptr)
}

func duplicateParamInvalidLifetimeDependence3(_ x: borrowing BufferView) -> dependsOn(x) dependsOn(x) BufferView { // expected-error{{duplicate lifetime dependence specifier}}
  return BufferView(x.ptr)
}

func consumingParamInvalidLifetimeDependence1(_ x: consuming BufferView) -> dependsOn(scoped x) BufferView { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
  return BufferView(x.ptr)
}

func borrowingParamInvalidLifetimeDependence1(_ x: borrowing BufferView) -> dependsOn(x) BufferView {
  return BufferView(x.ptr)
}

func implicitBorrowingParamLifetimeDependence1(_ x: BufferView) -> dependsOn(x) BufferView {
  return BufferView(x.ptr)
}

func implicitBorrowingParamLifetimeDependence2(_ x: BufferView) -> dependsOn(scoped x) BufferView {
  return BufferView(x.ptr)
}

func inoutParamLifetimeDependence1(_ x: inout BufferView) -> dependsOn(x) BufferView {
  return BufferView(x.ptr)
}

func inoutParamLifetimeDependence2(_ x: inout BufferView) -> dependsOn(scoped x) BufferView {
  return BufferView(x.ptr)
}

func invalidSpecifierPosition1(_ x: borrowing dependsOn(x) BufferView) -> BufferView { // expected-error{{lifetime dependence specifiers may only be used on result of functions, methods, initializers}}
  return BufferView(x.ptr)
}

func invalidSpecifierPosition2(_ x: borrowing BufferView) -> BufferView { 
  let y: dependsOn(x) x // expected-error{{lifetime dependence specifiers may only be used on result of functions, methods, initializers}}
  return BufferView(y.ptr)
}

func invalidTupleLifetimeDependence(_ x: inout BufferView) -> (dependsOn(x) BufferView, BufferView) { // expected-error{{lifetime dependence specifiers cannot be applied to tuple elements}}
  return (BufferView(x.ptr), BufferView(x.ptr))
}

struct Wrapper : ~Escapable {
  let view: BufferView
  init(_ view: consuming BufferView) {
    self.view = view
  }
  borrowing func getView1() -> dependsOn(self) BufferView {
    return view
  }

  consuming func getView2() -> dependsOn(self) BufferView {
    return view
  }

  mutating func getView3() -> dependsOn(self) BufferView {
    return view
  }

  borrowing func getView4() -> dependsOn(self) BufferView {
    return view
  }

  borrowing func borrowingMethodLifetimeDependence1() -> dependsOn(self) BufferView { 
    return view
  }

  borrowing func borrowingMethodLifetimeDependence2() -> dependsOn(scoped self) BufferView {
    return view
  }

  consuming func consumingMethodLifetimeDependence1() -> dependsOn(self) BufferView {
    return view
  }

  consuming func consumingMethodInvalidLifetimeDependence1() -> dependsOn(scoped self) BufferView { // expected-error{{invalid use of scoped lifetime dependence with consuming ownership}}
    return view
  }

  mutating func mutatingMethodLifetimeDependence1() -> dependsOn(self) BufferView {
    return view
  }

  mutating func mutatingMethodLifetimeDependence2() -> dependsOn(scoped self) BufferView {
    return view
  } 
}

public struct GenericBufferView<Element> : ~Escapable {
  public typealias Index = Int
  public typealias Pointer = UnsafePointer<Element>

  public let baseAddress: Pointer
  public let count: Int

  public init<Storage>(unsafeBuffer: UnsafeBufferPointer<Element>,
                       storage: borrowing Storage)
    -> dependsOn(storage) Self {
    let baseAddress = unsafeBuffer.baseAddress!
    self = GenericBufferView<Element>(baseAddress: baseAddress,
                                      count: unsafeBuffer.count)
    return self
  }
  // unsafe private API
  @_unsafeNonescapableResult
  init(baseAddress: Pointer, count: Int) {
    precondition(count >= 0, "Count must not be negative")
    self.baseAddress = baseAddress
    self.count = count
  } 
}

func derive(_ x: BufferView) -> dependsOn(x) BufferView { // expected-note{{'derive' previously declared here}}
  return BufferView(x.ptr)
}

func derive(_ x: BufferView) -> dependsOn(scoped x) BufferView { // expected-error{{invalid redeclaration of 'derive'}}
  return BufferView(x.ptr)
}

struct RawBufferView {
  let ptr: UnsafeRawBufferPointer
  @_unsafeNonescapableResult
  init(_ ptr: UnsafeRawBufferPointer) {
    self.ptr = ptr
  }
}

extension RawBufferView {
  mutating func zeroBufferView() throws -> BufferView { // expected-error{{cannot infer lifetime dependence on a self which is BitwiseCopyable & Escapable}}
    return BufferView(ptr)
  }
}