CxxSpanReturnThunkBuilder: use _cxxOverrideLifetime(_:copying:)

Do not rely on the @_unsafeNonescapableResult attribute. That attribute is only
for temporarily working around bugs! And it only affects lifetime diagnostics within
the function. It has no affect on the caller's diagnostics, so it won't solve
this problem:

func macroGeneratedThunk() -> CxxSpan<Int> {
  return _unsafeRemoveLifetime(Span...)
}

We cannot simply add @_unsafeRemoveLifetime to the thunk, because SwiftSyntax
does not natively support the attribute. We don't want to add SwiftSyntax
support because this attribute will never be supported syntax!

Instead, use `_overrideLifetime` copying the `Void` type to remove a dependency:

func macroGeneratedThunk() -> CxxSpan<Int> {
  return _cxxOverrideLifetime(Span..., copying: ())
}
This commit is contained in:
Andrew Trick
2025-03-19 08:51:57 -07:00
parent ed19f7a149
commit 955d089a90
3 changed files with 30 additions and 18 deletions

View File

@@ -468,7 +468,7 @@ struct CxxSpanReturnThunkBuilder: BoundsCheckedThunkBuilder {
func buildFunctionCall(_ pointerArgs: [Int: ExprSyntax]) throws -> ExprSyntax {
let call = try base.buildFunctionCall(pointerArgs)
return "_unsafeRemoveLifetime(Span(_unsafeCxxSpan: \(call)))"
return "_cxxOverrideLifetime(Span(_unsafeCxxSpan: \(call)), copying: ())"
}
}