Skip to content

Commit 2c4f623

Browse files
committed
[Swiftify] Always remove count parameters when possible
Previously we did not remove count parameters if any count parameters were shared between count expressions, or if any count expression contained operations. Buffer sizes were also just checked to be larger than or equal than the given count. We now extract the count from Spans/BufferPointers whenever possible, and store that value in a variable at the start of the function. If multiple parameters share the same count, a bounds check is emitted to make sure that they have the same size. Subspans can be used if one span is larger than necessary. The message in the bounds check is changed so that it includes the expected and actual value, to aid in debugging. This patch also fixes some incorrect indentation, and adds the Whitespace.swift test case to act as a regression test in case the indentation changes, since the other test cases don't use significant whitespace. rdar://151488820 rdar://151511090 rdar://146333006 rdar://147715799
1 parent 62e275c commit 2c4f623

38 files changed

+419
-265
lines changed

lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift

Lines changed: 114 additions & 105 deletions
Large diffs are not rendered by default.

test/Interop/C/swiftify-import/counted-by-lifetimebound.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import CountedByLifetimeboundClang
1515
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
1616
// CHECK-NEXT: @lifetime(copy p)
1717
// CHECK-NEXT: @lifetime(p: copy p)
18-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func complexExpr(_ len: Int32, _ offset: Int32, _ len2: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32>
18+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func complexExpr(_ len: Int32, _ offset: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32>
1919

2020
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
2121
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -44,7 +44,7 @@ import CountedByLifetimeboundClang
4444
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
4545
// CHECK-NEXT: @lifetime(copy p)
4646
// CHECK-NEXT: @lifetime(p: copy p)
47-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int32, _ p: inout MutableSpan<Int32>) -> MutableSpan<Int32>
47+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p: inout MutableSpan<Int32>) -> MutableSpan<Int32>
4848

4949
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
5050
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -56,7 +56,7 @@ import CountedByLifetimeboundClang
5656
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
5757
@inlinable
5858
public func callComplexExpr(_ p: inout MutableSpan<CInt>) {
59-
let _: MutableSpan<CInt> = complexExpr(73, 37, 42, &p)
59+
let _: MutableSpan<CInt> = complexExpr(73, 37, &p)
6060
}
6161

6262
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -80,7 +80,7 @@ public func callNullable(_ p: inout MutableSpan<CInt>?) {
8080
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
8181
@inlinable
8282
public func callShared(_ p: inout MutableSpan<CInt>) {
83-
let _: MutableSpan<CInt> = shared(CInt(p.count), &p)
83+
let _: MutableSpan<CInt> = shared(&p)
8484
}
8585

8686
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)

test/Interop/C/swiftify-import/counted-by-noescape.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ import CountedByNoEscapeClang
106106
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
107107
// CHECK-NEXT: @lifetime(p1: copy p1)
108108
// CHECK-NEXT: @lifetime(p2: copy p2)
109-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int32, _ p1: inout MutableSpan<Int32>, _ p2: inout MutableSpan<Int32>)
109+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p1: inout MutableSpan<Int32>, _ p2: inout MutableSpan<Int32>)
110110

111111
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
112112
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -165,7 +165,7 @@ public func callReturnPointer() {
165165
@lifetime(p2: copy p2)
166166
@inlinable
167167
public func callShared(_ p: inout MutableSpan<CInt>, _ p2: inout MutableSpan<CInt>) {
168-
shared(CInt(p.count), &p, &p2)
168+
shared(&p, &p2)
169169
}
170170

171171
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)

test/Interop/C/swiftify-import/counted-by.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import CountedByClang
4848
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func scalar(_ m: Int32, _ n: Int32, _ p: UnsafeMutableBufferPointer<Int32>)
4949

5050
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
51-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>)
51+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>)
5252

5353
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
5454
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func simple(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
@@ -108,7 +108,7 @@ public func callScalar(_ p: UnsafeMutableBufferPointer<CInt>) {
108108

109109
@inlinable
110110
public func callShared(_ p: UnsafeMutableBufferPointer<CInt>, _ p2: UnsafeMutableBufferPointer<CInt>) {
111-
shared(CInt(p.count), p, p2)
111+
shared(p, p2)
112112
}
113113

114114
@inlinable
@@ -136,4 +136,4 @@ public func callSimpleFlipped(_ p: UnsafeMutableBufferPointer<CInt>) {
136136
@inlinable
137137
public func callSwiftAttr(_ p: UnsafeMutableBufferPointer<CInt>) {
138138
swiftAttr(p)
139-
}
139+
}

test/Interop/C/swiftify-import/sized-by-lifetimebound.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SizedByLifetimeboundClang
1414
// CHECK: /// This is an auto-generated wrapper for safer interop
1515
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
1616
// CHECK-NEXT: @lifetime(copy p)
17-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func complexExpr(_ len: Int32, _ offset: Int32, _ len2: Int32, _ p: RawSpan) -> RawSpan
17+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func complexExpr(_ len: Int32, _ offset: Int32, _ p: RawSpan) -> RawSpan
1818

1919
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
2020
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -44,7 +44,7 @@ import SizedByLifetimeboundClang
4444
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
4545
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
4646
// CHECK-NEXT: @lifetime(copy p)
47-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int32, _ p: RawSpan) -> RawSpan
47+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p: RawSpan) -> RawSpan
4848

