|
| 1 | +// REQUIRES: swift_feature_SafeInteropWrappers |
| 2 | + |
| 3 | +// RUN: %empty-directory(%t) |
| 4 | +// RUN: split-file %s %t |
| 5 | + |
| 6 | +// RUN: %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t%{fs-sep}Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -verify -verify-additional-file %t%{fs-sep}Inputs%{fs-sep}instance.h %t/test.swift -I %bridging-path -DVERIFY |
| 7 | +// RUN: env SWIFT_BACKTRACE="" %target-swift-frontend -emit-module -plugin-path %swift-plugin-dir -o %t/Test.swiftmodule -I %t/Inputs -enable-experimental-feature SafeInteropWrappers -strict-memory-safety -warnings-as-errors -Xcc -Werror %t/test.swift -dump-macro-expansions -I %bridging-path 2> %t/out.txt |
| 8 | +// RUN: diff --strip-trailing-cr %t/out.txt %t/out.expected |
| 9 | + |
| 10 | +//--- test.swift |
| 11 | +import Instance |
| 12 | + |
| 13 | +@available(macOS 13.3.0, *) |
| 14 | +func foo(_ p: inout MutableSpan<CInt>, a: A, aa: inout A, c: C, b: B, bb: inout B) { |
| 15 | + aa.basic(&p) |
| 16 | + aa.bar(&p) |
| 17 | + a.constSelf(&p) |
| 18 | + a.valSelf(&p) |
| 19 | + let _: MutableSpan<CInt> = a.lifetimeBoundSelf(3) |
| 20 | + c.refSelf(&p) |
| 21 | + b.nonescaping(&p) |
| 22 | + let _: MutableSpan<CInt> = bb.nonescapingLifetimebound(73) |
| 23 | + |
| 24 | +#if VERIFY |
| 25 | + aa.countedSelf(&p) |
| 26 | + a.basic(&p) // expected-error{{cannot use mutating member on immutable value: 'a' is a 'let' constant}} |
| 27 | +#endif |
| 28 | +} |
| 29 | + |
| 30 | +//--- Inputs/instance.h |
| 31 | +#include <ptrcheck.h> |
| 32 | +#include <lifetimebound.h> |
| 33 | +#include <swift/bridging> |
| 34 | + |
| 35 | +struct A {}; |
| 36 | +struct SWIFT_NONESCAPABLE B {}; |
| 37 | +struct SWIFT_IMMORTAL_REFERENCE C {}; |
| 38 | + |
| 39 | +void basic(struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.basic(self:_:_:)"))); |
| 40 | + |
| 41 | +void renamed(struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.bar(self:_:_:)"))); |
| 42 | + |
| 43 | +void countedSelf(struct A * __counted_by(len) |
| 44 | + a, // expected-warning{{bounds attribute '__counted_by' ignored on parameter mapped to 'self'}} |
| 45 | + int * __counted_by(len) p __noescape, int len) |
| 46 | + __attribute__(( |
| 47 | + swift_name // expected-note{{swift_name maps free function to instance method here}} |
| 48 | + ("A.countedSelf(self:_:_:)"))); |
| 49 | + |
| 50 | +void constSelf(const struct A *a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.constSelf(self:_:_:)"))); |
| 51 | + |
| 52 | +void valSelf(struct A a, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("A.valSelf(self:_:_:)"))); |
| 53 | + |
| 54 | +void refSelf(struct C *c, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("C.refSelf(self:_:_:)"))); |
| 55 | + |
| 56 | +int * __counted_by(len) lifetimeBoundSelf(struct A a __lifetimebound, int len) __attribute__((swift_name("A.lifetimeBoundSelf(self:_:)"))); |
| 57 | + |
| 58 | +void nonescaping(const struct B *d, int * __counted_by(len) p __noescape, int len) __attribute__((swift_name("B.nonescaping(self:_:_:)"))); |
| 59 | + |
| 60 | +int * __counted_by(len) nonescapingLifetimebound(struct B *d __lifetimebound, int len) __attribute__((swift_name("B.nonescapingLifetimebound(self:_:)"))); |
| 61 | + |
| 62 | +//--- Inputs/module.modulemap |
| 63 | +module Instance { |
| 64 | + header "instance.h" |
| 65 | +} |
| 66 | + |
| 67 | +//--- out.expected |
| 68 | +@__swiftmacro_So1AV5basic15_SwiftifyImportfMp_.swift |
| 69 | +------------------------------ |
| 70 | +/// This is an auto-generated wrapper for safer interop |
| 71 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 72 | +public mutating func basic(_ p: inout MutableSpan<Int32>) { |
| 73 | + let len = Int32(exactly: p.count)! |
| 74 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 75 | + return unsafe basic(_pPtr.baseAddress!, len) |
| 76 | + } |
| 77 | +} |
| 78 | +------------------------------ |
| 79 | +@__swiftmacro_So5basic15_SwiftifyImportfMp_.swift |
| 80 | +------------------------------ |
| 81 | +/// This is an auto-generated wrapper for safer interop |
| 82 | +@available(swift, obsoleted: 3, renamed: "A.basic(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 83 | +public func basic(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 84 | + let len = Int32(exactly: p.count)! |
| 85 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 86 | + return unsafe basic(a, _pPtr.baseAddress!, len) |
| 87 | + } |
| 88 | +} |
| 89 | +------------------------------ |
| 90 | +@__swiftmacro_So1AV3bar15_SwiftifyImportfMp_.swift |
| 91 | +------------------------------ |
| 92 | +/// This is an auto-generated wrapper for safer interop |
| 93 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 94 | +public mutating func bar(_ p: inout MutableSpan<Int32>) { |
| 95 | + let len = Int32(exactly: p.count)! |
| 96 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 97 | + return unsafe bar(_pPtr.baseAddress!, len) |
| 98 | + } |
| 99 | +} |
| 100 | +------------------------------ |
| 101 | +@__swiftmacro_So7renamed15_SwiftifyImportfMp_.swift |
| 102 | +------------------------------ |
| 103 | +/// This is an auto-generated wrapper for safer interop |
| 104 | +@available(swift, obsoleted: 3, renamed: "A.bar(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 105 | +public func renamed(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 106 | + let len = Int32(exactly: p.count)! |
| 107 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 108 | + return unsafe renamed(a, _pPtr.baseAddress!, len) |
| 109 | + } |
| 110 | +} |
| 111 | +------------------------------ |
| 112 | +@__swiftmacro_So1AV9constSelf15_SwiftifyImportfMp_.swift |
| 113 | +------------------------------ |
| 114 | +/// This is an auto-generated wrapper for safer interop |
| 115 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 116 | +public func constSelf(_ p: inout MutableSpan<Int32>) { |
| 117 | + let len = Int32(exactly: p.count)! |
| 118 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 119 | + return unsafe constSelf(_pPtr.baseAddress!, len) |
| 120 | + } |
| 121 | +} |
| 122 | +------------------------------ |
| 123 | +@__swiftmacro_So9constSelf15_SwiftifyImportfMp_.swift |
| 124 | +------------------------------ |
| 125 | +/// This is an auto-generated wrapper for safer interop |
| 126 | +@available(swift, obsoleted: 3, renamed: "A.constSelf(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 127 | +public func constSelf(_ a: UnsafePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 128 | + let len = Int32(exactly: p.count)! |
| 129 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 130 | + return unsafe constSelf(a, _pPtr.baseAddress!, len) |
| 131 | + } |
| 132 | +} |
| 133 | +------------------------------ |
| 134 | +@__swiftmacro_So1AV7valSelf15_SwiftifyImportfMp_.swift |
| 135 | +------------------------------ |
| 136 | +/// This is an auto-generated wrapper for safer interop |
| 137 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 138 | +public func valSelf(_ p: inout MutableSpan<Int32>) { |
| 139 | + let len = Int32(exactly: p.count)! |
| 140 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 141 | + return unsafe valSelf(_pPtr.baseAddress!, len) |
| 142 | + } |
| 143 | +} |
| 144 | +------------------------------ |
| 145 | +@__swiftmacro_So7valSelf15_SwiftifyImportfMp_.swift |
| 146 | +------------------------------ |
| 147 | +/// This is an auto-generated wrapper for safer interop |
| 148 | +@available(swift, obsoleted: 3, renamed: "A.valSelf(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 149 | +public func valSelf(_ a: A, _ p: inout MutableSpan<Int32>) { |
| 150 | + let len = Int32(exactly: p.count)! |
| 151 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 152 | + return unsafe valSelf(a, _pPtr.baseAddress!, len) |
| 153 | + } |
| 154 | +} |
| 155 | +------------------------------ |
| 156 | +@__swiftmacro_So1AV17lifetimeBoundSelf15_SwiftifyImportfMp_.swift |
| 157 | +------------------------------ |
| 158 | +/// This is an auto-generated wrapper for safer interop |
| 159 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(borrow self) @_disfavoredOverload |
| 160 | +public func lifetimeBoundSelf(_ len: Int32) -> MutableSpan<Int32> { |
| 161 | + return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(len), count: Int(len)), copying: ()) |
| 162 | +} |
| 163 | +------------------------------ |
| 164 | +@__swiftmacro_So17lifetimeBoundSelf15_SwiftifyImportfMp_.swift |
| 165 | +------------------------------ |
| 166 | +/// This is an auto-generated wrapper for safer interop |
| 167 | +@available(swift, obsoleted: 3, renamed: "A.lifetimeBoundSelf(self:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(borrow a) @_disfavoredOverload |
| 168 | +public func lifetimeBoundSelf(_ a: A, _ len: Int32) -> MutableSpan<Int32> { |
| 169 | + return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(a, len), count: Int(len)), copying: ()) |
| 170 | +} |
| 171 | +------------------------------ |
| 172 | +@__swiftmacro_So1CV7refSelf15_SwiftifyImportfMp_.swift |
| 173 | +------------------------------ |
| 174 | +/// This is an auto-generated wrapper for safer interop |
| 175 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 176 | +public func refSelf(_ p: inout MutableSpan<Int32>) { |
| 177 | + let len = Int32(exactly: p.count)! |
| 178 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 179 | + return unsafe refSelf(_pPtr.baseAddress!, len) |
| 180 | + } |
| 181 | +} |
| 182 | +------------------------------ |
| 183 | +@__swiftmacro_So7refSelf15_SwiftifyImportfMp_.swift |
| 184 | +------------------------------ |
| 185 | +/// This is an auto-generated wrapper for safer interop |
| 186 | +@available(swift, obsoleted: 3, renamed: "C.refSelf(self:_:_:)") @_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 187 | +public func refSelf(_ c: C!, _ p: inout MutableSpan<Int32>) { |
| 188 | + let len = Int32(exactly: p.count)! |
| 189 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 190 | + return unsafe refSelf(c, _pPtr.baseAddress!, len) |
| 191 | + } |
| 192 | +} |
| 193 | +------------------------------ |
| 194 | +@__swiftmacro_So1BV11nonescaping15_SwiftifyImportfMp_.swift |
| 195 | +------------------------------ |
| 196 | +/// This is an auto-generated wrapper for safer interop |
| 197 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 198 | +public func nonescaping(_ p: inout MutableSpan<Int32>) { |
| 199 | + let len = Int32(exactly: p.count)! |
| 200 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 201 | + return unsafe nonescaping(_pPtr.baseAddress!, len) |
| 202 | + } |
| 203 | +} |
| 204 | +------------------------------ |
| 205 | +@__swiftmacro_So1BV24nonescapingLifetimebound15_SwiftifyImportfMp_.swift |
| 206 | +------------------------------ |
| 207 | +/// This is an auto-generated wrapper for safer interop |
| 208 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(copy self) @_disfavoredOverload |
| 209 | +public mutating func nonescapingLifetimebound(_ len: Int32) -> MutableSpan<Int32> { |
| 210 | + return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe nonescapingLifetimebound(len), count: Int(len)), copying: ()) |
| 211 | +} |
| 212 | +------------------------------ |
0 commit comments