Skip to content

Commit

Permalink
Avoid warning when embedding Never cases (#179)
Browse files Browse the repository at this point in the history
Sendability meant losing the `Enum.case` shorthand for
`{ Enum.case($0) }`, which meant introducing a warning when
`type(of: $0) == Never.self`. Looks like we can suppress this warning,
though, and it also luckily looks like this _only_ affects embedding
`Never` directly (embedding enums that only contain `Never` does not
trigger a warning).
  • Loading branch information
stephencelis authored Jul 16, 2024
1 parent b9ad266 commit 629314d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
{
"identity" : "swift-syntax",
"kind" : "remoteSourceControl",
"location" : "https://github.com/apple/swift-syntax",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
Expand Down
5 changes: 4 additions & 1 deletion Sources/CasePathsMacros/CasePathableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,14 @@ extension CasePathableMacro: MemberMacro {
.map { String($0.dropFirst(indent)) }
.joined(separator: "\n")
.trimmingSuffix(while: { $0.isWhitespace && !$0.isNewline })
let embed: DeclSyntax = ["Never", "Swift.Never"].contains(associatedValueName)
? " _ -> \(enumName) in "
: "\(enumName).\(caseName)\(raw: embedNames)"
return """
\(raw: leadingTrivia)public var \(caseName): \
\(raw: casePathTypeName.qualified)<\(enumName), \(raw: associatedValueName)> {
\(raw: casePathTypeName.qualified)<\(enumName), \(raw: associatedValueName)>(
embed: { \(enumName).\(caseName)\(raw: embedNames) },
embed: { \(embed) },
extract: {
guard case\(raw: hasPayload ? " let" : "").\(caseName)\(raw: bindingNames) = $0 else { \
return nil \
Expand Down
46 changes: 46 additions & 0 deletions Tests/CasePathsMacrosTests/CasePathableMacroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,52 @@ final class CasePathableMacroTests: XCTestCase {
}
}

func testCasePathable_NeverCase() {
assertMacro {
"""
@CasePathable enum Foo {
case bar(Never)
}
"""
} expansion: {
#"""
enum Foo {
case bar(Never)
public struct AllCasePaths: CasePaths.CasePathReflectable, Sendable, Sequence {
public subscript(root: Foo) -> CasePaths.PartialCaseKeyPath<Foo> {
if root.is(\.bar) {
return \.bar
}
return \.never
}
public var bar: CasePaths.AnyCasePath<Foo, Never> {
CasePaths.AnyCasePath<Foo, Never>(
embed: { _ -> Foo in
},
extract: {
guard case let .bar(v0) = $0 else {
return nil
}
return v0
}
)
}
public func makeIterator() -> IndexingIterator<[CasePaths.PartialCaseKeyPath<Foo>]> {
var allCasePaths: [CasePaths.PartialCaseKeyPath<Foo>] = []
allCasePaths.append(\.bar)
return allCasePaths.makeIterator()
}
}
public static var allCasePaths: AllCasePaths { AllCasePaths() }
}
extension Foo: CasePaths.CasePathable, CasePaths.CasePathIterable {
}
"""#
}
}

func testCasePathable_ElementList() {
assertMacro {
"""
Expand Down

0 comments on commit 629314d

Please sign in to comment.