4949
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
5050
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -55,7 +55,7 @@ import SizedByLifetimeboundClang
5555
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
5656
@inlinable
5757
public func callComplexExpr(_ p: RawSpan) {
58-
let _: RawSpan = complexExpr(73, 37, 42, p)
58+
let _: RawSpan = complexExpr(73, 37, p)
5959
}
6060

6161
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -79,7 +79,7 @@ public func callNullable(_ p: RawSpan?) {
7979
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
8080
@inlinable
8181
public func callShared(_ p: RawSpan) {
82-
let _: RawSpan = shared(CInt(p.byteCount), p)
82+
let _: RawSpan = shared(p)
8383
}
8484

8585
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)

test/Interop/C/swiftify-import/sized-by-noescape.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import SizedByNoEscapeClang
3535

3636
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3737
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
38-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int{{.*}}, _ p1: RawSpan, _ p2: RawSpan)
38+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p1: RawSpan, _ p2: RawSpan)
3939

4040
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
4141
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -79,7 +79,7 @@ public func callReturnPointer() {
7979
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
8080
@inlinable
8181
public func callShared(_ p: RawSpan, _ p2: RawSpan) {
82-
shared(CInt(p.byteCount), p, p2)
82+
shared(p, p2)
8383
}
8484

8585
@available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -92,4 +92,4 @@ public func callSimple(_ p: RawSpan) {
9292
@inlinable
9393
public func callSwiftAttr(_ p: RawSpan) {
9494
swiftAttr(p)
95-
}
95+
}

test/Interop/C/swiftify-import/sized-by.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import SizedByClang
2929
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableRawBufferPointer
3030

3131
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
32-
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer)
32+
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func shared(_ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer)
3333

