[Swiftify] Always annotate overloads with @_disfavoredOverload (#81579)

Previously we would only add @_disfavoredOverload if the only type
changed was the return type, because in any other case it is unambiguous
which overload to call. However it is still ambiguous when storing the
function as a value rather than calling the function, unless explicit
type annotations are used.

To avoid breaking any existing code, this patch adds
@_disfavoredOverload to every overload generated by @_SwiftifyImport.

rdar://151206394
This commit is contained in:
Henrik G. Olsson
2025-05-23 21:21:49 -07:00
committed by GitHub
parent 52f54534f3
commit 0f312adb92
43 changed files with 187 additions and 178 deletions

View File

@@ -381,10 +381,8 @@ func isMutablePointerType(_ type: TypeSyntax) -> 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, Bool)
-> FunctionSignatureSyntax
}
func getParam(_ signature: FunctionSignatureSyntax, _ paramIndex: Int) -> FunctionParameterSyntax {
@@ -412,7 +410,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
}
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> (FunctionSignatureSyntax, Bool)
-> FunctionSignatureSyntax
{
var newParams = base.signature.parameterClause.parameters.enumerated().filter {
let type = argTypes[$0.offset]
@@ -430,7 +428,7 @@ struct FunctionCallBuilder: BoundsCheckedThunkBuilder {
if returnType != nil {
sig = sig.with(\.returnClause!.type, returnType!)
}
return (sig, (argTypes.count == 0 && returnType != nil))
return sig
}
func buildFunctionCall(_ pointerArgs: [Int: ExprSyntax]) throws -> ExprSyntax {
@@ -479,7 +477,7 @@ struct CxxSpanThunkBuilder: SpanBoundsThunkBuilder, ParamBoundsThunkBuilder {
}
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> (FunctionSignatureSyntax, Bool)
-> FunctionSignatureSyntax
{
var types = argTypes
types[index] = try newType
@@ -529,7 +527,7 @@ struct CxxSpanReturnThunkBuilder: SpanBoundsThunkBuilder {
}
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> (FunctionSignatureSyntax, Bool)
-> FunctionSignatureSyntax
{
assert(returnType == nil)
return try base.buildFunctionSignature(argTypes, newType)
@@ -669,7 +667,7 @@ struct CountedOrSizedReturnPointerThunkBuilder: PointerBoundsThunkBuilder {
}
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> (FunctionSignatureSyntax, Bool)
-> FunctionSignatureSyntax
{
assert(returnType == nil)
return try base.buildFunctionSignature(argTypes, newType)
@@ -730,7 +728,7 @@ struct CountedOrSizedPointerThunkBuilder: ParamBoundsThunkBuilder, PointerBounds
var generateSpan: Bool { nonescaping }
func buildFunctionSignature(_ argTypes: [Int: TypeSyntax?], _ returnType: TypeSyntax?) throws
-> (FunctionSignatureSyntax, Bool)
-> FunctionSignatureSyntax
{
var types = argTypes
types[index] = try newType
@@ -1566,7 +1564,7 @@ public struct SwiftifyImportMacro: PeerMacro {
{ (prev, parsedArg) in
parsedArg.getBoundsCheckedThunkBuilder(prev, funcDecl, skipTrivialCount)
})
let (newSignature, onlyReturnTypeChanged) = try builder.buildFunctionSignature([:], nil)
let newSignature = try builder.buildFunctionSignature([:], nil)
let checks =
skipTrivialCount
? [] as [CodeBlockItemSyntax]
@@ -1584,13 +1582,12 @@ public struct SwiftifyImportMacro: PeerMacro {
returnLifetimeAttribute + paramLifetimeAttributes(newSignature, funcDecl.attributes)
let availabilityAttr = try getAvailability(newSignature, spanAvailability)
let disfavoredOverload: [AttributeListSyntax.Element] =
(onlyReturnTypeChanged
? [
.attribute(
AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: "_disfavoredOverload")))
] : [])
[
.attribute(
AttributeSyntax(
atSign: .atSignToken(),
attributeName: IdentifierTypeSyntax(name: "_disfavoredOverload")))
]
let newFunc =
funcDecl
.with(\.signature, newSignature)