|
| 1 | +// RUN: %target-sil-opt -enable-sil-verify-all %s -diagnose-lifetime-issues -o /dev/null -verify |
| 2 | + |
| 3 | +import Builtin |
| 4 | +import Swift |
| 5 | + |
| 6 | +enum FakeOptional<T> { |
| 7 | +case none |
| 8 | +case some(T) |
| 9 | +} |
| 10 | + |
| 11 | +class Delegate { |
| 12 | + func foo() { } |
| 13 | +} |
| 14 | + |
| 15 | +class MyDelegate : Delegate {} |
| 16 | + |
| 17 | +final class Container { |
| 18 | + weak var delegate: Delegate? |
| 19 | + var strongRef: Delegate |
| 20 | + |
| 21 | + func callDelegate() |
| 22 | + |
| 23 | + func strongDelegate(d: Delegate) |
| 24 | +} |
| 25 | + |
| 26 | +class WeakCycle { |
| 27 | + weak var c: WeakCycle? |
| 28 | +} |
| 29 | + |
| 30 | +sil [ossa] @$s24diagnose_lifetime_issues10MyDelegateCACycfC : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate |
| 31 | +sil [ossa] @$s24diagnose_lifetime_issues14strongDelegate1dyAA0D0C_tF : $@convention(method) (@guaranteed Delegate, @guaranteed Container) -> () |
| 32 | + |
| 33 | +// Test a warning for a reference that is copied, cast, and assigned |
| 34 | +// to an enum before it is assigned to a weak reference. |
| 35 | +sil [ossa] @testCastWarn : $@convention(thin) (@guaranteed Container) -> () { |
| 36 | +bb0(%0 : @guaranteed $Container): |
| 37 | + debug_value %0 : $Container, let, name "container", argno 1 |
| 38 | + %2 = metatype $@thick MyDelegate.Type |
| 39 | + // function_ref MyDelegate.__allocating_init() |
| 40 | + %3 = function_ref @$s24diagnose_lifetime_issues10MyDelegateCACycfC : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate |
| 41 | + |
| 42 | + // This is the owned allocation. |
| 43 | + %4 = apply %3(%2) : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate |
| 44 | + %11 = copy_value %4 : $MyDelegate |
| 45 | + |
| 46 | + // This upcast+enum is not an escape. |
| 47 | + %12 = upcast %11 : $MyDelegate to $Delegate |
| 48 | + %13 = enum $Optional<Delegate>, #Optional.some!enumelt, %12 : $Delegate |
| 49 | + %14 = ref_element_addr %0 : $Container, #Container.delegate |
| 50 | + %15 = begin_access [modify] [dynamic] %14 : $*@sil_weak Optional<Delegate> |
| 51 | + |
| 52 | + // This is the weak assignment. |
| 53 | + store_weak %13 to %15 : $*@sil_weak Optional<Delegate> // expected-warning {{weak reference will always be nil because the referenced object is deallocated here}} |
| 54 | + destroy_value %13 : $Optional<Delegate> |
| 55 | + end_access %15 : $*@sil_weak Optional<Delegate> |
| 56 | + destroy_value %4 : $MyDelegate |
| 57 | + %20 = tuple () |
| 58 | + return %20 : $() |
| 59 | +} |
| 60 | + |
| 61 | +// Test that a reference that escapes within a borrow scope has no warning. |
| 62 | +sil hidden [ossa] @testBorrowNoWarn : $@convention(thin) (@guaranteed Container) -> () { |
| 63 | +// %0 "container" |
| 64 | +bb0(%0 : @guaranteed $Container): |
| 65 | + debug_value %0 : $Container, let, name "container", argno 1 |
| 66 | + %2 = metatype $@thick MyDelegate.Type |
| 67 | + // function_ref MyDelegate.__allocating_init() |
| 68 | + %3 = function_ref @$s24diagnose_lifetime_issues10MyDelegateCACycfC : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate |
| 69 | + |
| 70 | + // This is the owned allocation. |
| 71 | + %4 = apply %3(%2) : $@convention(method) (@thick MyDelegate.Type) -> @owned MyDelegate |
| 72 | + debug_value %4 : $MyDelegate, let, name "delegate" |
| 73 | + %6 = begin_borrow %4 : $MyDelegate |
| 74 | + %7 = upcast %6 : $MyDelegate to $Delegate |
| 75 | + // function_ref Container.strongDelegate(d:) |
| 76 | + %8 = function_ref @$s24diagnose_lifetime_issues14strongDelegate1dyAA0D0C_tF : $@convention(method) (@guaranteed Delegate, @guaranteed Container) -> () |
| 77 | + |
| 78 | + // This apply is an escape. |
| 79 | + %9 = apply %8(%7, %0) : $@convention(method) (@guaranteed Delegate, @guaranteed Container) -> () |
| 80 | + end_borrow %6 : $MyDelegate |
| 81 | + %11 = copy_value %4 : $MyDelegate |
| 82 | + %12 = upcast %11 : $MyDelegate to $Delegate |
| 83 | + %13 = enum $Optional<Delegate>, #Optional.some!enumelt, %12 : $Delegate |
| 84 | + %14 = ref_element_addr %0 : $Container, #Container.delegate |
| 85 | + %15 = begin_access [modify] [dynamic] %14 : $*@sil_weak Optional<Delegate> |
| 86 | + |
| 87 | + // This is the weak assignment. |
| 88 | + store_weak %13 to %15 : $*@sil_weak Optional<Delegate> |
| 89 | + destroy_value %13 : $Optional<Delegate> |
| 90 | + end_access %15 : $*@sil_weak Optional<Delegate> |
| 91 | + destroy_value %4 : $MyDelegate |
| 92 | + %20 = tuple () |
| 93 | + return %20 : $() |
| 94 | +} |
| 95 | + |
| 96 | +// Helper for testReturnsAfterStore |
| 97 | +sil hidden [ossa] @testStoresWeakly : $@convention(method) (@owned WeakCycle) -> @owned WeakCycle { |
| 98 | +bb0(%0 : @owned $WeakCycle): |
| 99 | + %18 = begin_borrow %0 : $WeakCycle |
| 100 | + %19 = copy_value %0 : $WeakCycle |
| 101 | + %20 = enum $Optional<WeakCycle>, #Optional.some!enumelt, %19 : $WeakCycle |
| 102 | + %21 = ref_element_addr %18 : $WeakCycle, #WeakCycle.c |
| 103 | + %22 = begin_access [modify] [dynamic] %21 : $*@sil_weak Optional<WeakCycle> |
| 104 | + store_weak %20 to %22 : $*@sil_weak Optional<WeakCycle> |
| 105 | + destroy_value %20 : $Optional<WeakCycle> |
| 106 | + end_access %22 : $*@sil_weak Optional<WeakCycle> |
| 107 | + end_borrow %18 : $WeakCycle |
| 108 | + %27 = copy_value %0 : $WeakCycle |
| 109 | + destroy_value %0 : $WeakCycle |
| 110 | + return %27 : $WeakCycle |
| 111 | +} |
| 112 | + |
| 113 | +// Test no warning for a value returned after a weak store. |
| 114 | +sil hidden [exact_self_class] [ossa] @testReturnsAfterStore : $@convention(method) (@thick WeakCycle.Type) -> @owned WeakCycle { |
| 115 | +bb0(%0 : $@thick WeakCycle.Type): |
| 116 | + %1 = alloc_ref $WeakCycle |
| 117 | + |
| 118 | + %2 = function_ref @testStoresWeakly : $@convention(method) (@owned WeakCycle) -> @owned WeakCycle |
| 119 | + %3 = apply %2(%1) : $@convention(method) (@owned WeakCycle) -> @owned WeakCycle |
| 120 | + return %3 : $WeakCycle |
| 121 | +} |
0 commit comments