Commit Graph

7 Commits

Author SHA1 Message Date
Meghana Gupta
c14559173d Add dependsOn(immortal) 2024-06-11 11:18:10 -07:00
Andrew Trick
b5b0c75ccd Remove diagnostic: lifetime_dependence_on_bitwise_copyable
Allow lifetime depenendence on types that are BitwiseCopyable & Escapable.

This is unsafe in the sense that the compiler will not diagnose any use of the
dependent value outside of the lexcial scope of the source value. But, in
practice, dependence on an UnsafePointer is often needed. In that case, the
programmer should have already taken responsibility for ensuring the lifetime of the
pointer over all dependent uses. Typically, an unsafe pointer is valid for the
duration of a closure. Lifetime dependence prevents the dependent value from
being returned by the closure, so common usage is safe by default.

Typical example:

func decode(_ bufferRef: Span<Int>) { /*...*/ }

extension UnsafeBufferPointer {
  // The client must ensure the lifetime of the buffer across the invocation of `body`.
  // The client must ensure that no code modifies the buffer during the invocation of `body`.
  func withUnsafeSpan<Result>(_ body: (Span<Element>) throws -> Result) rethrows -> Result {
    // Construct Span using its internal, unsafe API.
    try body(Span(unsafePointer: baseAddress!, count: count))
  }
}

func decodeArrayAsUBP(array: [Int]) {
  array.withUnsafeBufferPointer { buffer in
    buffer.withUnsafeSpan {
      decode($0)
    }
  }
}

In the future, we may add SILGen support for tracking the lexical scope of
BitwiseCopyable values. That would allow them to have the same dependence
behavior as other source values.
2024-05-22 17:10:56 -07:00
Nate Chandler
ed5c7ef7ae [BitwiseCopyable] Promote to feature.
SE-0426 was accepted.
2024-04-24 15:52:20 -07:00
Meghana Gupta
b835c14964 LifetimeDependence inference, dont use BitwiseCopyable type as candidate 2024-04-16 13:19:45 -07:00
Meghana Gupta
a05c34c72f Fix typos in diagnostics 2024-04-16 13:19:40 -07:00
Meghana Gupta
eae192f1fb NFC: clang-format LifetimeDependence.cpp 2024-04-16 12:05:27 -07:00
Meghana Gupta
31b7a4e437 Move LifetimeDependence.cpp to AST 2024-04-16 12:05:23 -07:00