Skip to content

Commit 9b3a84a

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 c8eba86 commit 9b3a84a

35 files changed

+357
-220
lines changed

lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift

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

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import CountedByNoEscapeClang
4949
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
5050
// CHECK-NEXT: @lifetime(p1: copy p1)
5151
// CHECK-NEXT: @lifetime(p2: copy p2)
52-
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int32, _ p1: inout MutableSpan<Int32>, _ p2: inout MutableSpan<Int32>)
52+
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ p1: inout MutableSpan<Int32>, _ p2: inout MutableSpan<Int32>)
5353

5454
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
5555
// CHECK-NEXT: @available(visionOS 1.1, tvOS 12.2, watchOS 5.2, iOS 12.2, macOS 10.14.4, *)
@@ -108,7 +108,7 @@ public func callReturnPointer() {
108108
@lifetime(p2: copy p2)
109109
@inlinable
110110
public func callShared(_ p: inout MutableSpan<CInt>, _ p2: inout MutableSpan<CInt>) {
111-
shared(CInt(p.count), &p, &p2)
111+
shared(&p, &p2)
112112
}
113113

114114
@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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ import CountedByClang
4848
// CHECK-NEXT: @_alwaysEmitIntoClient 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 public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>)
51+
// CHECK-NEXT: @_alwaysEmitIntoClient 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 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

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

Lines changed: 2 additions & 2 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 public func shared(_ len: Int{{.*}}, _ p1: RawSpan, _ p2: RawSpan)
38+
// CHECK-NEXT: @_alwaysEmitIntoClient 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, *)

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 public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer)
32+
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer)
3333

3434
// CHECK-NEXT: /// This is an auto-generated wrapper for safer interop
3535
// CHECK-NEXT: @_alwaysEmitIntoClient 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: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,28 @@ public func myFunc4(_: UnsafeMutablePointer<CInt>, _ len: CInt) {
2020

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

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

3133
// CHECK: @_alwaysEmitIntoClient
3234
// CHECK-NEXT: public func myFunc3(_ _param0: Span<CInt>) {
35+
// CHECK-NEXT: let `len` = CInt(exactly: _param0.count)!
3336
// CHECK-NEXT: return unsafe _param0.withUnsafeBufferPointer { __param0Ptr in
34-
// CHECK-NEXT: return unsafe myFunc3(__param0Ptr.baseAddress!, CInt(exactly: __param0Ptr.count)!)
37+
// CHECK-NEXT: return unsafe myFunc3(__param0Ptr.baseAddress!, len)
3538
// CHECK-NEXT: }
3639
// CHECK-NEXT: }
3740

3841
// CHECK: @_alwaysEmitIntoClient @lifetime(_param0: copy _param0)
3942
// CHECK-NEXT: public func myFunc4(_ _param0: inout MutableSpan<CInt>) {
43+
// CHECK-NEXT: let `len` = CInt(exactly: _param0.count)!
4044
// CHECK-NEXT: return unsafe _param0.withUnsafeMutableBufferPointer { __param0Ptr in
41-
// CHECK-NEXT: return unsafe myFunc4(__param0Ptr.baseAddress!, CInt(exactly: __param0Ptr.count)!)
45+
// CHECK-NEXT: return unsafe myFunc4(__param0Ptr.baseAddress!, len)
4246
// CHECK-NEXT: }
43-
// CHECK-NEXT: }
47+
// CHECK-NEXT: }

test/Macros/SwiftifyImport/CountedBy/CountExpr.swift

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

99
// CHECK: @_alwaysEmitIntoClient
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: }
17-

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
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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ func myFunc(_ ptr: UnsafeMutablePointer<CInt>, _ len: CInt) {
88

99
// CHECK: @_alwaysEmitIntoClient
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: }
13-

test/Macros/SwiftifyImport/CountedBy/MutableSpan.swift

