Skip to content

Commit

Permalink
add scripted test for macro mixing
Browse files Browse the repository at this point in the history
  • Loading branch information
johnduffell committed Oct 9, 2023
1 parent 5c06bd4 commit 4d1f51b
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sbt-test/scala2-compat/i16630/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
val scala3Version = sys.props("plugin.scalaVersion")
val scala2Version = sys.props("plugin.scala2Version")

lazy val scala2 = project.in(file("scala2"))
.settings(
scalaVersion := scala2Version,
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
)

lazy val scala3 = project.in(file("scala3"))
.settings(
scalaVersion := scala3Version
)
.dependsOn(scala2)

lazy val `scala2-test` = project.in(file("scala2-test"))
.settings(
scalaVersion := scala2Version,
scalacOptions ++= Seq(
"-Ytasty-reader",
// TODO add an option here to let scalac read experimental tasty files
),
)
.dependsOn(scala3)
9 changes: 9 additions & 0 deletions sbt-test/scala2-compat/i16630/scala2-test/TestScala2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object TestScala2 extends App {
private val line: Any = Macros.location.line
if (line != 2) {
println("test failed: " + line)
System.exit(1)
} else
println("test passed: " + line)
TestScala3
}
14 changes: 14 additions & 0 deletions sbt-test/scala2-compat/i16630/scala2/Scala2MacrosCompat.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import scala.reflect.macros.blackbox.Context
import scala.language.experimental.macros

case class Location(path: String, line: Int)

object Scala2MacrosCompat {
def locationImpl[T](c: Context): c.Tree = {
import c.universe._
val location = typeOf[Location]
val line = Literal(Constant(c.enclosingPosition.line))
val path = Literal(Constant(c.enclosingPosition.source.path))
q"new $location($path, $line)"
}
}
19 changes: 19 additions & 0 deletions sbt-test/scala2-compat/i16630/scala3/Macros.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import scala.quoted.{Quotes, Expr}
import scala.language.experimental.macros

case class Location[T](path: String, line: Int)

class Macros[T]:
def location: Location[T] = macro Scala2MacrosCompat.locationImpl
inline def location: Location[T] = ${Macros.locationImpl}

object Macros:

import scala.quoted.*
def locationImpl[T: Type](using quotes: Quotes): Expr[Location[T]] =
import quotes.reflect.Position
val file = Expr(Position.ofMacroExpansion.sourceFile.jpath.toString)
val line = Expr(Position.ofMacroExpansion.startLine + 1)
'{new Location[T]($file, $line)}

11 changes: 11 additions & 0 deletions sbt-test/scala2-compat/i16630/scala3/TestScala3.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
def TestScala3 = {
val line: Any = new Macros[Unit].location.line
if (line != 2) {
println("test failed: " + line)
System.exit(1)
} else
println("test passed: " + line)
}
object TestScala3App extends App {
TestScala3
}
6 changes: 6 additions & 0 deletions sbt-test/scala2-compat/i16630/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# This test is based loosely on https://docs.scala-lang.org/scala3/guides/migration/tutorial-macro-mixing.html
# Unfortunately Scala 2.13 refuses to read "experimental" tasty files, so the test is incomplete.
# However, the original issue was a failure to compile, so the full test is not essential.

# FIXME change the line below to scala2-test/run once we can make scala 2.13 read experimental scala 3 tasty files
> scala3/run

0 comments on commit 4d1f51b

Please sign in to comment.