Skip to content

Commit 97b0af7

Browse files
Thiago PereiraThiago Pereira
Thiago Pereira
authored and
Thiago Pereira
committed
Add error message - Comments.scala:128
This commit adds the semantic object for the ```definition not found``` error. It is part of the (https://github.com/lampepfl/dotty/issues/1589)[https://github.com/lampepfl/dotty/issues/1589]
1 parent f1284b4 commit 97b0af7

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/dotty/tools/dotc/core/Comments.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import util.Positions._
99
import util.CommentParsing._
1010
import util.Property.Key
1111
import parsing.Parsers.Parser
12+
import reporting.diagnostic.messages.ProperDefinitionNotFound
1213

1314
object Comments {
1415
val ContextDoc = new Key[ContextDocstrings]
@@ -125,7 +126,7 @@ object Comments {
125126
val newName = (tree.name.show + "$" + codePos + "$doc").toTermName
126127
untpd.DefDef(newName, tree.tparams, tree.vparamss, tree.tpt, tree.rhs)
127128
case _ =>
128-
ctx.error("proper definition was not found in `@usecase`", codePos)
129+
ctx.error(ProperDefinitionNotFound(), codePos)
129130
tree
130131
}
131132
}

src/dotty/tools/dotc/reporting/diagnostic/messages.scala

+38-1
Original file line numberDiff line numberDiff line change
@@ -543,5 +543,42 @@ object messages {
543543
|
544544
|""".stripMargin
545545
}
546-
546+
547+
case class ProperDefinitionNotFound()(implicit ctx: Context) extends Message(20) {
548+
val kind = "Definition Not Found"
549+
val msg = hl"""|Proper definition was not found in ${"@usecase"}"""
550+
551+
val noUsecase = "def map[B, That](f: A => B)(implicit bf: CanBuildFrom[List[A], B, That]): That"
552+
553+
val usecase =
554+
"""|/** Map from List[A] => List[B]
555+
| *
556+
| * @usecase def map[B](f: A => B): List[B]
557+
| */
558+
|def map[B, That](f: A => B)(implicit bf: CanBuildFrom[List[A], B, That]): That""".stripMargin
559+
560+
val explanation = {
561+
hl"""|${"@usecase"} are only supported for ${"def"}s. They exist because with Scala's
562+
|advanced type-system, we sometimes end up with seemingly scary signatures.
563+
|
564+
|Let's see an example using the `map`function:
565+
|
566+
|${"List(1, 2, 3).map(2 * _) // res: List(2, 4, 6)"}
567+
|
568+
|It's very straight forward to understand and use, but has such a scary signature:
569+
|
570+
|$noUsecase
571+
|
572+
|In order to mitigate this and ease the usage of such functions we have the ${"@usecase"}
573+
|annotation for docstrings. Which can be used like this:
574+
|
575+
|$usecase
576+
|
577+
|Now when creating the docs, the method signature is substituted by the
578+
|${"@usecase"} with a reader-friendly version. The compiler makes sure that it is valid.
579+
|
580+
|Because of this, you must use ${"def"} when defining ${"@usecase"}.""".stripMargin
581+
}
582+
}
583+
547584
}

0 commit comments

Comments
 (0)