[Swiftify] Fix return value transformation being applied last

Casting the return value to Span must be done outside
withUnsafeBufferPointer, to prevent returning a ~Escapable type from a
function without lifetime info. To do this we sort the transformations
so that the return value transformation is performed last. There was
a bug in the comparison, so the sorting was not always done correctly.

rdar://147934170
This commit is contained in:
Henrik G. Olsson
2025-03-25 17:06:13 -07:00
parent c7af94ba07
commit 160bb41ec2
2 changed files with 47 additions and 6 deletions

View File

@@ -1142,7 +1142,13 @@ public struct SwiftifyImportMacro: PeerMacro {
parsedArgs.sort { a, b in
// make sure return value cast to Span happens last so that withUnsafeBufferPointer
// doesn't return a ~Escapable type
(a.pointerIndex != .return && b.pointerIndex == .return) || paramOrReturnIndex(a.pointerIndex) < paramOrReturnIndex(b.pointerIndex)
if a.pointerIndex != .return && b.pointerIndex == .return {
return true
}
if a.pointerIndex == .return && b.pointerIndex != .return {
return false
}
return paramOrReturnIndex(a.pointerIndex) < paramOrReturnIndex(b.pointerIndex)
}
let baseBuilder = FunctionCallBuilder(funcDecl)