summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2014-01-28 11:29:28 +0300
committerEugene Burmako <xeno.by@gmail.com>2014-02-14 14:08:57 +0100
commit51b16e421ddd4e6c7e90ba945addf39ffcb4fa41 (patch)
treed002d3dccfe613f830862a259957154523a21488 /test
parentedadc01df2cbac2c8a00c2e0cc520713690418b9 (diff)
downloadscala-51b16e421ddd4e6c7e90ba945addf39ffcb4fa41.tar.gz
scala-51b16e421ddd4e6c7e90ba945addf39ffcb4fa41.tar.bz2
scala-51b16e421ddd4e6c7e90ba945addf39ffcb4fa41.zip
SI-8192 adds ClassSymbol.isPrimaryConstructor
Exposes a popular code pattern in macros as a dedicated reflection API. This simple commit, however, ended up being not so simple, as it often happens with our compiler. When writing a test for the new API, I realized that our (pre-existing) MethodSymbol.isPrimaryConstructor API returns nonsensical results for implementation artifacts (trait mixin ctors, module class ctors). What’s even more funny is that according to our reflection internals, even Java classes have primary constructors. Well, that’s not surprising, because `primaryConstructor` is just `decl(ctorName).alternatives.head`. Good thing that package classes don’t have constructors or that would elevate the situation to three fries short of a happy meal. At the moment, I’m too scared to fiddle with internal#Symbol.primaryConstructor, because that could easily break someone right before RC1, so I simply documented the findings in SI-8193 and postponed the actual work, except for one thing - isJavaDefined symbols no longer have primary constructors.
Diffstat (limited to 'test')
-rw-r--r--test/files/run/t6392b.check2
-rw-r--r--test/files/run/t7582-private-within.check2
-rw-r--r--test/files/run/t8192.check44
-rw-r--r--test/files/run/t8192/Macros_1.scala44
-rw-r--r--test/files/run/t8192/Test_2.scala39
5 files changed, 129 insertions, 2 deletions
diff --git a/test/files/run/t6392b.check b/test/files/run/t6392b.check
index c2cc103373..3f191c7960 100644
--- a/test/files/run/t6392b.check
+++ b/test/files/run/t6392b.check
@@ -1 +1 @@
-ModuleDef(Modifiers(), TermName("C")#MOD, Template(List(Select(Ident(scala#PK), TypeName("AnyRef")#TPE)), noSelfType, List(DefDef(Modifiers(), nme.CONSTRUCTOR#PCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(TypeName("C")), tpnme.EMPTY), nme.CONSTRUCTOR#PCTOR), List())), Literal(Constant(())))))))
+ModuleDef(Modifiers(), TermName("C")#MOD, Template(List(Select(Ident(scala#PK), TypeName("AnyRef")#TPE)), noSelfType, List(DefDef(Modifiers(), nme.CONSTRUCTOR#PCTOR, List(), List(List()), TypeTree(), Block(List(Apply(Select(Super(This(TypeName("C")), tpnme.EMPTY), nme.CONSTRUCTOR#CTOR), List())), Literal(Constant(())))))))
diff --git a/test/files/run/t7582-private-within.check b/test/files/run/t7582-private-within.check
index b2743ffa06..1b9a0910af 100644
--- a/test/files/run/t7582-private-within.check
+++ b/test/files/run/t7582-private-within.check
@@ -2,7 +2,7 @@ private[package pack] class JavaPackagePrivate
private[package pack] module JavaPackagePrivate
private[package pack] module class JavaPackagePrivate
private[package pack] field field
-private[package pack] primary constructor <init>
+private[package pack] constructor <init>
private[package pack] method meth
private[package pack] field staticField
private[package pack] method staticMeth
diff --git a/test/files/run/t8192.check b/test/files/run/t8192.check
new file mode 100644
index 0000000000..7195703e19
--- /dev/null
+++ b/test/files/run/t8192.check
@@ -0,0 +1,44 @@
+compile-time
+class File
+primary constructor: NoSymbol
+def <init>(x$1: String): java.io.File => false
+def <init>(x$1: String,x$2: String): java.io.File => false
+def <init>(x$1: java.io.File,x$2: String): java.io.File => false
+def <init>(x$1: java.net.URI): java.io.File => false
+package scala
+primary constructor: NoSymbol
+object List
+primary constructor: def <init>(): scala.collection.immutable.List.type => true
+def <init>(): scala.collection.immutable.List.type => true
+trait Product1
+primary constructor: def $init$(): Unit => true
+class UninitializedFieldError
+primary constructor: def <init>(msg: String): UninitializedFieldError => true
+def <init>(msg: String): UninitializedFieldError => true
+def <init>(obj: Any): UninitializedFieldError => false
+class C
+primary constructor: def <init>(x: Int): C => true
+def <init>(x: Int): C => true
+def <init>(x: String): C => false
+runtime
+class File
+primary constructor: NoSymbol
+def <init>(x$1: java.io.File,x$2: java.lang.String): java.io.File => false
+def <init>(x$1: java.lang.String): java.io.File => false
+def <init>(x$1: java.lang.String,x$2: java.lang.String): java.io.File => false
+def <init>(x$1: java.net.URI): java.io.File => false
+package scala
+primary constructor: NoSymbol
+object List
+primary constructor: def <init>(): scala.collection.immutable.List.type => true
+def <init>(): scala.collection.immutable.List.type => true
+trait Product1
+primary constructor: def $init$(): Unit => true
+class UninitializedFieldError
+primary constructor: def <init>(msg: String): UninitializedFieldError => true
+def <init>(msg: String): UninitializedFieldError => true
+def <init>(obj: Any): UninitializedFieldError => false
+class C
+primary constructor: def <init>(x: Int): C => true
+def <init>(x: Int): C => true
+def <init>(x: String): C => false
diff --git a/test/files/run/t8192/Macros_1.scala b/test/files/run/t8192/Macros_1.scala
new file mode 100644
index 0000000000..2089273d6d
--- /dev/null
+++ b/test/files/run/t8192/Macros_1.scala
@@ -0,0 +1,44 @@
+import scala.reflect.macros.whitebox._
+import scala.language.experimental.macros
+import java.io._
+
+object Macros {
+ def impl(c: Context) = {
+ var messages = List[String]()
+ def println(msg: String) = messages :+= msg
+
+ import c.universe._
+ def test(sym: ClassSymbol): Unit = {
+ def fullyInitializeSymbol(sym: Symbol): Unit = {
+ val internal = c.universe.asInstanceOf[scala.reflect.internal.SymbolTable]
+ internal.definitions.fullyInitializeSymbol(sym.asInstanceOf[internal.Symbol])
+ }
+ def defString(sym: Symbol): String = {
+ val internal = c.universe.asInstanceOf[scala.reflect.internal.SymbolTable]
+ sym.asInstanceOf[internal.Symbol].defString
+ }
+ def showCtor(sym: Symbol): String = {
+ fullyInitializeSymbol(sym)
+ if (sym == NoSymbol) "NoSymbol"
+ else s"${defString(sym)} => ${sym.asMethod.isPrimaryConstructor}"
+ }
+ sym.typeSignature
+ println(sym.toString)
+ println(s"primary constructor: ${showCtor(sym.primaryConstructor)}")
+ val ctors = sym.typeSignature.members.filter(_.name == nme.CONSTRUCTOR).map(sym => showCtor(sym))
+ ctors.toList.sorted.foreach(println)
+ }
+
+ println("compile-time")
+ test(typeOf[File].typeSymbol.asClass)
+ test(definitions.ScalaPackageClass)
+ test(definitions.ListModule.moduleClass.asClass)
+ test(typeOf[Product1[_]].typeSymbol.asClass)
+ test(typeOf[UninitializedFieldError].typeSymbol.asClass)
+ test(c.mirror.staticClass("C").asClass)
+
+ q"..${messages.map(msg => q"println($msg)")}"
+ }
+
+ def foo: Any = macro impl
+} \ No newline at end of file
diff --git a/test/files/run/t8192/Test_2.scala b/test/files/run/t8192/Test_2.scala
new file mode 100644
index 0000000000..15f684eb3f
--- /dev/null
+++ b/test/files/run/t8192/Test_2.scala
@@ -0,0 +1,39 @@
+import java.io._
+import scala.reflect.runtime.universe._
+import scala.reflect.runtime.{universe => ru}
+
+class C(x: Int) {
+ def this(x: String) = this(x.toInt)
+}
+
+object Test extends App {
+ def test(sym: ClassSymbol): Unit = {
+ def fullyInitializeSymbol(sym: Symbol): Unit = {
+ val internal = ru.asInstanceOf[scala.reflect.internal.SymbolTable]
+ internal.definitions.fullyInitializeSymbol(sym.asInstanceOf[internal.Symbol])
+ }
+ def defString(sym: Symbol): String = {
+ val internal = ru.asInstanceOf[scala.reflect.internal.SymbolTable]
+ sym.asInstanceOf[internal.Symbol].defString
+ }
+ def showCtor(sym: Symbol): String = {
+ fullyInitializeSymbol(sym)
+ if (sym == NoSymbol) "NoSymbol"
+ else s"${defString(sym)} => ${sym.asMethod.isPrimaryConstructor}"
+ }
+ sym.typeSignature
+ println(sym.toString)
+ println(s"primary constructor: ${showCtor(sym.primaryConstructor)}")
+ val ctors = sym.typeSignature.members.filter(_.name == nme.CONSTRUCTOR).map(sym => showCtor(sym))
+ ctors.toList.sorted.foreach(println)
+ }
+
+ Macros.foo
+ println("runtime")
+ test(typeOf[File].typeSymbol.asClass)
+ test(definitions.ScalaPackageClass)
+ test(definitions.ListModule.moduleClass.asClass)
+ test(typeOf[Product1[_]].typeSymbol.asClass)
+ test(typeOf[UninitializedFieldError].typeSymbol.asClass)
+ test(typeOf[C].typeSymbol.asClass)
+}