Skip to content

Commit 8a9bc6f

Browse files
committed
nicer error messages for missing implicits in metadata
1 parent ed51041 commit 8a9bc6f

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

commons-macros/src/main/scala/com/avsystem/commons/macros/MacroCommons.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ trait MacroCommons { bundle =>
4747
final val NotInheritedFromSealedTypes = getType(tq"$CommonsPkg.annotation.NotInheritedFromSealedTypes")
4848
final val SeqCompanionSym = typeOf[scala.collection.Seq.type].termSymbol
4949
final val PositionedAT = getType(tq"$CommonsPkg.annotation.positioned")
50+
final val ImplicitNotFoundAT = getType(tq"$ScalaPkg.annotation.implicitNotFound")
5051

5152
final val NothingTpe: Type = typeOf[Nothing]
5253
final val StringPFTpe: Type = typeOf[PartialFunction[String, Any]]
@@ -369,6 +370,17 @@ trait MacroCommons { bundle =>
369370
def cachedImplicitDeclarations: List[Tree] =
370371
implicitsToDeclare.result()
371372

373+
def implicitNotFound(tpe: Type): String =
374+
tpe.typeSymbol.annotations.find(_.tree.tpe <:< ImplicitNotFoundAT)
375+
.map(_.tree.children.tail.head)
376+
.collect { case StringLiteral(error) =>
377+
val tpNames = tpe.typeSymbol.asType.typeParams.map(_.name.decodedName.toString)
378+
(tpNames zip tpe.typeArgs).foldLeft(error) {
379+
case (err, (tpName, tpArg)) => err.replaceAllLiterally(s"$${$tpName}", tpArg.toString)
380+
}
381+
}
382+
.getOrElse(s"no implicit value of type $tpe found")
383+
372384
implicit class treeOps[T <: Tree](t: T) {
373385
def debug: T = {
374386
c.echo(c.enclosingPosition, show(t))

commons-macros/src/main/scala/com/avsystem/commons/macros/meta/MacroMetadatas.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,7 @@ trait MacroMetadatas extends MacroSymbols {
151151
arity match {
152152
case ParamArity.Single(tpe) =>
153153
if (checked)
154-
tryInferCachedImplicit(tpe).map(n => Ok(q"$n"))
155-
.getOrElse(Fail(s"no implicit value $tpe for $description could be found"))
154+
tryInferCachedImplicit(tpe).map(n => Ok(q"$n")).getOrElse(Fail(implicitNotFound(tpe)))
156155
else
157156
Ok(q"${infer(tpe)}")
158157
case ParamArity.Optional(tpe) =>

0 commit comments

Comments
 (0)