Skip to content

Commit

Permalink
Use Point-Free syntax to avoid uninhabited warnings (#184)
Browse files Browse the repository at this point in the history
* Use Point-Free syntax to avoid uninhabited warnings

* wip

* bump

* wip

* wip
  • Loading branch information
stephencelis authored Jul 18, 2024
1 parent a774223 commit 12bc5b9
Show file tree
Hide file tree
Showing 5 changed files with 204 additions and 334 deletions.
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
"kind" : "remoteSourceControl",
"location" : "https://github.com/swiftlang/swift-syntax",
"state" : {
"revision" : "303e5c5c36d6a558407d364878df131c3546fad8",
"version" : "510.0.2"
"revision" : "4c6cc0a3b9e8f14b3ae2307c5ccae4de6167ac2c",
"version" : "600.0.0-prerelease-2024-06-12"
}
},
{
Expand Down
13 changes: 13 additions & 0 deletions Sources/CasePaths/AnyCasePath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@ public struct AnyCasePath<Root, Value>: Sendable {
self._extract = extract
}

public static func _$embed(
_ embed: @escaping (Value) -> Root,
extract: @escaping @Sendable (Root) -> Value?
) -> Self {
#if swift(>=5.10)
nonisolated(unsafe) let embed = embed
return Self(embed: { embed($0) }, extract: extract)
#else
@UncheckedSendable var embed = embed
return Self(embed: { [$embed] in $embed.wrappedValue($0) }, extract: extract)
#endif
}

/// Returns a root by embedding a value.
///
/// - Parameter value: A value to embed.
Expand Down
17 changes: 2 additions & 15 deletions Sources/CasePathsMacros/CasePathableMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,22 +185,16 @@ extension CasePathableMacro: MemberMacro {
? "_$Element"
: $0.trimmedTypeDescription
let hasPayload = $0.parameterClause.map { !$0.parameters.isEmpty } ?? false
let embedNames: String
let embed: DeclSyntax = hasPayload ? "\(enumName).\(caseName)" : "{ \(enumName).\(caseName) }"
let bindingNames: String
let returnName: String
if hasPayload, let associatedValue = $0.parameterClause {
embedNames =
"("
+ associatedValue.parameters.enumerated()
.map { "\($1.firstName.map { "\($0.text): " } ?? "")$\($0)" }
.joined(separator: ", ") + ")"
let parameterNames = (0..<associatedValue.parameters.count)
.map { "v\($0)" }
.joined(separator: ", ")
bindingNames = "(\(parameterNames))"
returnName = associatedValue.parameters.count == 1 ? parameterNames : bindingNames
} else {
embedNames = ""
bindingNames = ""
returnName = "()"
}
Expand All @@ -217,22 +211,15 @@ 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: { \(embed) },
extract: {
._$embed(\(embed)) {
guard case\(raw: hasPayload ? " let" : "").\(caseName)\(raw: bindingNames) = $0 else { \
return nil \
}
return \(raw: returnName)
}
)
}
"""
}
Expand Down
Loading

0 comments on commit 12bc5b9

Please sign in to comment.