File: moveonly_lifetime.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 (56 lines) | stat: -rw-r--r-- 1,894 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
// RUN: %target-swift-emit-sil -sil-verify-all -module-name moveonly_lifetime -o /dev/null -Xllvm -sil-print-canonical-module -Onone -verify -enable-experimental-feature MoveOnlyClasses %s | %FileCheck %s

// REQUIRES: swift_feature_MoveOnlyClasses

struct C : ~Copyable {
    deinit {}
}

@_silgen_name("getC")
func getC() -> C

@_silgen_name("takeC")
func takeC(_ c: __owned C)

@_silgen_name("borrowC")
func borrowC(_ c: borrowing C)

@_silgen_name("something")
func something()

// Verify that a move-only value's lifetime extends to the end of the
// non-consuming scope where it appears but not further (which would require a
// copy).
//
// NOTE: We do not properly maximize lifetimes yet in the address checker. So
// the lifetimes are shortened in the sides of the diamonds inappropriately.
//
// CHECK-LABEL: sil hidden [ossa] @test_diamond__consume_r__use_l : $@convention(thin) (Bool) -> () {
// CHECK:         [[STACK:%.*]] = alloc_stack
// CHECK:         cond_br {{%[^,]+}}, [[LEFT:bb[0-9]+]], [[RIGHT:bb[0-9]+]]
//
// CHECK:       [[RIGHT]]:
// CHECK:         [[INSTANCE:%.*]] = load [take] [[STACK]]
// CHECK:         [[TAKE_C:%[^,]+]] = function_ref @takeC
// CHECK:         apply [[TAKE_C]]([[INSTANCE]])
// CHECK:         br [[BOTTOM:bb[0-9]+]]
//
// CHECK:       [[LEFT]]:
// CHECK:         [[INSTANCE:%.*]] = load_borrow [[STACK]]
// CHECK:         [[BORROW_C:%[^,]+]] = function_ref @borrowC
// CHECK:         apply [[BORROW_C]]([[INSTANCE]])
// CHECK:         [[SOMETHING:%[^,]+]] = function_ref @something
// CHECK:         apply [[SOMETHING]]
// CHECK:         destroy_addr [[STACK]]
// CHECK:         br [[BOTTOM]]
// CHECK-LABEL: } // end sil function 'test_diamond__consume_r__use_l'
@_silgen_name("test_diamond__consume_r__use_l")
func test_diamond(_ condition: Bool) {
  let c = getC()
  if condition {
    borrowC(c)
    something()
  } else {
    takeC(c)
  }
}