Skip to content

Commit

Permalink
Annotate event adders as @discardableResult (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanlabelle authored Nov 1, 2024
1 parent 9a764d6 commit 00872a0
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 1 deletion.
5 changes: 4 additions & 1 deletion Generator/Sources/CodeWriters/Swift/SwiftAttribute.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
public struct SwiftAttribute {
public struct SwiftAttribute: ExpressibleByStringLiteral {
public var literal: String
public init(_ literal: String) { self.literal = literal }
public init(stringLiteral literal: String) { self.literal = literal }

public static var discardableResult: SwiftAttribute { "discardableResult" }
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fileprivate func writeProtocol(_ interfaceDefinition: InterfaceDefinition, proje
if let addAccessor = try event.addAccessor {
try writer.writeFunc(
documentation: projection.getDocumentationComment(event),
attributes: [ .discardableResult ],
name: Projection.toMemberName(event),
params: addAccessor.params.map { try projection.toParameter(label: "adding", $0) },
throws: true,
Expand Down
2 changes: 2 additions & 0 deletions Generator/Sources/SwiftWinRT/Writing/MemberDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,14 @@ fileprivate func writeEventDefinition(
projection: Projection, to writer: SwiftTypeDefinitionWriter) throws {
let name = Projection.toMemberName(event)

// @discardableResult
// public [static] func myEvent(adding handler: @escaping MyEventHandler) throws -> EventRegistration { ... }
if let addAccessor = try event.addAccessor, let handlerParameter = try addAccessor.params.first {
let handlerParamBinding = try projection.getParamBinding(handlerParameter, genericTypeArgs: typeGenericArgs)
let eventRegistrationType = SupportModules.WinRT.eventRegistration
try writer.writeFunc(
documentation: documentation ? projection.getDocumentationComment(event, classDefinition: classDefinition) : nil,
attributes: [ .discardableResult ],
visibility: overridable ? .open : .public, static: `static`,
name: name,
params: [ handlerParamBinding.toSwiftParam(label: "adding") ], throws: true,
Expand Down
6 changes: 6 additions & 0 deletions InteropTests/Tests/EventTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EventTests: WinRTTestCase {
class EventSource: WinRTPrimaryExport<IEventSourceBinding>, IEventSourceProtocol {
private var invocationList: EventInvocationList<MinimalDelegate> = .init()

@discardableResult
func event(adding handler: MinimalDelegate?) throws -> EventRegistration {
try invocationList.add(handler)
}
Expand All @@ -44,4 +45,9 @@ class EventTests: WinRTTestCase {
}
}
}

func testDiscardableResult() throws {
let eventSource = try XCTUnwrap(Events.createSource())
try eventSource.event { } // Should not produce a warning/error
}
}

0 comments on commit 00872a0

Please sign in to comment.