Skip to content

[cxx-interop] Avoid generating ambiguous wrapper functions #79012

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions lib/Macros/Sources/SwiftMacros/SwiftifyImportMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -289,8 +289,10 @@ func transformType(_ prev: TypeSyntax, _ generateSpan: Bool, _ isSizedBy: Bool)
protocol BoundsCheckedThunkBuilder {
func buildFunctionCall(_ pointerArgs: [Int: ExprSyntax]) throws -> ExprSyntax
func buildBoundsChecks() throws -> [CodeBlockItemSyntax.Item]
// The second component of the return value is true when only the return type of the
// function signature was changed.
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax
-> (FunctionSignatureSyntax, Bool)
}

func getParam(_ signature: FunctionSignatureSyntax, _ paramIndex: Int) -> FunctionParameterSyntax {
Expand All @@ -308,6 +310,7 @@ func getParam(_ funcDecl: FunctionDeclSyntax, _ paramIndex: Int) -> FunctionPara

struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
let base: FunctionDeclSyntax

init(_ function: FunctionDeclSyntax) {
base = function
}
Expand All @@ -317,7 +320,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
}

func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax {
-> (FunctionSignatureSyntax, Bool) {
var newParams = base.signature.parameterClause.parameters.enumerated().filter {
let type = argTypes[$0.offset]
// filter out deleted parameters, i.e. ones where argTypes[i] _contains_ nil
Expand All @@ -333,7 +336,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
if returnType != nil {
sig = sig.with(\.returnClause!.type, returnType!)
}
return sig
return (sig, (argTypes.count == 0 && returnType != nil))
}

func buildFunctionCall(_ pointerArgs: [Int: ExprSyntax]) throws -> ExprSyntax {
Expand Down Expand Up @@ -381,7 +384,7 @@ struct CxxSpanThunkBuilder: ParamPointerBoundsThunkBuilder {
}

func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax {
-> (FunctionSignatureSyntax, Bool) {
var types = argTypes
let typeName = try getTypeName(oldType).text
guard let desugaredType = typeMappings[typeName] else {
Expand Down Expand Up @@ -417,7 +420,7 @@ struct CxxSpanReturnThunkBuilder: BoundsCheckedThunkBuilder {
}

func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax {
-> (FunctionSignatureSyntax, Bool) {
assert(returnType == nil)
let typeName = try getTypeName(signature.returnClause!.type).text
guard let desugaredType = typeMappings[typeName] else {
Expand Down Expand Up @@ -490,7 +493,7 @@ struct CountedOrSizedReturnPointerThunkBuilder: PointerBoundsThunkBuilder {
}

func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax {
-> (FunctionSignatureSyntax, Bool) {
assert(returnType == nil)
return try base.buildFunctionSignature(argTypes, newType)
}
Expand Down Expand Up @@ -518,7 +521,7 @@ struct CountedOrSizedPointerThunkBuilder: ParamPointerBoundsThunkBuilder {
public let skipTrivialCount: Bool

func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> FunctionSignatureSyntax {
-> (FunctionSignatureSyntax, Bool) {
var types = argTypes
types[index] = try newType
if skipTrivialCount {
Expand Down Expand Up @@ -1104,7 +1107,7 @@ public struct SwiftifyImportMacro: PeerMacro {
{ (prev, parsedArg) in
parsedArg.getBoundsCheckedThunkBuilder(prev, funcDecl, skipTrivialCount)
})
let newSignature = try builder.buildFunctionSignature([:], nil)
let (newSignature, onlyReturnTypeChanged) = try builder.buildFunctionSignature([:], nil)
let checks =
skipTrivialCount
? [] as [CodeBlockItemSyntax]
Expand All @@ -1118,6 +1121,12 @@ public struct SwiftifyImportMacro: PeerMacro {
expression: try builder.buildFunctionCall([:]))))
let body = CodeBlockSyntax(statements: CodeBlockItemListSyntax(checks + [call]))
let lifetimeAttrs = lifetimeAttributes(funcDecl, lifetimeDependencies)
let disfavoredOverload : [AttributeListSyntax.Element] = (onlyReturnTypeChanged ? [
.attribute(
AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: "_disfavoredOverload")))
] : [])
let newFunc =
funcDecl
.with(\.signature, newSignature)
Expand All @@ -1138,7 +1147,8 @@ public struct SwiftifyImportMacro: PeerMacro {
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: "_alwaysEmitIntoClient")))
]
+ lifetimeAttrs)
+ lifetimeAttrs
+ disfavoredOverload)
return [DeclSyntax(newFunc)]
} catch let error as DiagnosticError {
context.diagnose(
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/C/swiftify-import/counted-by-noescape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import CountedByNoEscapeClang
// CHECK: @_alwaysEmitIntoClient public func complexExpr(_ len: Int{{.*}}, _ offset: Int{{.*}}, _ p: MutableSpan<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ p: MutableSpan<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: MutableSpan<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableBufferPointer<Int{{.*}}>
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableBufferPointer<Int{{.*}}>
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: MutableSpan<Int{{.*}}>, _ p2: MutableSpan<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: MutableSpan<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: MutableSpan<Int{{.*}}>)
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/C/swiftify-import/counted-by.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import CountedByClang
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ p: UnsafeMutableBufferPointer<Int{{.*}}>?)
// CHECK-NEXT: @_alwaysEmitIntoClient public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableBufferPointer<Int{{.*}}>
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableBufferPointer<Int{{.*}}>
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableBufferPointer<Int{{.*}}>, _ p2: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: UnsafeMutableBufferPointer<Int{{.*}}>)
Expand Down
2 changes: 1 addition & 1 deletion test/Interop/C/swiftify-import/sized-by-noescape.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SizedByNoEscapeClang
// CHECK-NEXT: @_alwaysEmitIntoClient public func nonnull(_ p: RawSpan)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: RawSpan)
// CHECK-NEXT: @_alwaysEmitIntoClient public func opaque(_ p: RawSpan)
// CHECK-NEXT: @_alwaysEmitIntoClient public func returnPointer(_ len: Int{{.*}}) -> UnsafeRawBufferPointer
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeRawBufferPointer
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: RawSpan, _ p2: RawSpan)
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: RawSpan)
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: RawSpan)
2 changes: 1 addition & 1 deletion test/Interop/C/swiftify-import/sized-by.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import SizedByClang
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullUnspecified(_ p: UnsafeMutableRawBufferPointer)
// CHECK-NEXT: @_alwaysEmitIntoClient public func nullable(_ p: UnsafeMutableRawBufferPointer?)
// CHECK-NEXT: @_alwaysEmitIntoClient public func opaque(_ p: UnsafeRawBufferPointer)
// CHECK-NEXT: @_alwaysEmitIntoClient public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableRawBufferPointer
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func returnPointer(_ len: Int{{.*}}) -> UnsafeMutableRawBufferPointer
// CHECK-NEXT: @_alwaysEmitIntoClient public func shared(_ len: Int{{.*}}, _ p1: UnsafeMutableRawBufferPointer, _ p2: UnsafeMutableRawBufferPointer)
// CHECK-NEXT: @_alwaysEmitIntoClient public func simple(_ p: UnsafeMutableRawBufferPointer)
// CHECK-NEXT: @_alwaysEmitIntoClient public func swiftAttr(_ p: UnsafeMutableRawBufferPointer)
Expand Down
4 changes: 2 additions & 2 deletions test/Interop/Cxx/stdlib/std-span-interface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import CxxStdlib

