Skip to content

Commit 9fe23a3

Browse files
committed
[ownership] Add an interior_pointer error test for store_borrow.
Just didn't see one. Now we have added to our proof collection a test case where the store_borrow's result has a use after the end of the source object's lifetime.
1 parent 2c9f9bd commit 9fe23a3

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

test/SIL/ownership-verifier/interior_pointer.sil

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
// RUN: %target-sil-opt -sil-ownership-verifier-enable-testing -ownership-verifier-textual-error-dumper -enable-sil-verify-all=0 %s -o /dev/null 2>&1 | %FileCheck %s
22
// REQUIRES: asserts
33

4-
import Swift
5-
64
sil_stage canonical
75

6+
import Builtin
7+
88
class Klass {}
99

1010
class KlassUser {
1111
var field: Klass
1212
}
1313

14+
sil @use_builtinnativeobject_inguaranteed : $@convention(thin) (@in_guaranteed Builtin.NativeObject) -> ()
15+
1416
// CHECK-LABEL: Function: 'simple_error_ref_element_addr'
1517
// CHECK-NEXT: Found outside of lifetime use?!
1618
// CHECK-NEXT: Value: %1 = begin_borrow %0 : $KlassUser // users: %3, %2
@@ -43,16 +45,21 @@ bb0(%0 : @owned $KlassUser):
4345
return %3 : $Klass
4446
}
4547

48+
enum FakeOptional<T> {
49+
case none
50+
case some(T)
51+
}
52+
4653
class OptionalBox<T> {
47-
var t: T?
54+
var t: FakeOptional<T>
4855
}
4956

5057
// CHECK-NOT: Function: 'inject_enum_addr_test'
5158
sil [ossa] @inject_enum_addr_test : $@convention(thin) <T> (@owned OptionalBox<T>) -> () {
5259
bb0(%0 : @owned $OptionalBox<T>):
5360
%1 = begin_borrow %0 : $OptionalBox<T>
5461
%2 = ref_element_addr %1 : $OptionalBox<T>, #OptionalBox.t
55-
inject_enum_addr %2 : $*Optional<T>, #Optional.none!enumelt
62+
inject_enum_addr %2 : $*FakeOptional<T>, #FakeOptional.none!enumelt
5663
end_borrow %1 : $OptionalBox<T>
5764
destroy_value %0 : $OptionalBox<T>
5865
%3 = tuple ()
@@ -64,7 +71,7 @@ sil [ossa] @init_enum_data_addr_test : $@convention(thin) <T> (@owned OptionalBo
6471
bb0(%0 : @owned $OptionalBox<T>, %1 : $*T):
6572
%2 = begin_borrow %0 : $OptionalBox<T>
6673
%3 = ref_element_addr %2 : $OptionalBox<T>, #OptionalBox.t
67-
%4 = init_enum_data_addr %3 : $*Optional<T>, #Optional.some!enumelt
74+
%4 = init_enum_data_addr %3 : $*FakeOptional<T>, #FakeOptional.some!enumelt
6875
copy_addr %1 to [initialization] %4 : $*T
6976
end_borrow %2 : $OptionalBox<T>
7077
destroy_value %0 : $OptionalBox<T>
@@ -76,6 +83,10 @@ class Box<T> {
7683
var t: T
7784
}
7885

86+
struct Int {
87+
var _value: Builtin.Int64
88+
}
89+
7990
// CHECK-NOT: Function: 'unconditional_cast_test'
8091
sil [ossa] @unconditional_cast_test : $@convention(thin) <T> (@owned Box<T>, @in Int) -> () {
8192
bb0(%0 : @owned $Box<T>, %1 : $*Int):
@@ -86,4 +97,24 @@ bb0(%0 : @owned $Box<T>, %1 : $*Int):
8697
destroy_value %0 : $Box<T>
8798
%4 = tuple ()
8899
return %4 : $()
89-
}
100+
}
101+
102+
// CHECK-LABEL: Error#: 0. Begin Error in Function: 'store_borrow_result_used_outside_of_borrow_lifetime'
103+
// CHECK-NEXT: Found outside of lifetime use?!
104+
// CHECK-NEXT: Value: %1 = begin_borrow %0 : $Builtin.NativeObject // users: %4, %3
105+
// CHECK-NEXT: Consuming User: end_borrow %1 : $Builtin.NativeObject // id: %4
106+
// CHECK-NEXT: Non Consuming User: %7 = apply %6(%3) : $@convention(thin) (@in_guaranteed Builtin.NativeObject) -> ()
107+
// CHECK-NEXT: Block: bb0
108+
sil [ossa] @store_borrow_result_used_outside_of_borrow_lifetime : $@convention(thin) (@owned Builtin.NativeObject) -> () {
109+
bb0(%0 : @owned $Builtin.NativeObject):
110+
%0a = begin_borrow %0 : $Builtin.NativeObject
111+
%1 = alloc_stack $Builtin.NativeObject
112+
%result = store_borrow %0a to %1 : $*Builtin.NativeObject
113+
end_borrow %0a : $Builtin.NativeObject
114+
destroy_value %0 : $Builtin.NativeObject
115+
%func = function_ref @use_builtinnativeobject_inguaranteed : $@convention(thin) (@in_guaranteed Builtin.NativeObject) -> ()
116+
apply %func(%result) : $@convention(thin) (@in_guaranteed Builtin.NativeObject) -> ()
117+
dealloc_stack %1 : $*Builtin.NativeObject
118+
%9999 = tuple()
119+
return %9999 : $()
120+
}

0 commit comments

Comments
 (0)