Skip to content

Commit 4d1f51b

Browse files
committed
add scripted test for macro mixing
1 parent 5c06bd4 commit 4d1f51b

File tree

6 files changed

+83
-0
lines changed

6 files changed

+83
-0
lines changed

Diff for: sbt-test/scala2-compat/i16630/build.sbt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
val scala3Version = sys.props("plugin.scalaVersion")
2+
val scala2Version = sys.props("plugin.scala2Version")
3+
4+
lazy val scala2 = project.in(file("scala2"))
5+
.settings(
6+
scalaVersion := scala2Version,
7+
libraryDependencies += "org.scala-lang" % "scala-reflect" % scalaVersion.value
8+
)
9+
10+
lazy val scala3 = project.in(file("scala3"))
11+
.settings(
12+
scalaVersion := scala3Version
13+
)
14+
.dependsOn(scala2)
15+
16+
lazy val `scala2-test` = project.in(file("scala2-test"))
17+
.settings(
18+
scalaVersion := scala2Version,
19+
scalacOptions ++= Seq(
20+
"-Ytasty-reader",
21+
// TODO add an option here to let scalac read experimental tasty files
22+
),
23+
)
24+
.dependsOn(scala3)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
object TestScala2 extends App {
2+
private val line: Any = Macros.location.line
3+
if (line != 2) {
4+
println("test failed: " + line)
5+
System.exit(1)
6+
} else
7+
println("test passed: " + line)
8+
TestScala3
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.reflect.macros.blackbox.Context
2+
import scala.language.experimental.macros
3+
4+
case class Location(path: String, line: Int)
5+
6+
object Scala2MacrosCompat {
7+
def locationImpl[T](c: Context): c.Tree = {
8+
import c.universe._
9+
val location = typeOf[Location]
10+
val line = Literal(Constant(c.enclosingPosition.line))
11+
val path = Literal(Constant(c.enclosingPosition.source.path))
12+
q"new $location($path, $line)"
13+
}
14+
}

Diff for: sbt-test/scala2-compat/i16630/scala3/Macros.scala

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
import scala.quoted.{Quotes, Expr}
3+
import scala.language.experimental.macros
4+
5+
case class Location[T](path: String, line: Int)
6+
7+
class Macros[T]:
8+
def location: Location[T] = macro Scala2MacrosCompat.locationImpl
9+
inline def location: Location[T] = ${Macros.locationImpl}
10+
11+
object Macros:
12+
13+
import scala.quoted.*
14+
def locationImpl[T: Type](using quotes: Quotes): Expr[Location[T]] =
15+
import quotes.reflect.Position
16+
val file = Expr(Position.ofMacroExpansion.sourceFile.jpath.toString)
17+
val line = Expr(Position.ofMacroExpansion.startLine + 1)
18+
'{new Location[T]($file, $line)}
19+
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def TestScala3 = {
2+
val line: Any = new Macros[Unit].location.line
3+
if (line != 2) {
4+
println("test failed: " + line)
5+
System.exit(1)
6+
} else
7+
println("test passed: " + line)
8+
}
9+
object TestScala3App extends App {
10+
TestScala3
11+
}

Diff for: sbt-test/scala2-compat/i16630/test

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This test is based loosely on https://docs.scala-lang.org/scala3/guides/migration/tutorial-macro-mixing.html
2+
# Unfortunately Scala 2.13 refuses to read "experimental" tasty files, so the test is incomplete.
3+
# However, the original issue was a failure to compile, so the full test is not essential.
4+
5+
# FIXME change the line below to scala2-test/run once we can make scala 2.13 read experimental scala 3 tasty files
6+
> scala3/run

0 commit comments

Comments
 (0)