Skip to content

Commit 0463e18

Browse files
committed
Implement contants extractor in terms of tasty trees
1 parent 616084c commit 0463e18

File tree

11 files changed

+25
-17
lines changed

11 files changed

+25
-17
lines changed

compiler/src/dotty/tools/dotc/quoted/Toolbox.scala

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,6 @@ object Toolbox {
4848
def toTasty(expr: Expr[T]): Term =
4949
new QuoteDriver().withTree(expr, (tree, ctx) => Term(tree)(ctx), Settings.run())
5050

51-
def toConstantOpt(expr: Expr[T]): Option[T] = {
52-
def toConstantOpt(tree: Tree): Option[T] = tree match {
53-
case Literal(Constant(c)) => Some(c.asInstanceOf[T])
54-
case Block(Nil, e) => toConstantOpt(e)
55-
case Inlined(_, Nil, e) => toConstantOpt(e)
56-
case _ => None
57-
}
58-
expr match {
59-
case expr: LiftedExpr[T] => Some(expr.value)
60-
case _ => new QuoteDriver().withTree(expr, (tree, _) => toConstantOpt(tree), Settings.run())
61-
}
62-
}
63-
6451
}
6552

6653
class Settings[T] private (val outDir: Option[String], val rawTree: Boolean, val compilerArgs: List[String])

compiler/src/dotty/tools/dotc/tasty/internal/Constant.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ object Constant {
8787
}
8888

8989
private[tasty] case class Impl(const: Constants.Constant) extends constants.Constant {
90+
91+
def value: Any = const.value
92+
9093
override def toString: String = {
9194
import Toolbox.extractor
9295
this match {
Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
package scala.quoted
22

3-
import scala.runtime.quoted.Toolbox
3+
import scala.runtime.quoted
4+
import scala.runtime.tasty
5+
6+
import scala.tasty.trees._
47

58
object Constant {
6-
def unapply[T](expr: Expr[T])(implicit runner: Toolbox[T]): Option[T] = runner.toConstantOpt(expr)
9+
def unapply[T](expr: Expr[T])(implicit qtools: quoted.Toolbox[T], ttools: tasty.Toolbox): Option[T] = {
10+
def const(tree: Tree): Option[T] = tree match {
11+
case Literal(c) => Some(c.value.asInstanceOf[T])
12+
case Block(Nil, e) => const(e)
13+
case Inlined(_, Nil, e) => const(e)
14+
case _ => None
15+
}
16+
const(expr.toTasty)
17+
}
718
}

library/src/scala/runtime/quoted/Toolbox.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,4 @@ trait Toolbox[T] {
99
def run(expr: Expr[T]): T
1010
def show(expr: Expr[T]): String
1111
def toTasty(expr: Expr[T]): Term
12-
def toConstantOpt(expr: Expr[T]): Option[T] // TODO remove and implement with toTasty
1312
}
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
package scala.tasty.constants
22

3-
trait Constant
3+
trait Constant {
4+
def value: Any
5+
}

tests/run-with-compiler/quote-run-constants-extract-1.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

tests/run-with-compiler/quote-run-constants-extract-2.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

tests/run-with-compiler/quote-run-constants-extract-3.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

tests/run-with-compiler/quote-run-constants-extract-4.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

tests/run-with-compiler/quote-run-constants-extract-5.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

tests/run-with-compiler/quote-run-constants-extract-6.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
4+
import dotty.tools.dotc.tasty.Toolbox._
45

56
object Test {
67

0 commit comments

Comments
 (0)