@@ -289,8 +289,10 @@ func transformType(_ prev: TypeSyntax, _ generateSpan: Bool, _ isSizedBy: Bool)
289
289
protocol BoundsCheckedThunkBuilder {
290
290
func buildFunctionCall( _ pointerArgs: [ Int : ExprSyntax ] ) throws -> ExprSyntax
291
291
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.
292
294
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
293
- -> FunctionSignatureSyntax
295
+ -> ( FunctionSignatureSyntax , Bool )
294
296
}
295
297
296
298
func getParam( _ signature: FunctionSignatureSyntax , _ paramIndex: Int ) -> FunctionParameterSyntax {
@@ -308,6 +310,7 @@ func getParam(_ funcDecl: FunctionDeclSyntax, _ paramIndex: Int) -> FunctionPara
308
310
309
311
struct FunctionCallBuilder : BoundsCheckedThunkBuilder {
310
312
let base : FunctionDeclSyntax
313
+
311
314
init ( _ function: FunctionDeclSyntax ) {
312
315
base = function
313
316
}
@@ -317,7 +320,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
317
320
}
318
321
319
322
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
320
- -> FunctionSignatureSyntax {
323
+ -> ( FunctionSignatureSyntax , Bool ) {
321
324
var newParams = base. signature. parameterClause. parameters. enumerated ( ) . filter {
322
325
let type = argTypes [ $0. offset]
323
326
// filter out deleted parameters, i.e. ones where argTypes[i] _contains_ nil
@@ -333,7 +336,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
333
336
if returnType != nil {
334
337
sig = sig. with ( \. returnClause!. type, returnType!)
335
338
}
336
- return sig
339
+ return ( sig, ( argTypes . count == 0 && returnType != nil ) )
337
340
}
338
341
339
342
func buildFunctionCall( _ pointerArgs: [ Int : ExprSyntax ] ) throws -> ExprSyntax {
@@ -381,7 +384,7 @@ struct CxxSpanThunkBuilder: ParamPointerBoundsThunkBuilder {
381
384
}
382
385
383
386
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
384
- -> FunctionSignatureSyntax {
387
+ -> ( FunctionSignatureSyntax , Bool ) {
385
388
var types = argTypes
386
389
let typeName = try getTypeName ( oldType) . text
387
390
guard let desugaredType = typeMappings [ typeName] else {
@@ -417,7 +420,7 @@ struct CxxSpanReturnThunkBuilder: BoundsCheckedThunkBuilder {
417
420
}
418
421
419
422
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
420
- -> FunctionSignatureSyntax {
423
+ -> ( FunctionSignatureSyntax , Bool ) {
421
424
assert ( returnType == nil )
422
425
let typeName = try getTypeName ( signature. returnClause!. type) . text
423
426
guard let desugaredType = typeMappings [ typeName] else {
@@ -490,7 +493,7 @@ struct CountedOrSizedReturnPointerThunkBuilder: PointerBoundsThunkBuilder {
490
493
}
491
494
492
495
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
493
- -> FunctionSignatureSyntax {
496
+ -> ( FunctionSignatureSyntax , Bool ) {
494
497
assert ( returnType == nil )
495
498
return try base. buildFunctionSignature ( argTypes, newType)
496
499
}
@@ -518,7 +521,7 @@ struct CountedOrSizedPointerThunkBuilder: ParamPointerBoundsThunkBuilder {
518
521
public let skipTrivialCount : Bool
519
522
520
523
func buildFunctionSignature( _ argTypes: [ Int : TypeSyntax ? ] , _ returnType: TypeSyntax ? ) throws
521
- -> FunctionSignatureSyntax {
524
+ -> ( FunctionSignatureSyntax , Bool ) {
522
525
var types = argTypes
523
526
types [ index] = try newType
524
527
if skipTrivialCount {
@@ -1104,7 +1107,7 @@ public struct SwiftifyImportMacro: PeerMacro {
1104
1107
{ ( prev, parsedArg) in
1105
1108
parsedArg. getBoundsCheckedThunkBuilder ( prev, funcDecl, skipTrivialCount)
1106
1109
} )
1107
- let newSignature = try builder. buildFunctionSignature ( [ : ] , nil )
1110
+ let ( newSignature, onlyReturnTypeChanged ) = try builder. buildFunctionSignature ( [ : ] , nil )
1108
1111
let checks =
1109
1112
skipTrivialCount
1110
1113
? [ ] as [ CodeBlockItemSyntax ]
@@ -1118,6 +1121,12 @@ public struct SwiftifyImportMacro: PeerMacro {
1118
1121
expression: try builder. buildFunctionCall ( [ : ] ) ) ) )
1119
1122
let body = CodeBlockSyntax ( statements: CodeBlockItemListSyntax ( checks + [ call] ) )
1120
1123
let lifetimeAttrs = lifetimeAttributes ( funcDecl, lifetimeDependencies)
1124
+ let disfavoredOverload : [ AttributeListSyntax . Element ] = ( onlyReturnTypeChanged ? [
1125
+ . attribute(
1126
+ AttributeSyntax (
1127
+ atSign: . atSignToken( ) ,
1128
+ attributeName: IdentifierTypeSyntax ( name: " _disfavoredOverload " ) ) )
1129
+ ] : [ ] )
1121
1130
let newFunc =
1122
1131
funcDecl
1123
1132
. with ( \. signature, newSignature)
@@ -1138,7 +1147,8 @@ public struct SwiftifyImportMacro: PeerMacro {
1138
1147
atSign: . atSignToken( ) ,
1139
1148
attributeName: IdentifierTypeSyntax ( name: " _alwaysEmitIntoClient " ) ) )
1140
1149
]
1141
- + lifetimeAttrs)
1150
+ + lifetimeAttrs
1151
+ + disfavoredOverload)
1142
1152
return [ DeclSyntax ( newFunc) ]
1143
1153
} catch let error as DiagnosticError {
1144
1154
context. diagnose (
0 commit comments