diff options
-rw-r--r-- | build.sbt | 2 | ||||
-rw-r--r-- | core/src/main/scala/magnolia.scala | 27 | ||||
-rw-r--r-- | examples/src/main/scala/example.scala | 4 | ||||
-rw-r--r-- | tests/shared/src/main/scala/magnolia/main.scala | 2 |
4 files changed, 20 insertions, 15 deletions
@@ -48,7 +48,7 @@ lazy val buildSettings = Seq( organization := "com.propensive", scalaVersion := "2.12.2", name := "magnolia", - version := "0.1.0", + version := "0.2.0", scalacOptions ++= Seq("-deprecation", "-feature", "-Ywarn-value-discard", "-Ywarn-dead-code", "-Ywarn-nullary-unit", "-Ywarn-numeric-widen", "-Ywarn-inaccessible", "-Ywarn-adapted-args"), crossScalaVersions := Seq("2.10.6", "2.11.11", "2.12.2"), scmInfo := Some(ScmInfo(url("https://github.com/propensive/magnolia"), diff --git a/core/src/main/scala/magnolia.scala b/core/src/main/scala/magnolia.scala index 22beec4..d2978b5 100644 --- a/core/src/main/scala/magnolia.scala +++ b/core/src/main/scala/magnolia.scala @@ -134,6 +134,11 @@ class Macros(val c: whitebox.Context) { } else if(isSealedTrait) { val subtypes = classType.get.knownDirectSubclasses.to[List] + + if(subtypes.isEmpty) { + c.info(c.enclosingPosition, s"could not find any direct subtypes of $typeSymbol", true) + c.abort(c.enclosingPosition, "") + } Some { val components = subtypes.map(_.asType.toType).map { searchType => @@ -167,20 +172,14 @@ class Macros(val c: whitebox.Context) { construct.map { const => val impl = derivationImplicit.merge - val res = q"""{ + q"""{ def $assignedName: $resultType = $impl.construct { sourceParameter => $const } $assignedName }""" - - try c.typecheck(res) catch { - case e: Exception => - e.printStackTrace() - } - res } } - def magnolia[T: WeakTypeTag, Typeclass: WeakTypeTag]: Tree = try { + def magnolia[T: WeakTypeTag, Typeclass: WeakTypeTag]: Tree = { val genericType: Type = weakTypeOf[T] val currentStack: Stack = recursionStack.get(c.enclosingPosition).getOrElse(Stack(List(), List())) @@ -196,7 +195,14 @@ class Macros(val c: whitebox.Context) { Left(c.untypecheck(c.inferImplicitValue(coDerivationType, false, false))) } catch { case e: Exception => - Right(c.untypecheck(c.inferImplicitValue(contraDerivationType))) + try Right(c.untypecheck(c.inferImplicitValue(contraDerivationType, false, false))) catch { + case e: Exception => + c.info(c.enclosingPosition, s"could not find an implicit instance of "+ + s"CovariantDerivation[$typeConstructor] or "+ + s"ContravariantDerivation[$typeConstructor]", true) + + throw e + } } if(directlyReentrant) throw DirectlyReentrantException() @@ -235,9 +241,6 @@ class Macros(val c: whitebox.Context) { c.abort(c.enclosingPosition, "could not infer typeclass for type $genericType") } - } catch { - case DirectlyReentrantException() => ??? - case e: Exception => e.printStackTrace(); ??? } } diff --git a/examples/src/main/scala/example.scala b/examples/src/main/scala/example.scala index 7199b13..e649b88 100644 --- a/examples/src/main/scala/example.scala +++ b/examples/src/main/scala/example.scala @@ -16,9 +16,11 @@ object `package` { implicit def showSet[T: Show]: Show[Set[T]] = s => "set" } +sealed trait EmptyType + sealed trait Tree case class Branch(left: Tree, right: Tree) extends Tree -case class Leaf(value: Int) extends Tree +case class Leaf(value: Int, no: EmptyType) extends Tree sealed trait Entity case class Person(name: String, address: Address) extends Entity diff --git a/tests/shared/src/main/scala/magnolia/main.scala b/tests/shared/src/main/scala/magnolia/main.scala index 727eb1f..014ec33 100644 --- a/tests/shared/src/main/scala/magnolia/main.scala +++ b/tests/shared/src/main/scala/magnolia/main.scala @@ -4,7 +4,7 @@ import examples._ object Main { def main(args: Array[String]): Unit = { - println(Branch(Branch(Leaf(1), Leaf(2)), Leaf(3)).show) + println(Branch(Branch(Leaf(1, null), Leaf(2, null)), Leaf(3, null)).show) println(List[Entity](Person("John Smith", Address(List("1 High Street", "London", "SW1A 1AA"), Country("UK", "GBR", false)))).show) |