3434
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3535
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func simple(_ p: UnsafeMutableRawBufferPointer)
@@ -70,7 +70,7 @@ public func callReturnPointer() {
7070

7171
@inlinable
7272
public func callShared(_ p: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer) {
73-
shared(CInt(p.count), p, p2)
73+
shared(p, p2)
7474
}
7575

7676
@inlinable

test/Macros/SwiftifyImport/CountedBy/Anonymous.swift

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@ public func myFunc4(_: UnsafeMutablePointer<CInt>, _ len: CInt) {
2020

2121
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
2222
// CHECK-NEXT: public func myFunc(_ _myFunc_param0: UnsafeBufferPointer<CInt>) {
23-
// CHECK-NEXT: return unsafe myFunc(_myFunc_param0.baseAddress!, CInt(exactly: _myFunc_param0.count)!)
23+
// CHECK-NEXT: let _myFunc_param1 = CInt(exactly: unsafe _myFunc_param0.count)!
24+
// CHECK-NEXT: return unsafe myFunc(_myFunc_param0.baseAddress!, _myFunc_param1)
2425
// CHECK-NEXT: }
2526

2627
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
2728
// CHECK-NEXT: public func myFunc2(_ _myFunc2_param0: UnsafeBufferPointer<CInt>, _ _myFunc2_param2: CInt) {
28-
// CHECK-NEXT: return unsafe myFunc2(_myFunc2_param0.baseAddress!, CInt(exactly: _myFunc2_param0.count)!, _myFunc2_param2)
29+
// CHECK-NEXT: let _myFunc2_param1 = CInt(exactly: unsafe _myFunc2_param0.count)!
30+
// CHECK-NEXT: return unsafe myFunc2(_myFunc2_param0.baseAddress!, _myFunc2_param1, _myFunc2_param2)
2931
// CHECK-NEXT: }
3032

3133
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
3234
// CHECK-NEXT: public func myFunc3(_ _myFunc3_param0: Span<CInt>) {
33-
// CHECK-NEXT: return unsafe _myFunc3_param0.withUnsafeBufferPointer { __myFunc3_param0Ptr in
34-
// CHECK-NEXT: return unsafe myFunc3(__myFunc3_param0Ptr.baseAddress!, CInt(exactly: __myFunc3_param0Ptr.count)!)
35-
// CHECK-NEXT: }
35+
// CHECK-NEXT: let _myFunc3_param1 = CInt(exactly: _myFunc3_param0.count)!
36+
// CHECK-NEXT: return unsafe _myFunc3_param0.withUnsafeBufferPointer { __myFunc3_param0Ptr in
37+
// CHECK-NEXT: return unsafe myFunc3(__myFunc3_param0Ptr.baseAddress!, _myFunc3_param1)
38+
// CHECK-NEXT: }
3639
// CHECK-NEXT: }
3740

3841
// CHECK: @_alwaysEmitIntoClient @lifetime(_myFunc4_param0: copy _myFunc4_param0) @_disfavoredOverload
3942
// CHECK-NEXT: public func myFunc4(_ _myFunc4_param0: inout MutableSpan<CInt>) {
40-
// CHECK-NEXT: return unsafe _myFunc4_param0.withUnsafeMutableBufferPointer { __myFunc4_param0Ptr in
41-
// CHECK-NEXT: return unsafe myFunc4(__myFunc4_param0Ptr.baseAddress!, CInt(exactly: __myFunc4_param0Ptr.count)!)
42-
// CHECK-NEXT: }
43+
// CHECK-NEXT: let _myFunc4_param1 = CInt(exactly: _myFunc4_param0.count)!
44+
// CHECK-NEXT: return unsafe _myFunc4_param0.withUnsafeMutableBufferPointer { __myFunc4_param0Ptr in
45+
// CHECK-NEXT: return unsafe myFunc4(__myFunc4_param0Ptr.baseAddress!, _myFunc4_param1)
46+
// CHECK-NEXT: }
4347
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/CountExpr.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ func myFunc(_ ptr: UnsafePointer<CInt>, _ size: CInt, _ count: CInt) {
88

99
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
1010
// CHECK-NEXT: func myFunc(_ ptr: UnsafeBufferPointer<CInt>, _ size: CInt, _ count: CInt) {
11-
// CHECK-NEXT: let _ptrCount: some BinaryInteger = size * count
12-
// CHECK-NEXT: if ptr.count < _ptrCount || _ptrCount < 0 {
13-
// CHECK-NEXT: fatalError("bounds check failure when calling unsafe function")
11+
// CHECK-NEXT: let _ptrCount = unsafe ptr.count
12+
// CHECK-NEXT: if _ptrCount != size * count {
13+
// CHECK-NEXT: fatalError("bounds check failure in myFunc: expected \(size * count) but got \(_ptrCount)")
1414
// CHECK-NEXT: }
1515
// CHECK-NEXT: return unsafe myFunc(ptr.baseAddress!, size, count)
1616
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/MultipleParams.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@ func myFunc(_ ptr: UnsafePointer<CInt>, _ len: CInt, _ ptr2: UnsafePointer<CInt>
88

99
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
1010
// CHECK-NEXT: func myFunc(_ ptr: UnsafeBufferPointer<CInt>, _ ptr2: UnsafeBufferPointer<CInt>) {
11-
// CHECK-NEXT: return unsafe myFunc(ptr.baseAddress!, CInt(exactly: ptr.count)!, ptr2.baseAddress!, CInt(exactly: ptr2.count)!)
11+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
12+
// CHECK-NEXT: let len2 = CInt(exactly: unsafe ptr2.count)!
13+
// CHECK-NEXT: return unsafe myFunc(ptr.baseAddress!, len, ptr2.baseAddress!, len2)
1214
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/Mutable.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {
88

99
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
1010
// CHECK-NEXT: func myFunc(_ ptr: UnsafeMutableBufferPointer<CInt>) {
11-
// CHECK-NEXT: return unsafe myFunc(ptr.baseAddress!, CInt(exactly: ptr.count)!)
11+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
12+
// CHECK-NEXT: return unsafe myFunc(ptr.baseAddress!, len)
1213
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/MutableSpan.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {
99

1010
// CHECK: @_alwaysEmitIntoClient @lifetime(ptr: copy ptr) @_disfavoredOverload
1111
// CHECK-NEXT: func myFunc(_ ptr: inout MutableSpan<CInt>) {
12+
// CHECK-NEXT: let len = CInt(exactly: ptr.count)!
1213
// CHECK-NEXT: return unsafe ptr.withUnsafeMutableBufferPointer { _ptrPtr in
13-
// CHECK-NEXT: return unsafe myFunc(_ptrPtr.baseAddress!, CInt(exactly: _ptrPtr.count)!)
14+
// CHECK-NEXT: return unsafe myFunc(_ptrPtr.baseAddress!, len)
1415
// CHECK-NEXT: }
1516
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/NamedParams.swift

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,30 +28,36 @@ func allNamedOther(buf ptr: UnsafePointer<CInt>, count len: CInt) {
2828

2929
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
3030
// CHECK-NEXT: func ptrNamed(ptr: UnsafeBufferPointer<CInt>) {
31-
// CHECK-NEXT: return unsafe ptrNamed(ptr: ptr.baseAddress!, CInt(exactly: ptr.count)!)
31+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
32+
// CHECK-NEXT: return unsafe ptrNamed(ptr: ptr.baseAddress!, len)
3233
// CHECK-NEXT: }
3334

3435
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
3536
// CHECK-NEXT: func ptrNamedOther(buf ptr: UnsafeBufferPointer<CInt>) {
36-
// CHECK-NEXT: return unsafe ptrNamedOther(buf: ptr.baseAddress!, CInt(exactly: ptr.count)!)
37+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
38+
// CHECK-NEXT: return unsafe ptrNamedOther(buf: ptr.baseAddress!, len)
3739
// CHECK-NEXT: }
3840

3941
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
4042
// CHECK-NEXT: func lenNamed(_ ptr: UnsafeBufferPointer<CInt>) {
41-
// CHECK-NEXT: return unsafe lenNamed(ptr.baseAddress!, len: CInt(exactly: ptr.count)!)
43+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
44+
// CHECK-NEXT: return unsafe lenNamed(ptr.baseAddress!, len: len)
4245
// CHECK-NEXT: }
4346

4447
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
4548
// CHECK-NEXT: func lenNamedOther(_ ptr: UnsafeBufferPointer<CInt>) {
46-
// CHECK-NEXT: return unsafe lenNamedOther(ptr.baseAddress!, count: CInt(exactly: ptr.count)!)
49+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
50+
// CHECK-NEXT: return unsafe lenNamedOther(ptr.baseAddress!, count: len)
4751
// CHECK-NEXT: }
4852

4953
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
5054
// CHECK-NEXT: func allNamed(ptr: UnsafeBufferPointer<CInt>) {
51-
// CHECK-NEXT: return unsafe allNamed(ptr: ptr.baseAddress!, len: CInt(exactly: ptr.count)!)
55+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
56+
// CHECK-NEXT: return unsafe allNamed(ptr: ptr.baseAddress!, len: len)
5257
// CHECK-NEXT: }
5358

5459
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
5560
// CHECK-NEXT: func allNamedOther(buf ptr: UnsafeBufferPointer<CInt>) {
56-
// CHECK-NEXT: return unsafe allNamedOther(buf: ptr.baseAddress!, count: CInt(exactly: ptr.count)!)
61+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr.count)!
62+
// CHECK-NEXT: return unsafe allNamedOther(buf: ptr.baseAddress!, count: len)
5763
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/Nullable.swift

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,43 +20,47 @@ func myFunc4(_ ptr: UnsafeMutablePointer<CInt>?, _ len: CInt) -> UnsafeMutablePo
2020

2121
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
2222
// CHECK-NEXT: func myFunc(_ ptr: UnsafeBufferPointer<CInt>?) {
23-
// CHECK-NEXT: return unsafe myFunc(ptr?.baseAddress, CInt(exactly: ptr?.count ?? 0)!)
23+
// CHECK-NEXT: let len = CInt(exactly: unsafe ptr?.count ?? 0)!
24+
// CHECK-NEXT: return unsafe myFunc(ptr?.baseAddress, len)
2425
// CHECK-NEXT: }
2526

2627
// CHECK: @_alwaysEmitIntoClient @lifetime(ptr: copy ptr) @_disfavoredOverload
2728
// CHECK-NEXT: func myFunc2(_ ptr: inout MutableSpan<CInt>?) {
29+
// CHECK-NEXT: let len = CInt(exactly: ptr?.count ?? 0)!
2830
// CHECK-NEXT: return { () in
2931
// CHECK-NEXT: return if ptr == nil {
30-
// CHECK-NEXT: unsafe myFunc2(nil, CInt(exactly: ptr?.count ?? 0)!)
32+
// CHECK-NEXT: unsafe myFunc2(nil, len)
3133
// CHECK-NEXT: } else {
3234
// CHECK-NEXT: unsafe ptr!.withUnsafeMutableBufferPointer { _ptrPtr in
33-
// CHECK-NEXT: return unsafe myFunc2(_ptrPtr.baseAddress, CInt(exactly: _ptrPtr.count)!)
35+
// CHECK-NEXT: return unsafe myFunc2(_ptrPtr.baseAddress, len)
3436
// CHECK-NEXT: }
3537
// CHECK-NEXT: }
3638
// CHECK-NEXT: }()
3739
// CHECK-NEXT: }
3840

3941
// CHECK: @_alwaysEmitIntoClient @lifetime(ptr: copy ptr) @lifetime(ptr2: copy ptr2) @_disfavoredOverload
4042
// CHECK-NEXT: func myFunc3(_ ptr: inout MutableSpan<CInt>?, _ ptr2: inout MutableSpan<CInt>?) {
43+
// CHECK-NEXT: let len = CInt(exactly: ptr?.count ?? 0)!
44+
// CHECK-NEXT: let len2 = CInt(exactly: ptr2?.count ?? 0)!
4145
// CHECK-NEXT: return { () in
4246
// CHECK-NEXT: return if ptr2 == nil {
4347
// CHECK-NEXT: { () in
4448
// CHECK-NEXT: return if ptr == nil {
45-
// CHECK-NEXT: unsafe myFunc3(nil, CInt(exactly: ptr?.count ?? 0)!, nil, CInt(exactly: ptr2?.count ?? 0)!)
49+
// CHECK-NEXT: unsafe myFunc3(nil, len, nil, len2)
4650
// CHECK-NEXT: } else {
4751
// CHECK-NEXT: unsafe ptr!.withUnsafeMutableBufferPointer { _ptrPtr in
48-
// CHECK-NEXT: return unsafe myFunc3(_ptrPtr.baseAddress, CInt(exactly: _ptrPtr.count)!, nil, CInt(exactly: ptr2?.count ?? 0)!)
52+
// CHECK-NEXT: return unsafe myFunc3(_ptrPtr.baseAddress, len, nil, len2)
4953
// CHECK-NEXT: }
5054
// CHECK-NEXT: }
5155
// CHECK-NEXT: }()
5256
// CHECK-NEXT: } else {
5357
// CHECK-NEXT: unsafe ptr2!.withUnsafeMutableBufferPointer { _ptr2Ptr in
5458
// CHECK-NEXT: return { () in
5559
// CHECK-NEXT: return if ptr == nil {
56-
// CHECK-NEXT: unsafe myFunc3(nil, CInt(exactly: ptr?.count ?? 0)!, _ptr2Ptr.baseAddress, CInt(exactly: _ptr2Ptr.count)!)
60+
// CHECK-NEXT: unsafe myFunc3(nil, len, _ptr2Ptr.baseAddress, len2)
5761
// CHECK-NEXT: } else {
5862
// CHECK-NEXT: unsafe ptr!.withUnsafeMutableBufferPointer { _ptrPtr in
59-
// CHECK-NEXT: return unsafe myFunc3(_ptrPtr.baseAddress, CInt(exactly: _ptrPtr.count)!, _ptr2Ptr.baseAddress, CInt(exactly: _ptr2Ptr.count)!)
63+
// CHECK-NEXT: return unsafe myFunc3(_ptrPtr.baseAddress, len, _ptr2Ptr.baseAddress, len2)
6064
// CHECK-NEXT: }
6165
// CHECK-NEXT: }
6266
// CHECK-NEXT: }()
@@ -66,11 +70,8 @@ func myFunc4(_ ptr: UnsafeMutablePointer<CInt>?, _ len: CInt) -> UnsafeMutablePo
6670
// CHECK-NEXT: }
6771

6872
// CHECK: @_alwaysEmitIntoClient @lifetime(copy ptr) @lifetime(ptr: copy ptr) @_disfavoredOverload
69-
// CHECK-NEXT: func myFunc4(_ ptr: inout MutableSpan<CInt>?, _ len: CInt) -> MutableSpan<CInt>? {
70-
// CHECK-NEXT: let _ptrCount: some BinaryInteger = len
71-
// CHECK-NEXT: if ptr?.count ?? 0 < _ptrCount || _ptrCount < 0 {
72-
// CHECK-NEXT: fatalError("bounds check failure when calling unsafe function")
73-
// CHECK-NEXT: }
73+
// CHECK-NEXT: func myFunc4(_ ptr: inout MutableSpan<CInt>?) -> MutableSpan<CInt>? {
74+
// CHECK-NEXT: let len = CInt(exactly: ptr?.count ?? 0)!
7475
// CHECK-NEXT: return unsafe _swiftifyOverrideLifetime({ () in
7576
// CHECK-NEXT: let _resultValue = { () in
7677
// CHECK-NEXT: return if ptr == nil {

0 commit comments

Comments
 (0)