scala / scala3

The Scala 3 compiler, also known as Dotty.
https://dotty.epfl.ch
Apache License 2.0
5.89k stars 1.06k forks source link

Fix inductive-implicits benchmark #22007

Open mbovel opened 3 days ago

mbovel commented 3 days ago

The inductive-implicits benchmark fails on the benchmarks machine since April 2024:

image

This should have been caught by the tests at:

https://github.com/scala/scala3/blob/912b6f278b0fe5e7a25160612922cadf184acdbb/compiler/test/dotty/tools/dotc/BootstrappedOnlyCompilationTests.scala#L30

When I run scala3-bootstrapped / testOnly dotty.tools.dotc.BootstrappedOnlyCompilationTests -- *posMacros locally, the tests are indeed failing and the following error is printed:

64 |  val sel = Selector[L, Boolean]
   |                                ^
   |No given instance of type shapeless.Selector[Test.L, Boolean] was found for parameter selector of method apply in object Selector.
   |I found:
   |
   |    shapeless.Selector.inTail[Int,
   |      Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |         Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int
   |         :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |        Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int
   |        :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |        Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: ...Int :: ... :: ... :: ...,
   |    Boolean](
   |      shapeless.Selector.inTail[Int,
   |        Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int
   |          :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |           Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |          Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |          Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |          Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: ...Int :: ... :: ... :: ...,
   |      Boolean](
   |        shapeless.Selector.inTail[Int,
   |          Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |            Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |            Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |            Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |            Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |            Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: ...Int :: ... :: ... :: ...,
   |        Boolean](

...

   |            )
   |          )
   |        )
   |      )
   |    )
   |
   |But method inTail in object Selector does not match type shapeless.Selector[
   |  Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int :: Int ::
   |     Boolean :: shapeless.HNil,
   |Boolean].
   |
   |Note: method inTail in object Selector was not considered because it was not imported with `import given`.

It is not clear to me why the test does not fail on the CI. I checked that dotty.tools.dotc.BootstrappedOnlyCompilationTests.posMacros indeed runs there, and I confirm it is the case.

Anyway, the fix is simple, we just need to import the implicits explicitly.

This PR additionally removes the duplicate tests/pos-deep-subtype/inductive-implicits-bench.scala.

mbovel commented 18 hours ago

After having a deeper look with @hamzaremmal, we found that importing the given is actually not needed; the last line of the error message is incorrect. However, importing explicitly the given apparently diminishes the required stack size, which is why it made the test pass on my machine. This also explains why the test was not failing on the CI: the stack size was sufficient there.

I pushed a new fix which comments out some more lines of the summoned implicit argument type.

mbovel commented 17 hours ago

For the record, here is another error I got for the same benchmarks while working on another branch, with the import in the object:

object Test extends App {
  import Selector.given
  val sel = Selector[L, Boolean]
  ...
  Exception while compiling tests/bench/inductive-implicits.scala

  An unhandled exception was thrown in the compiler.
  Please file a crash report here:
  https://github.com/scala/scala3/issues/new/choose
  For non-enriched exceptions, compile with -Xno-enrich-error-messages.

     while compiling: <no file>pped / Test / testOnly 11s
        during phase: parser
                mode: Mode()
     library version: version 2.13.15
    compiler version: version 3.6.3-RC1-bin-SNAPSHOT-git-924ea08
            settings: -Xsemanticdb true -Xunchecked-java-output-version 9 -Xverify-signatures true -Ycheck List(all) -Yforce-sbt-phases true -Yno-double-bindings true -classpath /Users/mbovel/Library/Caches/Coursier/v1/https/repo1.maven.org/maven2/org/scala-lang/scala-library/2.13.15/scala-library-2.13.15.jar:/Users/mbovel/dotty/library/../out/bootstrap/scala3-library-bootstrapped/scala-3.6.3-RC1-bin-SNAPSHOT-nonbootstrapped/scala3-library_3-3.6.3-RC1-bin-SNAPSHOT.jar:out/compilePosMacros/bench/inductive-implicits -color never -d out/compilePosMacros/bench/inductive-implicits -indent true -pagewidth 120

java.lang.StackOverflowError
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        at dotty.tools.dotc.core.Scopes$MutableScope.enterAllInHash(Scopes.scala:310)
        ...

Here the stack overflow error is not caught and crashes the compiler.