Skip to content

Commit 5718503

Browse files
committed
Fix format of ConstantExpr.show
1 parent 8b93074 commit 5718503

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
package dotty.tools.dotc.quoted
22

33
import dotty.tools.dotc.Driver
4-
import dotty.tools.dotc.core.Contexts.Context
4+
import dotty.tools.dotc.core.Contexts.{Context, FreshContext}
55
import dotty.tools.dotc.core.StdNames._
66
import dotty.tools.io.VirtualDirectory
7-
87
import dotty.tools.repl.AbstractFileClassLoader
98

109
import scala.quoted.Expr
11-
1210
import java.io.ByteArrayOutputStream
1311
import java.io.PrintStream
1412
import java.nio.charset.StandardCharsets

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package dotty.tools.dotc.quoted
22

3+
import dotty.tools.dotc.ast.Trees.Literal
4+
import dotty.tools.dotc.core.Constants.Constant
5+
import dotty.tools.dotc.printing.RefinedPrinter
6+
37
import scala.quoted.Expr
48
import scala.quoted.Liftable.ConstantExpr
59
import scala.runtime.quoted._
@@ -15,7 +19,11 @@ object Runners {
1519
}
1620

1721
def show(expr: Expr[T]): String = expr match {
18-
case expr: ConstantExpr[T] => expr.value.toString
22+
case expr: ConstantExpr[T] =>
23+
val ctx = new QuoteDriver().initCtx
24+
ctx.settings.color.update("never")(ctx)
25+
val printer = new RefinedPrinter(ctx)
26+
printer.toText(Literal(Constant(expr.value))).mkString(Int.MaxValue, false)
1927
case _ => new QuoteDriver().show(expr)
2028
}
2129
}

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ class CompilationTests extends ParallelTesting {
195195
implicit val testGroup: TestGroup = TestGroup("runAll")
196196
compileFilesInDir("../tests/run", defaultOptions) +
197197
compileFilesInDir("../tests/run-no-optimise", defaultOptions) +
198+
compileFile("../tests/run-special/quote-run-constants.scala", defaultRunWithCompilerOptions) +
198199
compileFile("../tests/run-special/quote-run.scala", defaultRunWithCompilerOptions) +
199200
compileFile("../tests/run-special/quote-run-2.scala", defaultRunWithCompilerOptions) +
200201
compileFile("../tests/run-special/quote-run-staged-interpreter.scala", defaultRunWithCompilerOptions)
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
true
2+
a
3+
4+
5+
"
6+
'
7+
\
8+
1
9+
2
10+
3
11+
4.0
12+
5.0
13+
xyz
14+
======
15+
true
16+
'a'
17+
'\n'
18+
'\"'
19+
'\''
20+
'\\'
21+
1
22+
2
23+
3L
24+
4.0
25+
5.0
26+
"xyz"
27+
"\n\\\"\'"
28+
"abc\n xyz"
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
import dotty.tools.dotc.quoted.Runners._
3+
4+
import scala.quoted._
5+
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
def run[T](expr: Expr[T]): Unit = println(expr.run)
9+
def show[T](expr: Expr[T]): Unit = println(expr.show)
10+
11+
run(true)
12+
run('a')
13+
run('\n')
14+
run('"')
15+
run('\'')
16+
run('\\')
17+
run(1)
18+
run(2)
19+
run(3L)
20+
run(4.0f)
21+
run(5.0d)
22+
run("xyz")
23+
24+
println("======")
25+
26+
show(true)
27+
show('a')
28+
show('\n')
29+
show('"')
30+
show('\'')
31+
show('\\')
32+
show(1)
33+
show(2)
34+
show(3L)
35+
show(4.0f)
36+
show(5.0d)
37+
show("xyz")
38+
show("\n\\\"'")
39+
show("""abc
40+
xyz""")
41+
}
42+
}

0 commit comments

Comments
 (0)