Skip to content

Commit 0fe87eb

Browse files
committed
wip
1 parent 8c4ca02 commit 0fe87eb

File tree

4 files changed

+76
-9
lines changed

4 files changed

+76
-9
lines changed

library/src-bootstrapped/scala/runtime/quoted/Matcher.scala

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,10 @@ import scala.tasty._
88

99
object Matcher {
1010

11-
private final val debug = false
11+
private final val debug = true
1212

13-
// FIXME
14-
// /** Pattern representing a quoted.Type splice */
15-
// type Hole[T <: AnyKind] = T
13+
/** Pattern representing a quoted.Type splice */
14+
type Hole[T <: AnyKind] = T
1615

1716
/** Pattern representing a quoted.matching.Binding
1817
* The pattern is placed around the type tree of the `val`, `lazy val`, `var` or `def`
@@ -79,13 +78,12 @@ object Matcher {
7978
case (_, Block(Nil, pat)) => treeMatches(scrutinee, pat)
8079

8180
case (IsTerm(scrutinee), TypeApply(Ident("hole"), tpt :: Nil))
82-
if isMatcherHole(pattern.symbol) && scrutinee.tpe <:< tpt.tpe =>
81+
if isMatcherHole(pattern.symbol) =>
8382
Some(Tuple1(scrutinee.seal))
8483

85-
// FIXME
86-
// case (IsTypeTree(scrutinee), IsTypeTree(pattern @ TypeTree.Applied(TypeIdent("Hole"), IsTypeTree(tpt) :: Nil)))
87-
// if isMatcherHole(pattern.symbol) && scrutinee.tpe <:< tpt.tpe => // Is the subtype check required?
88-
// Some(Tuple1(scrutinee.tpe.seal))
84+
case (IsTypeTree(scrutinee), IsTypeTree(pattern @ TypeTree.Applied(TypeIdent("Hole"), IsTypeTree(tpt) :: Nil)))
85+
if isMatcherHole(pattern.symbol) => // Is the subtype check required?
86+
Some(Tuple1(scrutinee.tpe.seal))
8987

9088
case (Inlined(_, Nil, scr), _) =>
9189
treeMatches(scr, pattern)
@@ -199,6 +197,9 @@ object Matcher {
199197
val finalizerMatch = treeOptMatches(finalizer1, finalizer2)
200198
foldMatchings(bodyMacth, casesMatch, finalizerMatch)
201199

200+
case (_, Block(stats, expr)) if stats.forall { case IsTypeDef(s) if s.name.toString.startsWith("Binding$") => true; case _ => false } =>
201+
treeMatches(scrutinee, expr)
202+
202203
case _ =>
203204
if (debug)
204205
println(
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
g: 5
2+
f: abc
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
2+
import scala.quoted._
3+
import scala.quoted.matching._
4+
5+
import scala.tasty.Reflection
6+
7+
import scala.runtime.quoted.Matcher._
8+
9+
object Macros {
10+
11+
inline def swapFandG(x: => Unit): Unit = ${impl('x)}
12+
13+
private def impl(x: Expr[Unit])(implicit reflect: Reflection): Expr[Unit] = {
14+
15+
type TT // type binding
16+
object DSLf extends ExprMatch[Tuple2[Type[TT], Expr[TT]]]('{ type Binding$T1; DSL.f[Hole[Binding$T1]](hole[Binding$T1]) }) // case '{ DSL.f[$t]($x) } =>
17+
18+
x match {
19+
// case '{ DSL.f[$t]($x) } =>
20+
// case scala.runtime.quoted.Matcher.unapply[Tuple2[Type[tt /*type binding*/], Expr[tt]]](Tuple2(t, x)(/*implicits*/ '{ DSL.f[Hole[Nothing, Any]](hole[Any])] }, reflect) if consforms(x, t) =>
21+
case DSLf(t, x) =>
22+
implicit val tt = t
23+
'{ DSL.g[$t]($x) }
24+
25+
// case '{ DSL.f[$t]($x) } =>
26+
// case DSLg(t, x) =>
27+
// '{ DSL.f[$t]($x) }
28+
29+
case _ =>
30+
x
31+
}
32+
}
33+
34+
}
35+
36+
//
37+
// DSL in which the user write the code
38+
//
39+
40+
object DSL {
41+
def f[T](x: T): Unit = println("f: " + x.toString)
42+
def g[T](x: T): Unit = println("g: " + x.toString)
43+
}
44+
45+
//
46+
// Helper to abstract call to scala.runtime.quoted.Matcher.unapply and setup an object with the unapply
47+
//
48+
49+
class ExprMatch[Tup <: Tuple](pattern: Expr[_]) {
50+
def unapply(x: Expr[_])(implicit reflect: Reflection): Option[Tup] =
51+
scala.runtime.quoted.Matcher.unapply[Tup](x)(pattern, reflect)
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
import Macros._
3+
4+
5+
object Test {
6+
7+
def main(args: Array[String]): Unit = {
8+
swapFandG(DSL.f(5))
9+
// swapFandG(DSL.g("abc"))
10+
}
11+
12+
}

0 commit comments

Comments
 (0)