Skip to content

Commit

Permalink
Fix crash block capturing expressions (#551)
Browse files Browse the repository at this point in the history
  • Loading branch information
keynmol authored Aug 24, 2021
1 parent dd79fac commit ad331b2
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 49 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- uses: olafurpg/setup-scala@v12
with:
java-version: [email protected]
- run: sbt '++2.13.5 test'
- run: sbt '++2.13.6 test'
shell: bash
test:
name: ${{ matrix.command }} ${{ matrix.java }}
Expand Down
27 changes: 20 additions & 7 deletions mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,25 @@ class Instrumenter(
sb.println(s"""object ${gensym.fresh("compile")} {""")
sb.println(section.source.pos.text)
sb.println("\n}")
} else if (section.mod.isCrash) {
section.source.stats match {
case head :: _ =>
sb.println(s"$$doc.startStatement(${position(head.pos)});")

sb.append("$doc.crash(")
.append(position(head.pos))
.append(") {\n")

section.source.stats.foreach { stat =>
sb.append(stat.pos.text).append(";\n")
}
// closing the $doc.crash {... block
sb.append("\n}\n")

sb.println("\n$doc.endStatement();")

case Nil =>
}
} else {
section.source.stats.foreach { stat =>
sb.println(s"$$doc.startStatement(${position(stat.pos)});")
Expand All @@ -84,13 +103,7 @@ class Instrumenter(
sb.print(s"; $$doc.binder($name, ${position(pos)})")
}
private def printStatement(stat: Tree, m: Modifier, sb: PrintStream): Unit = {
if (m.isCrash) {
sb.append("$doc.crash(")
.append(position(stat.pos))
.append(") {\n")
.append(stat.pos.text)
.append("\n}")
} else {
if (!m.isCrash) {
val binders = stat match {
case Binders(names) =>
names.map(name => name -> name.pos)
Expand Down
21 changes: 16 additions & 5 deletions mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,21 @@ class Instrumenter(
sb.definition(s"""object ${gensym.fresh("compile")}""") {
_.println(section.source.pos.text)
}
} else if (section.mod.isCrash) {
section.source.stats match {
case head :: _ =>
sb.println(s"$$doc.startStatement(${position(head.pos)});")

sb.definition("$doc.crash(" ++ position(head.pos) ++ ")") { cb =>
section.source.stats.foreach { stat =>
cb.appendLines(stat.pos.text)
}
}

sb.println("\n$doc.endStatement();")

case Nil =>
}
} else {
section.source.stats.foreach { stat =>
sb.println(s"$$doc.startStatement(${position(stat.pos)});")
Expand All @@ -83,11 +98,7 @@ class Instrumenter(
sb.println(s"$$doc.binder($name, ${position(pos)});")
}
private def printStatement(stat: Tree, m: Modifier, sb: CodePrinter): Unit = {
if (m.isCrash) {
sb.definition("$doc.crash(" ++ position(stat.pos) ++ ")") {
_.appendLines(stat.pos.text)
}
} else {
if (!m.isCrash) {
val binders = stat match {
case Binders(names) =>
names.map(name => name -> name.pos)
Expand Down
65 changes: 33 additions & 32 deletions tests/unit/src/test/scala/tests/imports/DependencySuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tests.imports
import tests.markdown.BaseMarkdownSuite
import tests.markdown.Compat
import scala.util.Properties
import tests.BuildInfo

class DependencySuite extends BaseMarkdownSuite {
val userHome = System.getProperty("user.home")
Expand Down Expand Up @@ -82,39 +83,39 @@ class DependencySuite extends BaseMarkdownSuite {
|```
| """.stripMargin, {
if (Properties.isWin)
"""|error: dep-error.md:4:13: Error downloading org.scalameta:not-exists_2.13.5:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/not-exists_2.13.5/2.3.4/not-exists_2.13.5-2.3.4.pom
|import $dep.`org.scalameta:::not-exists:2.3.4`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:3:49: Error downloading org.scalameta:foobar:1.2.1
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/foobar/1.2.1/foobar-1.2.1.pom
|import $dep.`org.scalameta::mmunit:2.3.4`, $dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:3:13: Error downloading org.scalameta:mmunit_2.13:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/mmunit_2.13/2.3.4/mmunit_2.13-2.3.4.pom
|import $dep.`org.scalameta::mmunit:2.3.4`, $dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|""".stripMargin
s"""|error: dep-error.md:3:49: Error downloading org.scalameta:foobar:1.2.1
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/foobar/1.2.1/foobar-1.2.1.pom
|import $$dep.`org.scalameta::mmunit:2.3.4`, $$dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:4:13: Error downloading org.scalameta:not-exists_${BuildInfo.scalaVersion}:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/not-exists_${BuildInfo.scalaVersion}/2.3.4/not-exists_${BuildInfo.scalaVersion}-2.3.4.pom
|import $$dep.`org.scalameta:::not-exists:2.3.4`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:3:13: Error downloading org.scalameta:mmunit_2.13:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/mmunit_2.13/2.3.4/mmunit_2.13-2.3.4.pom
|import $$dep.`org.scalameta::mmunit:2.3.4`, $$dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|""".stripMargin
else
"""|error: dep-error.md:3:49: Error downloading org.scalameta:foobar:1.2.1
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/foobar/1.2.1/foobar-1.2.1.pom
|import $dep.`org.scalameta::mmunit:2.3.4`, $dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:4:13: Error downloading org.scalameta:not-exists_2.13.6:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/not-exists_2.13.6/2.3.4/not-exists_2.13.6-2.3.4.pom
|import $dep.`org.scalameta:::not-exists:2.3.4`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:3:13: Error downloading org.scalameta:mmunit_2.13:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/mmunit_2.13/2.3.4/mmunit_2.13-2.3.4.pom
|import $dep.`org.scalameta::mmunit:2.3.4`, $dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|""".stripMargin
s"""|error: dep-error.md:3:49: Error downloading org.scalameta:foobar:1.2.1
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/foobar/1.2.1/foobar-1.2.1.pom
|import $$dep.`org.scalameta::mmunit:2.3.4`, $$dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:4:13: Error downloading org.scalameta:not-exists_${BuildInfo.scalaVersion}:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/not-exists_${BuildInfo.scalaVersion}/2.3.4/not-exists_${BuildInfo.scalaVersion}-2.3.4.pom
|import $$dep.`org.scalameta:::not-exists:2.3.4`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|error: dep-error.md:3:13: Error downloading org.scalameta:mmunit_2.13:2.3.4
|<redacted user.home>
| not found: https://repo1.maven.org/maven2/org/scalameta/mmunit_2.13/2.3.4/mmunit_2.13-2.3.4.pom
|import $$dep.`org.scalameta::mmunit:2.3.4`, $$dep.`org.scalameta:foobar:1.2.1`
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|""".stripMargin
}
)

Expand Down
57 changes: 53 additions & 4 deletions tests/unit/src/test/scala/tests/markdown/CrashSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class CrashSuite extends BaseMarkdownSuite {
|???
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:288)
|// at repl.MdocSession$App$$anonfun$3.apply(basic.md:14)
|// at repl.MdocSession$App$$anonfun$3.apply(basic.md:14)
|// at repl.MdocSession$App$$anonfun$1.apply(basic.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(basic.md:8)
|```
""".stripMargin,
compat = Map(
Expand All @@ -25,8 +25,8 @@ class CrashSuite extends BaseMarkdownSuite {
|???
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:347)
|// at repl.MdocSession$App$$anonfun$3.apply(basic.md:14)
|// at repl.MdocSession$App$$anonfun$3.apply(basic.md:14)
|// at repl.MdocSession$App$$anonfun$1.apply(basic.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(basic.md:8)
|```
""".stripMargin,
Compat.Scala3 ->
Expand Down Expand Up @@ -236,4 +236,53 @@ class CrashSuite extends BaseMarkdownSuite {
)
)

check(
"multiple-statements",
"""
|```scala mdoc:crash
|class Cat { def func = ??? }
|(new Cat).func
|```
|""".stripMargin,
"""|```scala
|class Cat { def func = ??? }
|(new Cat).func
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
|// at repl.MdocSession$App$$anonfun$1$Cat$1.func(multiple-statements.md:9)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:8)
|```
""".stripMargin,
compat = Map(
Compat.Scala212 -> """|```scala
|class Cat { def func = ??? }
|(new Cat).func
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
|// at repl.MdocSession$App$$anonfun$1$Cat$1.func(multiple-statements.md:9)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:8)
|```""".stripMargin,
Compat.Scala211 -> """|```scala
|class Cat { def func = ??? }
|(new Cat).func
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:230)
|// at repl.MdocSession$App$$anonfun$1$Cat$1.func(multiple-statements.md:9)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:8)
|```""".stripMargin,
Compat.Scala3 -> """|```scala
|class Cat { def func = ??? }
|(new Cat).func
|// scala.NotImplementedError: an implementation is missing
|// at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
|// at repl.MdocSession$App$$anonfun$1$Cat$1.func(multiple-statements.md:9)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:10)
|// at repl.MdocSession$App$$anonfun$1.apply(multiple-statements.md:8)
|```""".stripMargin
)
)

}

0 comments on commit ad331b2

Please sign in to comment.