@@ -289,8 +289,10 @@ func transformType(_ prev: TypeSyntax, _ generateSpan: Bool, _ isSizedBy: Bool)
289289protocol BoundsCheckedThunkBuilder {
290290 func buildFunctionCall( _ pointerArgs: [ Int : ExprSyntax ] ) throws -> ExprSyntax
291291 func buildBoundsChecks( ) throws -> [ CodeBlockItemSyntax . Item ]
292+ // The second component of the return value is true when only the return type of the
293+ // function signature was changed.
292294 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
293- -> FunctionSignatureSyntax
295+ -> ( FunctionSignatureSyntax , Bool )
294296}
295297
296298func getParam( _ signature: FunctionSignatureSyntax , _ paramIndex: Int ) -> FunctionParameterSyntax {
@@ -308,6 +310,7 @@ func getParam(_ funcDecl: FunctionDeclSyntax, _ paramIndex: Int) -> FunctionPara
308310
309311struct FunctionCallBuilder : BoundsCheckedThunkBuilder {
310312 let base : FunctionDeclSyntax
313+
311314 init ( _ function: FunctionDeclSyntax ) {
312315 base = function
313316 }
@@ -317,7 +320,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
317320 }
318321
319322 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
320- -> FunctionSignatureSyntax {
323+ -> ( FunctionSignatureSyntax , Bool ) {
321324 var newParams = base. signature. parameterClause. parameters. enumerated ( ) . filter {
322325 let type = argTypes [ $0. offset]
323326 // filter out deleted parameters, i.e. ones where argTypes[i] _contains_ nil
@@ -333,7 +336,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
333336 if returnType != nil {
334337 sig = sig. with ( \. returnClause!. type, returnType!)
335338 }
336- return sig
339+ return ( sig, ( argTypes . count == 0 && returnType != nil ) )
337340 }
338341
339342 func buildFunctionCall( _ pointerArgs: [ Int : ExprSyntax ] ) throws -> ExprSyntax {
@@ -381,7 +384,7 @@ struct CxxSpanThunkBuilder: ParamPointerBoundsThunkBuilder {
381384 }
382385
383386 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
384- -> FunctionSignatureSyntax {
387+ -> ( FunctionSignatureSyntax , Bool ) {
385388 var types = argTypes
386389 let typeName = try getTypeName ( oldType) . text
387390 guard let desugaredType = typeMappings [ typeName] else {
@@ -417,7 +420,7 @@ struct CxxSpanReturnThunkBuilder: BoundsCheckedThunkBuilder {
417420 }
418421
419422 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
420- -> FunctionSignatureSyntax {
423+ -> ( FunctionSignatureSyntax , Bool ) {
421424 assert ( returnType == nil )
422425 let typeName = try getTypeName ( signature. returnClause!. type) . text
423426 guard let desugaredType = typeMappings [ typeName] else {
@@ -490,7 +493,7 @@ struct CountedOrSizedReturnPointerThunkBuilder: PointerBoundsThunkBuilder {
490493 }
491494
492495 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
493- -> FunctionSignatureSyntax {
496+ -> ( FunctionSignatureSyntax , Bool ) {
494497 assert ( returnType == nil )
495498 return try base. buildFunctionSignature ( argTypes, newType)
496499 }
@@ -518,7 +521,7 @@ struct CountedOrSizedPointerThunkBuilder: ParamPointerBoundsThunkBuilder {
518521 public let skipTrivialCount : Bool
519522
520523 func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
521- -> FunctionSignatureSyntax {
524+ -> ( FunctionSignatureSyntax , Bool ) {
522525 var types = argTypes
523526 types [ index] = try newType
524527 if skipTrivialCount {
@@ -1104,7 +1107,7 @@ public struct SwiftifyImportMacro: PeerMacro {
11041107 { ( prev, parsedArg) in
11051108 parsedArg. getBoundsCheckedThunkBuilder ( prev, funcDecl, skipTrivialCount)
11061109 } )
1107- let newSignature = try builder. buildFunctionSignature ( [ : ] , nil )
1110+ let ( newSignature, onlyReturnTypeChanged ) = try builder. buildFunctionSignature ( [ : ] , nil )
11081111 let checks =
11091112 skipTrivialCount
11101113 ? [ ] as [ CodeBlockItemSyntax ]
@@ -1118,6 +1121,12 @@ public struct SwiftifyImportMacro: PeerMacro {
11181121 expression: try builder. buildFunctionCall ( [ : ] ) ) ) )
11191122 let body = CodeBlockSyntax ( statements: CodeBlockItemListSyntax ( checks + [ call] ) )
11201123 let lifetimeAttrs = lifetimeAttributes ( funcDecl, lifetimeDependencies)
1124+ let disfavoredOverload : [ AttributeListSyntax . Element ] = ( onlyReturnTypeChanged ? [
1125+ . attribute(
1126+ AttributeSyntax (
1127+ atSign: . atSignToken( ) ,
1128+ attributeName: IdentifierTypeSyntax ( name: " _disfavoredOverload " ) ) )
1129+ ] : [ ] )
11211130 let newFunc =
11221131 funcDecl
11231132 . with ( \. signature, newSignature)
@@ -1138,7 +1147,8 @@ public struct SwiftifyImportMacro: PeerMacro {
11381147 atSign: . atSignToken( ) ,
11391148 attributeName: IdentifierTypeSyntax ( name: " _alwaysEmitIntoClient " ) ) )
11401149 ]
1141- + lifetimeAttrs)
1150+ + lifetimeAttrs
1151+ + disfavoredOverload)
11421152 return [ DeclSyntax ( newFunc) ]
11431153 } catch let error as DiagnosticError {
11441154 context. diagnose (
0 commit comments