com-lihaoyi / utest

A simple testing framework for Scala

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

exception during macro expansion: java.lang.ArrayIndexOutOfBoundsException:

tribbloid opened this issue · comments

Simple demonstration:

import org.scalatest.funspec.AnyFunSpec

import scala.language.experimental.macros

class ErrorCase extends AnyFunSpec {

  final val code = """
object FoundReq {
  class L
  type R
  def f(r: R): Int = ???
  f(new L)
}"""

  val aa = utest.compileError(
    code
  )
}

This generates the error:

[Error] /home/..../ErrorCase.scala:19:30: exception during macro expansion: 
java.lang.ArrayIndexOutOfBoundsException: 26
	at scala.reflect.internal.util.BatchSourceFile.findLine$1(SourceFile.scala:218)
	at scala.reflect.internal.util.BatchSourceFile.offsetToLine(SourceFile.scala:221)
	at scala.reflect.internal.util.InternalPositionImpl.line0(Position.scala:177)
	at scala.reflect.internal.util.InternalPositionImpl.lineContent(Position.scala:189)
	at scala.reflect.internal.util.InternalPositionImpl.lineContent$(Position.scala:189)
	at scala.reflect.internal.util.Position.lineContent(Position.scala:19)
	at utest.asserts.AssertsCompanionVersionSpecific.calcPosMsg$1(Asserts.scala:27)
	at utest.asserts.AssertsCompanionVersionSpecific.compileError(Asserts.scala:82)
	at utest.asserts.AssertsCompanionVersionSpecific.compileError$(Asserts.scala:21)
	at utest.asserts.Asserts$.compileError(Asserts.scala:15)

it is observed on Scala 2.13.7 on OpenJDK 8u311

I believe the error is triggered by the following code:

      def prefix = if (funcs.length == 1) "" else s"#${i+1}: "
      res match{
        case Success(value) =>
          if (!value) throw Util.makeAssertError(prefix + src, logged, null)
        case Failure(e) => throw Util.makeAssertError(prefix + src, logged, e)
      }

The solution should be simple: just add a fallback in case makeAssertError throws an exception, in which the prefix is reverted to ""