Lines changed: 3 additions & 2 deletions
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)
1111
// CHECK-NEXT: func myFunc(_ ptr: inout MutableSpan<CInt>) {
12-
// CHECK-NEXT: return unsafe ptr.withUnsafeMutableBufferPointer { _ptrPtr in
13-
// CHECK-NEXT: return unsafe myFunc(_ptrPtr.baseAddress!, CInt(exactly: _ptrPtr.count)!)
12+
// CHECK-NEXT: let `len` = CInt(exactly: ptr.count)!
13+
// CHECK-NEXT: return unsafe ptr.withUnsafeMutableBufferPointer { _ptrPtr in
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 & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,31 +28,36 @@ func allNamedOther(buf ptr: UnsafePointer<CInt>, count len: CInt) {
2828

2929
// CHECK: @_alwaysEmitIntoClient
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
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
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
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
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
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: }
58-

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
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)
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)
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)
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 { () in
7576
// CHECK-NEXT: let _resultValue = { () in
7677
// CHECK-NEXT: return if ptr == nil {

test/Macros/SwiftifyImport/CountedBy/PointerReturn.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@ func lifetimeDependentBorrow(_ p: borrowing UnsafePointer<CInt>, _ len1: CInt, _
3030

3131
// CHECK: @_alwaysEmitIntoClient @lifetime(copy p)
3232
// CHECK-NEXT: func lifetimeDependentCopy(_ p: Span<CInt>, _ len2: CInt) -> Span<CInt> {
33-
// CHECK-NEXT: return unsafe _swiftifyOverrideLifetime(Span<CInt> (_unsafeStart: unsafe p.withUnsafeBufferPointer { _pPtr in
34-
// CHECK-NEXT: return unsafe lifetimeDependentCopy(_pPtr.baseAddress!, CInt(exactly: _pPtr.count)!, len2)
35-
// CHECK-NEXT: }, count: Int(len2)), copying: ())
33+
// CHECK-NEXT: let `len1` = CInt(exactly: p.count)!
34+
// CHECK-NEXT: return unsafe _swiftifyOverrideLifetime(Span<CInt> (_unsafeStart: unsafe p.withUnsafeBufferPointer { _pPtr in
35+
// CHECK-NEXT: return unsafe lifetimeDependentCopy(_pPtr.baseAddress!, len1, len2)
36+
// CHECK-NEXT: }, count: Int(len2)), copying: ())
3637
// CHECK-NEXT: }
3738

3839
// CHECK: @_alwaysEmitIntoClient @lifetime(borrow p)
3940
// CHECK-NEXT: func lifetimeDependentBorrow(_ p: borrowing UnsafeBufferPointer<CInt>, _ len2: CInt) -> Span<CInt> {
40-
// CHECK-NEXT: return unsafe _swiftifyOverrideLifetime(Span<CInt> (_unsafeStart: unsafe lifetimeDependentBorrow(p.baseAddress!, CInt(exactly: p.count)!, len2), count: Int(len2)), copying: ())
41+
// CHECK-NEXT: let `len1` = CInt(exactly: unsafe p.count)!
42+
// CHECK-NEXT: return unsafe _swiftifyOverrideLifetime(Span<CInt> (_unsafeStart: unsafe lifetimeDependentBorrow(p.baseAddress!, len1, len2), count: Int(len2)), copying: ())
4143
// CHECK-NEXT: }
42-

test/Macros/SwiftifyImport/CountedBy/QualifiedTypes.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ func bar(_ ptr: Swift.UnsafePointer<Swift.CInt>, _ len: Swift.Int) -> () {
1212

1313
// CHECK: @_alwaysEmitIntoClient
1414
// CHECK-NEXT: func foo(_ ptr: Swift.UnsafeBufferPointer<Swift.Int>) -> Swift.Void {
15-
// CHECK-NEXT: return unsafe foo(ptr.baseAddress!, ptr.count)
15+
// CHECK-NEXT: let `len` = unsafe ptr.count
16+
// CHECK-NEXT: return unsafe foo(ptr.baseAddress!, len)
1617
// CHECK-NEXT: }
1718

1819
// CHECK: @_alwaysEmitIntoClient
1920
// CHECK-NEXT: func bar(_ ptr: Swift.UnsafeBufferPointer<Swift.CInt>) -> () {
20-
// CHECK-NEXT: return unsafe bar(ptr.baseAddress!, ptr.count)
21+
// CHECK-NEXT: let `len` = unsafe ptr.count
22+
// CHECK-NEXT: return unsafe bar(ptr.baseAddress!, len)
2123
// CHECK-NEXT: }
22-
23-

test/Macros/SwiftifyImport/CountedBy/Return.swift

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

99
// CHECK: @_alwaysEmitIntoClient
1010
// CHECK-NEXT: func myFunc(_ ptr: UnsafeBufferPointer<CInt>) -> 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: }

0 commit comments

Comments
 (0)