File: lifetime_dependence_diagnostics.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (111 lines) | stat: -rw-r--r-- 3,967 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
// RUN: %target-swift-frontend %s -emit-sil \
// RUN:   -sil-verify-all \
// RUN:   -module-name test \
// RUN:   -enable-experimental-feature LifetimeDependence \
// RUN:   2>&1 | %FileCheck %s

// REQUIRES: swift_in_compiler
// REQUIRES: swift_feature_LifetimeDependence

struct BV : ~Escapable {
  let p: UnsafeRawPointer
  let c: Int
  @lifetime(borrow p)
  init(_ p: UnsafeRawPointer, _ c: Int) {
    self.p = p
    self.c = c
  }
}

@lifetime(bv)
func bv_copy(_ bv: borrowing BV) -> BV {
  copy bv
}

struct NCInt: ~Copyable {
  var i: Int
}

public struct NEInt: ~Escapable {
  var i: Int

  // Test yielding an address.
  // CHECK-LABEL: sil hidden @$s4test5NEIntV5ipropSivM : $@yield_once @convention(method) (@inout NEInt) -> @yields @inout Int {
  // CHECK: bb0(%0 : $*NEInt):
  // CHECK: [[A:%.*]] = begin_access [modify] [static] %0 : $*NEInt
  // CHECK: [[E:%.*]] = struct_element_addr [[A]] : $*NEInt, #NEInt.i
  // CHECK: yield [[E]] : $*Int, resume bb1, unwind bb2
  // CHECK: end_access [[A]] : $*NEInt
  // CHECK: end_access [[A]] : $*NEInt
  // CHECK-LABEL: } // end sil function '$s4test5NEIntV5ipropSivM'
  var iprop: Int {
    _read { yield i }
    _modify { yield &i }
  }

  @lifetime(borrow owner)
  init(owner: borrowing NCInt) {
    self.i = owner.i
  }
}

public enum NEOptional<Wrapped: ~Escapable>: ~Escapable {
  case none
  case some(Wrapped)
}

extension NEOptional where Wrapped: ~Escapable {
  // Test that enum initialization passes diagnostics.
  public init(_ some: consuming Wrapped) { self = .some(some) }
}

func takeClosure(_: () -> ()) {}

// No mark_dependence is needed for a inherited scope.
//
// CHECK-LABEL: sil hidden @$s4test14bv_borrow_copyyAA2BVVADF : $@convention(thin) (@guaranteed BV) -> @lifetime(borrow 0)  @owned BV {
// CHECK:      bb0(%0 : @noImplicitCopy $BV):
// CHECK:        apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> @lifetime(copy 0) @owned BV
// CHECK-NEXT:   return %3 : $BV
// CHECK-LABEL: } // end sil function '$s4test14bv_borrow_copyyAA2BVVADF'
@lifetime(borrow bv)
func bv_borrow_copy(_ bv: borrowing BV) -> BV {
  bv_copy(bv) 
}

// The mark_dependence for the borrow scope should be marked
// [nonescaping] after diagnostics.
//
// CHECK-LABEL: sil hidden @$s4test010bv_borrow_C00B0AA2BVVAE_tF : $@convention(thin) (@guaranteed BV) -> @lifetime(borrow 0)  @owned BV {
// CHECK:       bb0(%0 : @noImplicitCopy $BV):
// CHECK:         [[R:%.*]] = apply %{{.*}}(%0) : $@convention(thin) (@guaranteed BV) -> @lifetime(borrow 0) @owned BV
// CHECK:         %{{.*}} = mark_dependence [nonescaping] [[R]] : $BV on %0 : $BV
// CHECK-NEXT:    return %{{.*}} : $BV
// CHECK-LABEL: } // end sil function '$s4test010bv_borrow_C00B0AA2BVVAE_tF'
@lifetime(borrow bv)
func bv_borrow_borrow(bv: borrowing BV) -> BV {
  bv_borrow_copy(bv)
}

// This already has a mark_dependence [nonescaping] before diagnostics. If it triggers diagnostics again, it will fail
// because lifetime dependence does not expect a dependence directly on an 'inout' address without any 'begin_access'
// marker.
func ncint_capture(ncInt: inout NCInt) {
  takeClosure { _ = ncInt.i }
}

func neint_throws(ncInt: borrowing NCInt) throws -> NEInt {
  return NEInt(owner: ncInt)
}

// CHECK-LABEL: sil hidden @$s4test9neint_try5ncIntAA5NEIntVAA5NCIntV_tKF : $@convention(thin) (@guaranteed NCInt) -> @lifetime(borrow 0)  (@owned NEInt, @error any Error) {
// CHECK:   try_apply %{{.*}}(%0) : $@convention(thin) (@guaranteed NCInt) -> @lifetime(borrow 0)  (@owned NEInt, @error any Error), normal bb1, error bb2
// CHECK: bb1([[R:%.*]] : $NEInt):
// CHECK:   [[MD:%.*]] = mark_dependence [nonescaping] %5 : $NEInt on %0 : $NCInt
// CHECK:   return [[MD]] : $NEInt
// CHECK: bb2([[E:%.*]] : $any Error):
// CHECK:   throw [[E]] : $any Error
// CHECK-LABEL: } // end sil function '$s4test9neint_try5ncIntAA5NEIntVAA5NCIntV_tKF'
func neint_try(ncInt: borrowing NCInt) throws -> NEInt {
  try neint_throws(ncInt: ncInt)
}