Skip to content

Commit a179086

Browse files
committed
Merge remote-tracking branch 'kepler/topic/reifytests'
2 parents da35106 + 7193d21 commit a179086

28 files changed

+352
-0
lines changed

test/files/run/reify_anonymous.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

test/files/run/reify_anonymous.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
println(new {def x = 2; def y = x * x}.y)
8+
};
9+
10+
val reporter = new ConsoleReporter(new Settings)
11+
val toolbox = new ToolBox(reporter)
12+
val ttree = toolbox.typeCheck(code.tree)
13+
toolbox.runExpr(ttree)
14+
}

test/files/run/reify_generic.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4

test/files/run/reify_generic.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
val product = List(1, 2, 3).head * List[Any](4, 2, 0).head.asInstanceOf[Int]
8+
println(product)
9+
};
10+
11+
val reporter = new ConsoleReporter(new Settings)
12+
val toolbox = new ToolBox(reporter)
13+
val ttree = toolbox.typeCheck(code.tree)
14+
toolbox.runExpr(ttree)
15+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
18
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
class C {
8+
def x = 2
9+
def y = x * x
10+
}
11+
12+
class D extends C {
13+
override def x = 3
14+
}
15+
16+
println(new D().y * new C().x)
17+
};
18+
19+
val reporter = new ConsoleReporter(new Settings)
20+
val toolbox = new ToolBox(reporter)
21+
val ttree = toolbox.typeCheck(code.tree)
22+
toolbox.runExpr(ttree)
23+
}

test/files/run/reify_printf.check

Whitespace-only changes.

test/files/run/reify_printf.scala

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import java.io.{ ByteArrayOutputStream, PrintStream }
2+
import scala.reflect.Code
3+
import scala.reflect.mirror._
4+
import scala.reflect.api._
5+
import scala.reflect.api.Trees
6+
import scala.reflect.internal.Types
7+
import reflect.runtime.Mirror.ToolBox
8+
import scala.tools.nsc.reporters._
9+
import scala.tools.nsc.Settings
10+
import scala.util.matching.Regex
11+
12+
object Test extends App {
13+
val tree = tree_printf(Code.lift("hello %s").tree, Code.lift("world").tree)
14+
15+
val reporter = new ConsoleReporter(new Settings)
16+
val toolbox = new ToolBox(reporter, args mkString " ")
17+
val ttree = toolbox.typeCheck(tree)
18+
19+
val output = new ByteArrayOutputStream()
20+
Console.setOut(new PrintStream(output))
21+
val evaluated = toolbox.runExpr(ttree)
22+
23+
assert(output.toString() == "hello world", output.toString() +" == hello world")
24+
25+
/*
26+
macro def printf(format: String, params: Any*) : String = tree_printf(format: Tree, (params: Seq[Tree]): _*)
27+
*/
28+
29+
var i = 0
30+
def gensym(name: String) = { i += 1; newTermName(name + i) }
31+
32+
def createTempValDef( value : Tree, tpe : Type ) : (Option[Tree],Tree) = {
33+
val local = gensym("temp")
34+
(
35+
Some(
36+
ValDef(
37+
Modifiers()
38+
, local
39+
, TypeTree().setType(tpe)
40+
, value
41+
)
42+
)
43+
, Ident(local)
44+
)
45+
}
46+
47+
def tree_printf(format: Tree, params: Tree*) = {
48+
val Literal(Constant(s_format: String)) = format
49+
val paramsStack = scala.collection.mutable.Stack(params: _*)
50+
val parsed = s_format.split("(?<=%[\\w%])|(?=%[\\w%])") map {
51+
case "%d" => createTempValDef( paramsStack.pop, classToType(classOf[Int]) )
52+
case "%s" => createTempValDef( paramsStack.pop, classToType(classOf[String]) )
53+
case "%%" => {
54+
(None:Option[Tree], Literal(Constant("%")))
55+
}
56+
case part => {
57+
(None:Option[Tree], Literal(Constant(part)))
58+
}
59+
}
60+
61+
val evals = for ((Some(eval), _) <- parsed if eval != None) yield (eval: Tree)
62+
val prints = for ((_, ref) <- parsed) yield
63+
Apply(
64+
Select(
65+
Select(
66+
Ident( newTermName("scala") )
67+
, newTermName("Predef")
68+
)
69+
, newTermName("print")
70+
)
71+
, List(ref)
72+
): Tree
73+
Block((evals ++ prints).toList, Literal(Constant(())))
74+
}
75+
}

test/pending/run/reify_csv.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
List(phase name, id, description)
2+
record(parser,1,parse source into ASTs, perform simple desugaring)
3+
record(namer,2,resolve names, attach symbols to named trees)
4+
record(packageobjects,3,load package objects)
5+
record(typer,4,the meat and potatoes: type the trees)
6+
record(superaccessors,5,add super accessors in traits and nested classes)
7+
record(pickler,6,serialize symbol tables)
8+
record(refchecks,7,reference/override checking, translate nested objects)
9+
record(selectiveanf,8,)
10+
record(liftcode,9,reify trees)

