Skip to content

Commit fabd65d

Browse files
Merge pull request #4491 from dotty-staging/fix-quote-stuff
Fix impure by name macro parameters
2 parents f507958 + 127aa30 commit fabd65d

File tree

6 files changed

+45
-1
lines changed

6 files changed

+45
-1
lines changed

compiler/src/dotty/tools/dotc/transform/Splicer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ object Splicer {
4949
* - Other parameters are lifted to quoted.Types.TreeExpr (may reference a binding)
5050
*/
5151
private def getLiftedArgs(call: Tree, bindings: List[Tree])(implicit ctx: Context): List[Any] = {
52-
val bindMap = bindings.map {
52+
val bindMap = bindings.collect {
5353
case vdef: ValDef => (vdef.rhs, ref(vdef.symbol))
5454
}.toMap
5555
def allArgs(call: Tree, acc: List[List[Tree]]): List[List[Tree]] = call match {

tests/run/quote-impure-by-name.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1 + {Index.zero[String("bar"), scala.Tuple2[String("baz"), scala.Unit]]}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import scala.quoted._
2+
3+
import dotty.tools.dotc.quoted.Toolbox._
4+
5+
class Index[K, Keys](val index: String) extends AnyVal {
6+
override def toString: String = index
7+
}
8+
object Index {
9+
10+
implicit def zero[K, T]: Index[K, (K, T)] = new Index("0")
11+
12+
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl('(prev))('[K], '[H], '[T])
13+
14+
def succImpl[K, H, T](prev: Expr[Index[K, T]])(implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
15+
val value = s"1 + {${prev.show}}"
16+
'(new Index(~value.toExpr))
17+
}
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
println(Index.succ["bar", "foo", ("bar", ("baz", Unit))])
5+
}
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
3+
class Index[K, Keys](val index: Int) extends AnyVal
4+
object Index {
5+
6+
implicit def zero[K, T]: Index[K, (K, T)] = new Index(0)
7+
8+
implicit inline def succ[K, H, T](implicit prev: => Index[K, T]): Index[K, (H, T)] = ~succImpl('[K], '[H], '[T])
9+
10+
def succImpl[K, H, T](implicit k: Type[K], h: Type[H], t: Type[T]): Expr[Index[K, (H, T)]] = {
11+
'(new Index(0))
12+
}
13+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object Test {
2+
3+
def main(args: Array[String]): Unit = {
4+
Index.succ["bar", "foo", ("bar", ("baz", Unit))]
5+
}
6+
}

0 commit comments

Comments
 (0)