Skip to content

Commit

Permalink
fix PPrint derivation fails with nested class definitions #47
Browse files Browse the repository at this point in the history
  • Loading branch information
Li Haoyi committed Apr 18, 2015
1 parent 357d08a commit c57eedd
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 12 deletions.
6 changes: 2 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import scalatex.ScalatexReadme

scalaVersion := "2.11.5"
scalaVersion := "2.11.6"

crossScalaVersions := Seq("2.11.5", "2.10.5")

Expand Down Expand Up @@ -113,9 +113,7 @@ lazy val repl = project
"com.lihaoyi" %% "scala-parser" % "0.1.3"
),
javaOptions += "-Xmx2G",
fork in (Test, testOnly) := true,
// Will not be necessary with sbt 0.13.8
unmanagedSourceDirectories in Compile += (sourceDirectory in Compile).value / s"scala-${scalaBinaryVersion.value}"
fork in (Test, testOnly) := true
)

lazy val readme = ScalatexReadme(
Expand Down
6 changes: 6 additions & 0 deletions pprint/src/main/scala-2.10/ammonite/pprint/MacroContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ammonite.pprint


object MacroContext {
type Context = scala.reflect.macros.Context
}
6 changes: 6 additions & 0 deletions pprint/src/main/scala-2.11/ammonite/pprint/MacroContext.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package ammonite.pprint


object MacroContext {
type Context = scala.reflect.macros.blackbox.Context
}
30 changes: 25 additions & 5 deletions pprint/src/main/scala/ammonite/pprint/PPrint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -233,18 +233,37 @@ object Internals {
}

object LowerPriPPrint {
def companionTree(c: MacroContext.Context)(tpe: c.Type) = {
import c.universe._
val companionSymbol = tpe.typeSymbol.companionSymbol

if (companionSymbol == NoSymbol) {
val clsSymbol = tpe.typeSymbol.asClass
val msg = "[error] The companion symbol could not be determined for " +
s"[[${clsSymbol.name}]]. This may be due to a bug in scalac (SI-7567) " +
"that arises when a case class within a function is pickled. As a " +
"workaround, move the declaration to the module-level."
Console.err.println(msg)
c.abort(c.enclosingPosition, msg) /* TODO Does not show message. */
}

val symTab = c.universe.asInstanceOf[reflect.internal.SymbolTable]
val pre = tpe.asInstanceOf[symTab.Type].prefix.asInstanceOf[Type]
c.universe.treeBuild.mkAttributedRef(pre, companionSymbol)
}
// Should use blackbox.Context in 2.11, doing this for 2.10 compatibility
def FinalRepr[T: c.WeakTypeTag](c: scala.reflect.macros.Context) = c.Expr[PPrint[T]] {
def FinalRepr[T: c.WeakTypeTag](c: MacroContext.Context) = c.Expr[PPrint[T]] {
import c.universe._

val tpe = c.weakTypeOf[T]
util.Try(c.weakTypeOf[T].typeSymbol.asClass) match {

util.Try(tpe.typeSymbol.asClass) match {

case util.Success(f) if f.isCaseClass && !f.isModuleClass =>

val constructor = tpe.member(newTermName("<init>"))

val companion = tpe.typeSymbol.companionSymbol
val companion = companionTree(c)(tpe)

val paramTypes =
constructor
Expand Down Expand Up @@ -278,7 +297,7 @@ object Internals {
// We're fleshing this out a lot more than necessary to help
// scalac along with its implicit search, otherwise it gets
// confused and explodes
q"""
val res = q"""
new ammonite.pprint.PPrint[$tpe](
ammonite.pprint.Internals.fromUnpacker[$tpe](_.productPrefix){
(t: $tpe, cfg: ammonite.pprint.Config) =>
Expand All @@ -290,7 +309,8 @@ object Internals {
implicitly[ammonite.pprint.Config]
)
"""

// println(res)
res
case _ =>
q"""new ammonite.pprint.PPrint[$tpe](
ammonite.pprint.PPrinter.Literal,
Expand Down
14 changes: 14 additions & 0 deletions pprint/src/test/scala/ammonite/pprint/PPrintTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ import scala.annotation.tailrec
import scala.collection.{immutable => imm}
import scala.util.matching.Regex
import ammonite.pprint._

object Nested{
object ODef { case class Foo(i: Int, s: String) }

class CDef { case class Foo(i: Int, s: String) }
object CDef extends CDef

}
case class Foo(integer: Int, sequence: Seq[String])
case class FooG[T](t: T, sequence: Seq[String])
case class FooNoArgs()
Expand All @@ -18,6 +26,7 @@ object PPrintTests extends TestSuite{
val tests = TestSuite{
'Horizontal {
import ammonite.pprint.Config.Defaults._

'primitives {
'Unit {
* - check((), "()")
Expand Down Expand Up @@ -337,6 +346,11 @@ object PPrintTests extends TestSuite{
)
}
}
'traited {
import ammonite.pprint.Config.Defaults._
check(Nested.ODef.Foo(2, "ba"), "Foo(2, \"ba\")")
check(Nested.CDef.Foo(2, "ba"), "Foo(2, \"ba\")")
}
'Color{
import ammonite.pprint.Config.Colors._
def count(haystack: Iterator[String], needles: (String, Int)*) = {
Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=0.13.7
sbt.version=0.13.8
2 changes: 1 addition & 1 deletion repl/src/main/scala/ammonite/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ object Repl{
},
saveHistory = { s =>
val fw = new FileWriter(saveFile, true)
try fw.write(s)
try fw.write(delimiter + s)
finally fw.close()
}
)
Expand Down
1 change: 0 additions & 1 deletion repl/src/test/scala/ammonite/repl/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ object Main {
def main(args: Array[String]): Unit = {
println("""\\s""")
println(s"""\\s""")

// val mirror = scala.reflect.runtime.currentMirror
// val u = mirror.universe
// val rm = mirror.reflectModule(mirror.RootPackage)
Expand Down

0 comments on commit c57eedd

Please sign in to comment.