|
| 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, iOS 16.4, watchOS 9.4, tvOS 16.4, *) |
| 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 | +// Swift 6.2 error: a mutating method cannot have a ~Escapable 'self' |
| 61 | +// requires https://github.com/swiftlang/swift/pull/84010 |
| 62 | +//int * __counted_by(len) nonescapingLifetimebound(struct B *d __lifetimebound, int len) __attribute__((swift_name("B.nonescapingLifetimebound(self:_:)"))); |
| 63 | + |
| 64 | +//--- Inputs/module.modulemap |
| 65 | +module Instance { |
| 66 | + header "instance.h" |
| 67 | +} |
| 68 | + |
| 69 | +//--- out.expected |
| 70 | +@__swiftmacro_So1AV5basic15_SwiftifyImportfMp_.swift |
| 71 | +------------------------------ |
| 72 | +/// This is an auto-generated wrapper for safer interop |
| 73 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 74 | +public mutating func basic(_ p: inout MutableSpan<Int32>) { |
| 75 | + let len = Int32(exactly: p.count)! |
| 76 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 77 | + return unsafe basic(_pPtr.baseAddress!, len) |
| 78 | + } |
| 79 | +} |
| 80 | +------------------------------ |
| 81 | +@__swiftmacro_So5basic15_SwiftifyImportfMp_.swift |
| 82 | +------------------------------ |
| 83 | +/// This is an auto-generated wrapper for safer interop |
| 84 | +@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 |
| 85 | +public func basic(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 86 | + let len = Int32(exactly: p.count)! |
| 87 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 88 | + return unsafe basic(a, _pPtr.baseAddress!, len) |
| 89 | + } |
| 90 | +} |
| 91 | +------------------------------ |
| 92 | +@__swiftmacro_So1AV3bar15_SwiftifyImportfMp_.swift |
| 93 | +------------------------------ |
| 94 | +/// This is an auto-generated wrapper for safer interop |
| 95 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 96 | +public mutating func bar(_ p: inout MutableSpan<Int32>) { |
| 97 | + let len = Int32(exactly: p.count)! |
| 98 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 99 | + return unsafe bar(_pPtr.baseAddress!, len) |
| 100 | + } |
| 101 | +} |
| 102 | +------------------------------ |
| 103 | +@__swiftmacro_So7renamed15_SwiftifyImportfMp_.swift |
| 104 | +------------------------------ |
| 105 | +/// This is an auto-generated wrapper for safer interop |
| 106 | +@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 |
| 107 | +public func renamed(_ a: UnsafeMutablePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 108 | + let len = Int32(exactly: p.count)! |
| 109 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 110 | + return unsafe renamed(a, _pPtr.baseAddress!, len) |
| 111 | + } |
| 112 | +} |
| 113 | +------------------------------ |
| 114 | +@__swiftmacro_So1AV9constSelf15_SwiftifyImportfMp_.swift |
| 115 | +------------------------------ |
| 116 | +/// This is an auto-generated wrapper for safer interop |
| 117 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 118 | +public func constSelf(_ p: inout MutableSpan<Int32>) { |
| 119 | + let len = Int32(exactly: p.count)! |
| 120 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 121 | + return unsafe constSelf(_pPtr.baseAddress!, len) |
| 122 | + } |
| 123 | +} |
| 124 | +------------------------------ |
| 125 | +@__swiftmacro_So9constSelf15_SwiftifyImportfMp_.swift |
| 126 | +------------------------------ |
| 127 | +/// This is an auto-generated wrapper for safer interop |
| 128 | +@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 |
| 129 | +public func constSelf(_ a: UnsafePointer<A>!, _ p: inout MutableSpan<Int32>) { |
| 130 | + let len = Int32(exactly: p.count)! |
| 131 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 132 | + return unsafe constSelf(a, _pPtr.baseAddress!, len) |
| 133 | + } |
| 134 | +} |
| 135 | +------------------------------ |
| 136 | +@__swiftmacro_So1AV7valSelf15_SwiftifyImportfMp_.swift |
| 137 | +------------------------------ |
| 138 | +/// This is an auto-generated wrapper for safer interop |
| 139 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 140 | +public func valSelf(_ p: inout MutableSpan<Int32>) { |
| 141 | + let len = Int32(exactly: p.count)! |
| 142 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 143 | + return unsafe valSelf(_pPtr.baseAddress!, len) |
| 144 | + } |
| 145 | +} |
| 146 | +------------------------------ |
| 147 | +@__swiftmacro_So7valSelf15_SwiftifyImportfMp_.swift |
| 148 | +------------------------------ |
| 149 | +/// This is an auto-generated wrapper for safer interop |
| 150 | +@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 |
| 151 | +public func valSelf(_ a: A, _ p: inout MutableSpan<Int32>) { |
| 152 | + let len = Int32(exactly: p.count)! |
| 153 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 154 | + return unsafe valSelf(a, _pPtr.baseAddress!, len) |
| 155 | + } |
| 156 | +} |
| 157 | +------------------------------ |
| 158 | +@__swiftmacro_So1AV17lifetimeBoundSelf15_SwiftifyImportfMp_.swift |
| 159 | +------------------------------ |
| 160 | +/// This is an auto-generated wrapper for safer interop |
| 161 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(borrow self) @_disfavoredOverload |
| 162 | +public func lifetimeBoundSelf(_ len: Int32) -> MutableSpan<Int32> { |
| 163 | + return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(len), count: Int(len)), copying: ()) |
| 164 | +} |
| 165 | +------------------------------ |
| 166 | +@__swiftmacro_So17lifetimeBoundSelf15_SwiftifyImportfMp_.swift |
| 167 | +------------------------------ |
| 168 | +/// This is an auto-generated wrapper for safer interop |
| 169 | +@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 |
| 170 | +public func lifetimeBoundSelf(_ a: A, _ len: Int32) -> MutableSpan<Int32> { |
| 171 | + return unsafe _swiftifyOverrideLifetime(MutableSpan<Int32>(_unsafeStart: unsafe lifetimeBoundSelf(a, len), count: Int(len)), copying: ()) |
| 172 | +} |
| 173 | +------------------------------ |
| 174 | +@__swiftmacro_So1CV7refSelf15_SwiftifyImportfMp_.swift |
| 175 | +------------------------------ |
| 176 | +/// This is an auto-generated wrapper for safer interop |
| 177 | +@_alwaysEmitIntoClient @available(visionOS 1.0, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *) @_lifetime(p: copy p) @_disfavoredOverload |
| 178 | +public func refSelf(_ p: inout MutableSpan<Int32>) { |
| 179 | + let len = Int32(exactly: p.count)! |
| 180 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 181 | + return unsafe refSelf(_pPtr.baseAddress!, len) |
| 182 | + } |
| 183 | +} |
| 184 | +------------------------------ |
| 185 | +@__swiftmacro_So7refSelf15_SwiftifyImportfMp_.swift |
| 186 | +------------------------------ |
| 187 | +/// This is an auto-generated wrapper for safer interop |
| 188 | +@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 |
| 189 | +public func refSelf(_ c: C!, _ p: inout MutableSpan<Int32>) { |
| 190 | + let len = Int32(exactly: p.count)! |
| 191 | + return unsafe p.withUnsafeMutableBufferPointer { _pPtr in |
| 192 | + return unsafe refSelf(c, _pPtr.baseAddress!, len) |
| 193 | + } |
| 194 | +} |
| 195 | +------------------------------ |
0 commit comments