Skip to content

Commit 629314d

Browse files
authored
Avoid warning when embedding Never cases (#179)
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).
1 parent b9ad266 commit 629314d

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

Package.resolved

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Sources/CasePathsMacros/CasePathableMacro.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,14 @@ extension CasePathableMacro: MemberMacro {
204204
.map { String($0.dropFirst(indent)) }
205205
.joined(separator: "\n")
206206
.trimmingSuffix(while: { $0.isWhitespace && !$0.isNewline })
207+
let embed: DeclSyntax = ["Never", "Swift.Never"].contains(associatedValueName)
208+
? " _ -> \(enumName) in "
209+
: "\(enumName).\(caseName)\(raw: embedNames)"
207210
return """
208211
\(raw: leadingTrivia)public var \(caseName): \
209212
\(raw: casePathTypeName.qualified)<\(enumName), \(raw: associatedValueName)> {
210213
\(raw: casePathTypeName.qualified)<\(enumName), \(raw: associatedValueName)>(
211-
embed: { \(enumName).\(caseName)\(raw: embedNames) },
214+
embed: { \(embed) },
212215
extract: {
213216
guard case\(raw: hasPayload ? " let" : "").\(caseName)\(raw: bindingNames) = $0 else { \
214217
return nil \

Tests/CasePathsMacrosTests/CasePathableMacroTests.swift

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,52 @@ final class CasePathableMacroTests: XCTestCase {
144144
}
145145
}
146146

147+
func testCasePathable_NeverCase() {
148+
assertMacro {
149+
"""
150+
@CasePathable enum Foo {
151+
case bar(Never)
152+
}
153+
"""
154+
} expansion: {
155+
#"""
156+
enum Foo {
157+
case bar(Never)
158+
159+
public struct AllCasePaths: CasePaths.CasePathReflectable, Sendable, Sequence {
160+
public subscript(root: Foo) -> CasePaths.PartialCaseKeyPath<Foo> {
161+
if root.is(\.bar) {
162+
return \.bar
163+
}
164+
return \.never
165+
}
166+
public var bar: CasePaths.AnyCasePath<Foo, Never> {
167+
CasePaths.AnyCasePath<Foo, Never>(
168+
embed: { _ -> Foo in
169+
},
170+
extract: {
171+
guard case let .bar(v0) = $0 else {
172+
return nil
173+
}
174+
return v0
175+
}
176+
)
177+
}
178+
public func makeIterator() -> IndexingIterator<[CasePaths.PartialCaseKeyPath<Foo>]> {
179+
var allCasePaths: [CasePaths.PartialCaseKeyPath<Foo>] = []
180+
allCasePaths.append(\.bar)
181+
return allCasePaths.makeIterator()
182+
}
183+
}
184+
public static var allCasePaths: AllCasePaths { AllCasePaths() }
185+
}
186+
187+
extension Foo: CasePaths.CasePathable, CasePaths.CasePathIterable {
188+
}
189+
"""#
190+
}
191+
}
192+
147193
func testCasePathable_ElementList() {
148194
assertMacro {
149195
"""

0 commit comments

Comments
 (0)