Skip to content

Commit 0e5c929

Browse files
WojciechMazurdanslapman
authored andcommitted
Fix TupleLambda macros under Scala 3.10
1 parent 12c3502 commit 0e5c929

1 file changed

Lines changed: 24 additions & 6 deletions

File tree

  • oolong-bson/src/main/scala/oolong/bson/utils

oolong-bson/src/main/scala/oolong/bson/utils/utils.scala

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,29 @@ object AsTerm {
3333
object TupleLambda {
3434
def unapply(using quotes: Quotes)(expr: Expr[Any]): Option[(String, String)] =
3535
import quotes.reflect.*
36+
37+
object ArrowToLiteral {
38+
def unapply(body: Term): Option[(String, String)] =
39+
body match
40+
// x -> y : Scala 3.9 or earlier
41+
case Apply(
42+
TypeApply(Select(Apply(_, List(Select(_, first))), "->"), _),
43+
List(Literal(StringConstant(second)))
44+
) =>
45+
Some(first -> second)
46+
// Starting with Scala 3.10, inline `->` expands to a typed Tuple2.apply.
47+
// A direct `(field, "name")` is an untyped Apply and remains unsupported.
48+
case Typed(
49+
Apply(
50+
TypeApply(Select(receiver, "apply"), _),
51+
List(Select(_, first), Literal(StringConstant(second)))
52+
),
53+
_
54+
) if receiver.symbol == Symbol.requiredModule("scala.Tuple2") =>
55+
Some(first -> second)
56+
case _ => None
57+
}
58+
3659
expr match
3760
case AsTerm(
3861
Block(
@@ -41,12 +64,7 @@ object TupleLambda {
4164
"$anonfun",
4265
_,
4366
_,
44-
Some(
45-
Apply(
46-
TypeApply(Select(Apply(_, List(Select(_, first))), "->"), _),
47-
List(Literal(StringConstant(second)))
48-
)
49-
)
67+
Some(ArrowToLiteral(first, second))
5068
)
5169
),
5270
_

0 commit comments

Comments
 (0)