Skip to content

Commit dc2dbe3

Browse files
committed
Fix #3946: Add Unit lifter in quoted.Liftable
1 parent 39980f3 commit dc2dbe3

File tree

5 files changed

+16
-2
lines changed

5 files changed

+16
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ package dotty.tools.dotc.quoted
33
import dotty.tools.dotc.ast.Trees._
44
import dotty.tools.dotc.ast.tpd
55
import dotty.tools.dotc.core.Constants._
6-
import dotty.tools.dotc.core.Contexts._
76
import dotty.tools.dotc.printing.RefinedPrinter
87

98
import scala.quoted.Expr
109
import scala.quoted.Liftable.ConstantExpr
10+
import scala.runtime.BoxedUnit
1111
import scala.runtime.quoted._
1212

1313
/** Default runners for quoted expressions */
@@ -23,7 +23,8 @@ object Runners {
2323
implicit val ctx = new QuoteDriver().initCtx
2424
ctx.settings.color.update("never")
2525
val printer = new RefinedPrinter(ctx)
26-
printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
26+
if (expr.value == BoxedUnit.UNIT) "()"
27+
else printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
2728
case _ => new QuoteDriver().show(expr)
2829
}
2930

library/src/scala/quoted/Liftable.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ object Liftable {
2222
override def toString: String = s"Expr($value)"
2323
}
2424

25+
implicit def UnitIsLiftable: Liftable[Unit] = (x: Unit) => new ConstantExpr(x)
2526
implicit def BooleanIsLiftable: Liftable[Boolean] = (x: Boolean) => new ConstantExpr(x)
2627
implicit def ByteLiftable: Liftable[Byte] = (x: Byte) => new ConstantExpr(x)
2728
implicit def CharIsLiftable: Liftable[Char] = (x: Char) => new ConstantExpr(x)

tests/pos/quote-liftable.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ object Test {
3333
(1.0f: Expr[Float])
3434
(1.0: Expr[Double])
3535
("abc": Expr[String])
36+
((): Expr[Unit])
3637

3738
val xs: Expr[List[Int]] = 1 :: 2 :: 3 :: Nil
3839
}

tests/run-with-compiler/i3946.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
()
2+
()

tests/run-with-compiler/i3946.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import dotty.tools.dotc.quoted.Runners._
2+
import scala.quoted._
3+
object Test {
4+
def main(args: Array[String]): Unit = {
5+
val u: Expr[Unit] = ()
6+
println(u.show)
7+
println(u.run)
8+
}
9+
}

0 commit comments

Comments
 (0)