File: moveonly_lifetime.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 (54 lines) | stat: -rw-r--r-- 1,850 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
// 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

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)
  }
}