// CHECK: struct DependsOnSelf {
// CHECK: @lifetime(borrow self)
// CHECK-NEXT: @_alwaysEmitIntoClient public mutating func get() -> Span<CInt>
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public mutating func get() -> Span<CInt>
// CHECK-NEXT: mutating func get() -> ConstSpanOfInt

// CHECK: func funcWithSafeWrapper(_ s: ConstSpanOfInt)
Expand All @@ -31,4 +31,4 @@ import CxxStdlib
// CHECK-NEXT: @lifetime(s)
// CHECK-NEXT: @_alwaysEmitIntoClient public func funcWithSafeWrapper2(_ s: borrowing Span<CInt>) -> Span<CInt>
// CHECK-NEXT: @lifetime(borrow v)
// CHECK-NEXT: @_alwaysEmitIntoClient public func funcWithSafeWrapper3(_ v: borrowing VecOfInt) -> Span<CInt>
// CHECK-NEXT: @_alwaysEmitIntoClient @_disfavoredOverload public func funcWithSafeWrapper3(_ v: borrowing VecOfInt) -> Span<CInt>
4 changes: 2 additions & 2 deletions test/Macros/SwiftifyImport/CountedBy/PointerReturn.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ func myFunc(_ len: CInt) -> UnsafeMutablePointer<CInt> {
func nonEscaping(_ len: CInt) -> UnsafePointer<CInt> {
}

// CHECK: @_alwaysEmitIntoClient
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func myFunc(_ len: CInt) -> UnsafeMutableBufferPointer<CInt> {
// CHECK-NEXT: return UnsafeMutableBufferPointer<CInt> (start: myFunc(len), count: Int(len))
// CHECK-NEXT: }

// CHECK: @_alwaysEmitIntoClient
// CHECK: @_alwaysEmitIntoClient @_disfavoredOverload
// CHECK-NEXT: func nonEscaping(_ len: CInt) -> UnsafeBufferPointer<CInt> {
// CHECK-NEXT: return UnsafeBufferPointer<CInt> (start: nonEscaping(len), count: Int(len))
// CHECK-NEXT: }
Expand Down
4 changes: 2 additions & 2 deletions test/Macros/SwiftifyImport/CxxSpan/LifetimeboundSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ struct X {
// CHECK-NEXT: return Span(_unsafeCxxSpan: myFunc(SpanOfInt(span)))
// CHECK-NEXT: }

// CHECK: @_alwaysEmitIntoClient @lifetime(borrow vec)
// CHECK: @_alwaysEmitIntoClient @lifetime(borrow vec) @_disfavoredOverload
// CHECK-NEXT: func myFunc2(_ vec: borrowing VecOfInt) -> Span<CInt> {
// CHECK-NEXT: return Span(_unsafeCxxSpan: myFunc2(vec))
// CHECK-NEXT: }
Expand All @@ -54,7 +54,7 @@ struct X {
// CHECK-NEXT: return Span(_unsafeCxxSpan: myFunc4(vec, SpanOfInt(span)))
// CHECK-NEXT: }

// CHECK: @_alwaysEmitIntoClient @lifetime(borrow self)
// CHECK: @_alwaysEmitIntoClient @lifetime(borrow self) @_disfavoredOverload
// CHECK-NEXT: func myFunc5() -> Span<CInt> {
// CHECK-NEXT: return Span(_unsafeCxxSpan: myFunc5())
// CHECK-NEXT: }