Skip to content

Incorrect line positions in macro expansion #5629

Closed
@liufengyun

Description

@liufengyun

This is an error encountered when working with ScalaTest (#5491 ). Given the macro definition below:

import scala.quoted._
import scala.tasty._

object scalatest {
  inline def assert(condition: => Boolean): Unit = ~assertImpl('(condition), '(""))

  def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = {
    import refl._
    val b = cond.unseal.underlyingArgument.seal[Boolean]
    '{ scala.Predef.assert(~b) }
  }

  inline def thisLineNumber = ~thisLineNumberImpl

  def thisLineNumberImpl(implicit refl: Reflection): Expr[Int] = {
    import refl._
    refl.rootPosition.startLine.toExpr
  }
}

The following code fails at runtime:

object Test {
  import scalatest._

  def main(args: Array[String]): Unit = {
    val startLine = thisLineNumber
    assert(thisLineNumber == startLine + 1)
    // scala.Predef.assert(thisLineNumber == startLine + 1)  // OK
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions