-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5c06bd4
commit 4d1f51b
Showing
6 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
14
sbt-test/scala2-compat/i16630/scala2/Scala2MacrosCompat.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |