From ad331b2109c5060b902169bfcfa1ca2d379266e6 Mon Sep 17 00:00:00 2001 From: Anton Sviridov Date: Tue, 24 Aug 2021 13:28:40 +0100 Subject: [PATCH] Fix crash block capturing expressions (#551) --- .github/workflows/ci.yml | 2 +- .../mdoc/internal/markdown/Instrumenter.scala | 27 ++++++-- .../mdoc/internal/markdown/Instrumenter.scala | 21 ++++-- .../scala/tests/imports/DependencySuite.scala | 65 ++++++++++--------- .../scala/tests/markdown/CrashSuite.scala | 57 ++++++++++++++-- 5 files changed, 123 insertions(+), 49 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 349f12033..d1f6887fc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: - uses: olafurpg/setup-scala@v12 with: java-version: adopt@1.11 - - run: sbt '++2.13.5 test' + - run: sbt '++2.13.6 test' shell: bash test: name: ${{ matrix.command }} ${{ matrix.java }} diff --git a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala index 6725cb679..d36217b55 100644 --- a/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala +++ b/mdoc/src/main/scala-2/mdoc/internal/markdown/Instrumenter.scala @@ -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)});") @@ -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) diff --git a/mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala b/mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala index 3f0bd43bc..4ba1c5c8f 100644 --- a/mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala +++ b/mdoc/src/main/scala-3/mdoc/internal/markdown/Instrumenter.scala @@ -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)});") @@ -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) diff --git a/tests/unit/src/test/scala/tests/imports/DependencySuite.scala b/tests/unit/src/test/scala/tests/imports/DependencySuite.scala index 49a8525b9..8aba6e616 100644 --- a/tests/unit/src/test/scala/tests/imports/DependencySuite.scala +++ b/tests/unit/src/test/scala/tests/imports/DependencySuite.scala @@ -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") @@ -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 - | - | 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 - | - | 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 - | - | 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 + | + | 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 + | + | 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 + | + | 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 - | - | 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 - | - | 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 - | - | 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 + | + | 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 + | + | 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 + | + | 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 } ) diff --git a/tests/unit/src/test/scala/tests/markdown/CrashSuite.scala b/tests/unit/src/test/scala/tests/markdown/CrashSuite.scala index 9d816e306..1078622da 100644 --- a/tests/unit/src/test/scala/tests/markdown/CrashSuite.scala +++ b/tests/unit/src/test/scala/tests/markdown/CrashSuite.scala @@ -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( @@ -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 -> @@ -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 + ) + ) + }