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

Scaladoc fails to run on code using the new MacroAnnotation #16875

Open alexarchambault opened 1 year ago

alexarchambault commented 1 year ago

Compiler version

3.3.0-RC2

Minimized code

See this repository, that basically re-uses the test case from here, and runs scaladoc on it.

Output

-- Error: memoize.scala:26:10 --------------------------------------------------
26 |            else
   |          ^
   |          an identifier expected, but $XMLSTART$< found
-- Error: memoize.scala:32:26 --------------------------------------------------
32 |        List(cacheVal, newFib)
   |                          ^
   |                          Not found: type MacroAnnotation
2 errors found
Following generated file paths might not be compatible with Jekyll:
_empty_/Test
_empty_/Bar
_empty_/memoize
If using GitHub Pages consider adding a ".nojekyll" file.

(see README of the reproduction repository above for the full command running scaladoc here)

Expectation

Scaladoc generation succeeds.

jchyb commented 1 year ago

Minimized (with scala-cli):

memoize.scala:

import scala.annotation.{experimental, MacroAnnotation}
import scala.quoted._

@experimental
class memoize extends MacroAnnotation {
  def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =

    ???
}

Running scala-cli doc memoize.scala -S 3.nightly outputs:

-- Error: memoize.scala:29:7 ---------------------------------------------------
29 |    ???
   |       ^
   |       an identifier expected, but $XMLSTART$< found
-- Error: memoize.scala:31:26 --------------------------------------------------
31 |
   |                          ^
   |                          Not found: type MacroAnnotation

Fun part is, those 22 empty lines are there for a reason. Removing just one of them leaves us with just a single warning instead of errors:

-- Warning: memoize.scala:6:6 --------------------------------------------------
 6 |  def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
   |  ^
   |java.lang.ArrayIndexOutOfBoundsException: Index 31 out of bounds for length 31
   |    at dotty.tools.dotc.util.SourceFile.lineToOffset(SourceFile.scala:159)
   |    at dotty.tools.scaladoc.snippets.SnippetCompiler.$anonfun$3(SnippetCompiler.scala:66)
   |    at scala.collection.immutable.List.map(List.scala:250)
   |    at scala.collection.immutable.List.map(List.scala:79)
 7 |    ???
1 warning found

Removing more lines will change the index number in the warning message.

In the errors, both reported pointed positions appear to be moved around depending on the number of characters in the "empty" space, which is why the errors in this minimization are not exactly the same as in the original output.