|
| 1 | +// RUN: %target-swift-frontend -enable-experimental-feature NonescapableTypes -enable-experimental-feature BuiltinModule -enable-experimental-feature NoncopyableGenerics -parse-stdlib -module-name Swift -DADDRESS_ONLY -emit-sil -verify %s |
| 2 | +// RUN: %target-swift-frontend -enable-experimental-feature NonescapableTypes -enable-experimental-feature BuiltinModule -enable-experimental-feature NoncopyableGenerics -parse-stdlib -module-name Swift -DLOADABLE -emit-sil -verify %s |
| 3 | +// RUN: %target-swift-frontend -enable-experimental-feature NonescapableTypes -enable-experimental-feature BuiltinModule -enable-experimental-feature NoncopyableGenerics -parse-stdlib -module-name Swift -DTRIVIAL -emit-sil -verify %s |
| 4 | +// RUN: %target-swift-frontend -enable-experimental-feature NonescapableTypes -enable-experimental-feature BuiltinModule -enable-experimental-feature NoncopyableGenerics -parse-stdlib -module-name Swift -DEMPTY -emit-sil -verify %s |
| 5 | + |
| 6 | +// TODO: Use the real stdlib types once `UnsafePointer` supports noncopyable |
| 7 | +// types. |
| 8 | + |
| 9 | +import Builtin |
| 10 | + |
| 11 | +@_marker public protocol Copyable: ~Escapable {} |
| 12 | +@_marker public protocol Escapable: ~Copyable {} |
| 13 | +@frozen public struct UnsafePointer<T: ~Copyable>: Copyable { |
| 14 | + var value: Builtin.RawPointer |
| 15 | +} |
| 16 | + |
| 17 | +@frozen public struct UnsafeMutablePointer<T: ~Copyable>: Copyable { |
| 18 | + var value: Builtin.RawPointer |
| 19 | +} |
| 20 | + |
| 21 | +@frozen public struct Int { var value: Builtin.Word } |
| 22 | + |
| 23 | +@_silgen_name("makeUpAPointer") |
| 24 | +func makeUpAPointer<T: ~Copyable>() -> UnsafePointer<T> |
| 25 | +@_silgen_name("makeUpAMutablePointer") |
| 26 | +func makeUpAPointer<T: ~Copyable>() -> UnsafeMutablePointer<T> |
| 27 | +@_silgen_name("makeUpAnInt") |
| 28 | +func makeUpAnInt() -> Int |
| 29 | + |
| 30 | +class X {} |
| 31 | + |
| 32 | +struct NC: ~Copyable { |
| 33 | +#if EMPTY |
| 34 | +#elseif TRIVIAL |
| 35 | + var x: Int = makeUpAnInt() |
| 36 | +#elseif LOADABLE |
| 37 | + var x: X = X() |
| 38 | +#elseif ADDRESS_ONLY |
| 39 | + var x: Any = X() |
| 40 | +#else |
| 41 | +#error("pick a mode") |
| 42 | +#endif |
| 43 | + deinit {} |
| 44 | +} |
| 45 | + |
| 46 | +struct S { |
| 47 | + var data: NC { |
| 48 | + unsafeAddress { return makeUpAPointer() } |
| 49 | + } |
| 50 | + |
| 51 | + var mutableData: NC { |
| 52 | + unsafeAddress { return makeUpAPointer() } |
| 53 | + unsafeMutableAddress { return makeUpAPointer() } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +struct SNC: ~Copyable { |
| 58 | + var data: NC { |
| 59 | + unsafeAddress { return makeUpAPointer() } |
| 60 | + } |
| 61 | + |
| 62 | + var mutableData: NC { |
| 63 | + unsafeAddress { return makeUpAPointer() } |
| 64 | + unsafeMutableAddress { return makeUpAPointer() } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +class C { |
| 69 | + final var data: NC { |
| 70 | + unsafeAddress { return makeUpAPointer() } |
| 71 | + } |
| 72 | + |
| 73 | + final var mutableData: NC { |
| 74 | + unsafeAddress { return makeUpAPointer() } |
| 75 | + unsafeMutableAddress { return makeUpAPointer() } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func borrow(_ nc: borrowing NC) {} |
| 80 | +func mod(_ nc: inout NC) {} |
| 81 | +func take(_ nc: consuming NC) {} |
| 82 | + |
| 83 | +// TODO: Resolve thing being consumed more precisely than 'unknown'. |
| 84 | +// TODO: Use more specific diagnostic than "reinitialization of inout parameter" |
| 85 | + |
| 86 | +func test(c: C) { |
| 87 | + borrow(c.data) |
| 88 | + take(c.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 89 | + |
| 90 | + borrow(c.mutableData) |
| 91 | + mod(&c.mutableData) |
| 92 | + take(c.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}} |
| 93 | +} |
| 94 | +func test(s: S) { |
| 95 | + borrow(s.data) |
| 96 | + take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 97 | + |
| 98 | + borrow(s.mutableData) |
| 99 | + take(s.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 100 | +} |
| 101 | +func test(mut_s s: inout S) { |
| 102 | + borrow(s.data) |
| 103 | + take(s.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 104 | + |
| 105 | + borrow(s.mutableData) |
| 106 | + mod(&s.mutableData) |
| 107 | + take(s.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}} |
| 108 | +} |
| 109 | +func test(snc: borrowing SNC) { |
| 110 | + borrow(snc.data) |
| 111 | + take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 112 | + |
| 113 | + borrow(snc.mutableData) |
| 114 | + take(snc.mutableData) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 115 | +} |
| 116 | +func test(mut_snc snc: inout SNC) { |
| 117 | + borrow(snc.data) |
| 118 | + take(snc.data) // expected-error{{'unknown' is borrowed and cannot be consumed}} expected-note{{consumed here}} |
| 119 | + |
| 120 | + borrow(snc.mutableData) |
| 121 | + mod(&snc.mutableData) |
| 122 | + take(snc.mutableData) // expected-error{{missing reinitialization of inout parameter 'unknown' after consume}} expected-note{{consumed here}} |
| 123 | +} |
0 commit comments