From a672dc02521eeee5dcbac9891a4d91a1a3f040ed Mon Sep 17 00:00:00 2001 From: Tristan Labelle Date: Sun, 18 Feb 2024 22:06:59 -0500 Subject: [PATCH] Use @retroactive only with Swift language versions which require it --- Generator/Sources/ProjectionGenerator/SupportModule.swift | 1 + .../Sources/SwiftWinRT/Writing/COMInteropExtension.swift | 8 +++++++- .../Sources/SwiftWinRT/Writing/ClassDefinition.swift | 1 + .../SwiftWinRT/Writing/InterfaceImplementation.swift | 5 +++++ 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/Generator/Sources/ProjectionGenerator/SupportModule.swift b/Generator/Sources/ProjectionGenerator/SupportModule.swift index bc99d077..bd0630d0 100644 --- a/Generator/Sources/ProjectionGenerator/SupportModule.swift +++ b/Generator/Sources/ProjectionGenerator/SupportModule.swift @@ -26,6 +26,7 @@ public enum SupportModule { extension SupportModule { public static var winrtModuleName: String { "WindowsRuntime" } + public static var comIInspectableStruct: SwiftType { .chain(winrtModuleName, "COMIInspectableStruct") } public static var eventRegistration: SwiftType { .chain(winrtModuleName, "EventRegistration") } public static var eventRegistrationToken: SwiftType { .chain(winrtModuleName, "EventRegistrationToken") } public static var hstringProjection: SwiftType { .chain(winrtModuleName, "HStringProjection") } diff --git a/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift b/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift index 50f2b338..cc108f27 100644 --- a/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift +++ b/Generator/Sources/SwiftWinRT/Writing/COMInteropExtension.swift @@ -69,7 +69,13 @@ fileprivate func writeCOMInteropExtension(abiType: BoundType, abiName: String, m if abiType.definition is InterfaceDefinition { // COM interfaces for WinRT interfaces are IInspectable-compatible. This is not true for delegates. - writer.output.writeFullLine(grouping: .never, "extension \(qualifiedAbiName): @retroactive WindowsRuntime.COMIInspectableStruct {}") + // @retroactive is only supported in Swift 5.10 and above. + let group = writer.output.allocateVerticalGrouping() + writer.output.writeFullLine(grouping: group, "#if swift(>=5.10)") + writer.output.writeFullLine(grouping: group, "extension \(qualifiedAbiName): @retroactive \(SupportModule.comIInspectableStruct) {}") + writer.output.writeFullLine(grouping: group, "#else") + writer.output.writeFullLine(grouping: group, "extension \(qualifiedAbiName): \(SupportModule.comIInspectableStruct) {}") + writer.output.writeFullLine(grouping: group, "#endif") } try writer.writeExtension(name: "COMInterop", whereClauses: [ "Interface == \(qualifiedAbiName)" ]) { writer in diff --git a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift index a42685ae..87d4cf5b 100644 --- a/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift +++ b/Generator/Sources/SwiftWinRT/Writing/ClassDefinition.swift @@ -151,6 +151,7 @@ fileprivate func writeActivationFactoryInitializers( for method in activationFactory.methods { let (params, returnParam) = try projection.getParamProjections(method: method, genericTypeArgs: []) try writer.writeInit( + documentation: try projection.getDocumentationComment(method), visibility: .public, convenience: true, params: params.map { $0.toSwiftParam() }, diff --git a/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift b/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift index c9f7874c..8b0ffd81 100644 --- a/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift +++ b/Generator/Sources/SwiftWinRT/Writing/InterfaceImplementation.swift @@ -45,6 +45,7 @@ fileprivate func writeInterfacePropertyImplementation( if let getter = try property.getter, try getter.hasReturnValue { let returnParamProjection = try projection.getParamProjection(getter.returnParam, genericTypeArgs: typeGenericArgs) try writer.writeComputedProperty( + documentation: try projection.getDocumentationComment(property), visibility: .public, static: `static`, name: SwiftProjection.toMemberName(property), @@ -61,6 +62,7 @@ fileprivate func writeInterfacePropertyImplementation( guard let newValueParam = try setter.params.first else { fatalError() } let newValueParamProjection = try projection.getParamProjection(newValueParam, genericTypeArgs: typeGenericArgs) try writer.writeFunc( + documentation: try projection.getDocumentationComment(property), visibility: .public, static: `static`, name: SwiftProjection.toMemberName(property), @@ -84,6 +86,7 @@ fileprivate func writeInterfaceEventImplementation( let handlerParamProjection = try projection.getParamProjection(handlerParameter, genericTypeArgs: typeGenericArgs) let eventRegistrationType = SupportModule.eventRegistration try writer.writeFunc( + documentation: try projection.getDocumentationComment(event), visibility: .public, static: `static`, name: name, params: [ handlerParamProjection.toSwiftParam(label: "adding") ], throws: true, returnType: eventRegistrationType) { writer throws in @@ -104,6 +107,7 @@ fileprivate func writeInterfaceEventImplementation( if let removeAccessor = try event.removeAccessor, let tokenParameter = try removeAccessor.params.first { let tokenParamProjection = try projection.getParamProjection(tokenParameter, genericTypeArgs: typeGenericArgs) try writer.writeFunc( + documentation: try projection.getDocumentationComment(event), visibility: .public, static: `static`, name: name, @@ -122,6 +126,7 @@ fileprivate func writeInterfaceMethodImplementation( projection: SwiftProjection, to writer: SwiftTypeDefinitionWriter) throws { let (params, returnParam) = try projection.getParamProjections(method: method, genericTypeArgs: typeGenericArgs) try writer.writeFunc( + documentation: try projection.getDocumentationComment(method), visibility: .public, static: `static`, name: SwiftProjection.toMemberName(method),