test/pending/run/reify_csv.scala

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val csv = """
7+
| phase name; id; description
8+
| parser; 1; parse source into ASTs, perform simple desugaring
9+
| namer; 2; resolve names, attach symbols to named trees
10+
|packageobjects; 3; load package objects
11+
| typer; 4; the meat and potatoes: type the trees
12+
|superaccessors; 5; add super accessors in traits and nested classes
13+
| pickler; 6; serialize symbol tables
14+
| refchecks; 7; reference/override checking, translate nested objects
15+
| selectiveanf; 8;
16+
| liftcode; 9; reify trees""".stripMargin.split("\n").map{_.trim()}.drop(1).toList
17+
18+
val fields = csv.head.split(";").map{_.trim()}.toList
19+
println(fields)
20+
21+
val code = scala.reflect.Code.lift({
22+
object Csv {
23+
case class record(`phase name`: String, id: String, description: String)
24+
25+
object record {
26+
def parse(lines: List[String]) = {
27+
lines drop(1) map { line => line.split(";", -1).toList match {
28+
case phase$whitespace$name :: id :: description :: _ => record(phase$whitespace$name.trim(), id.trim(), description.trim())
29+
case _ => throw new Exception("format error")
30+
}}
31+
}
32+
}
33+
}
34+
35+
Csv.record.parse(csv) foreach println
36+
})
37+
38+
val reporter = new ConsoleReporter(new Settings)
39+
val toolbox = new ToolBox(reporter)
40+
val ttree = toolbox.typeCheck(code.tree)
41+
toolbox.runExpr(ttree)
42+
}

test/pending/run/t5224.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
@serializable class C extends Object with ScalaObject {
3+
def <init>() = {
4+
super.<init>();
5+
()
6+
}
7+
};
8+
()
9+
}

test/pending/run/t5224.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.reflect._
2+
import scala.reflect.api._
3+
4+
object Test extends App {
5+
println(scala.reflect.Code.lift{
6+
@serializable class C
7+
}.tree.toString)
8+
}

test/pending/run/t5225_1.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
@transient @volatile var x: Int = 2;
3+
()
4+
}

test/pending/run/t5225_1.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.reflect._
2+
import scala.reflect.api._
3+
4+
object Test extends App {
5+
println(scala.reflect.Code.lift{
6+
@transient @volatile var x = 2
7+
}.tree.toString)
8+
}

test/pending/run/t5225_2.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
def foo(@cloneable x: Int): String = "";
3+
()
4+
}

test/pending/run/t5225_2.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.reflect._
2+
import scala.reflect.api._
3+
4+
object Test extends App {
5+
println(scala.reflect.Code.lift{
6+
def foo(@cloneable x: Int) = ""
7+
}.tree.toString)
8+
}

test/pending/run/t5229_1.check

Whitespace-only changes.

test/pending/run/t5229_1.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
object C
8+
};
9+
10+
val reporter = new ConsoleReporter(new Settings)
11+
val toolbox = new ToolBox(reporter)
12+
val ttree = toolbox.typeCheck(code.tree)
13+
toolbox.runExpr(ttree)
14+
}

test/pending/run/t5229_2.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
evaluated = null

test/pending/run/t5229_2.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
object C {
8+
val x = 2
9+
}
10+
11+
println(C.x)
12+
};
13+
14+
val reporter = new ConsoleReporter(new Settings)
15+
val toolbox = new ToolBox(reporter)
16+
val ttree = toolbox.typeCheck(code.tree)
17+
val evaluated = toolbox.runExpr(ttree)
18+
println("evaluated = " + evaluated)
19+
}

test/pending/run/t5266_1.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
evaluated = null

test/pending/run/t5266_1.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
def x = 2
8+
println(x)
9+
};
10+
11+
val settings = new Settings
12+
settings.debug.value = true
13+
settings.Xshowtrees.value = true
14+
settings.Xprint.value = List("typer")
15+
settings.printtypes.value = true
16+
settings.Ytyperdebug.value = true
17+
18+
val reporter = new ConsoleReporter(settings)
19+
val toolbox = new ToolBox(reporter)
20+
val ttree = toolbox.typeCheck(code.tree)
21+
val evaluated = toolbox.runExpr(ttree)
22+
println("evaluated = " + evaluated)
23+
}

test/pending/run/t5266_2.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
2
2+
evaluated = null

test/pending/run/t5266_2.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
def x = 2
8+
def y = x
9+
println(y)
10+
};
11+
12+
val reporter = new ConsoleReporter(settings)
13+
val toolbox = new ToolBox(reporter)
14+
val ttree = toolbox.typeCheck(code.tree)
15+
val evaluated = toolbox.runExpr(ttree)
16+
println("evaluated = " + evaluated)
17+
}

test/pending/run/t5269.check

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

test/pending/run/t5269.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
trait Z {
8+
val z = 2
9+
}
10+
11+
class X extends Z {
12+
def println() = Predef.println(z)
13+
}
14+
15+
new X().println()
16+
};
17+
18+
val reporter = new ConsoleReporter(new Settings)
19+
val toolbox = new ToolBox(reporter)
20+
val ttree = toolbox.typeCheck(code.tree)
21+
toolbox.runExpr(ttree)
22+
}

test/pending/run/t5270.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
200

test/pending/run/t5270.scala

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.tools.nsc.reporters._
2+
import scala.tools.nsc.Settings
3+
import reflect.runtime.Mirror.ToolBox
4+
5+
object Test extends App {
6+
val code = scala.reflect.Code.lift{
7+
class Y {
8+
def y = 100
9+
}
10+
11+
trait Z { this: Y =>
12+
val z = 2 * y
13+
}
14+
15+
class X extends Y with Z {
16+
def println() = Predef.println(z)
17+
}
18+
19+
new X().println()
20+
};
21+
22+
val reporter = new ConsoleReporter(new Settings)
23+
val toolbox = new ToolBox(reporter)
24+
val ttree = toolbox.typeCheck(code.tree)
25+
toolbox.runExpr(ttree)
26+
}

0 commit comments

Comments
 (0)