Skip to content

Commit 5a042ee

Browse files
committed
Fix macro migration build
1 parent 744d95b commit 5a042ee

File tree

6 files changed

+33
-20
lines changed

6 files changed

+33
-20
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Test Cross Compilation
2929
run: sbt '++3.0.0-M3; macro-cross-app / run; ++2.13.4; macro-cross-app / run'
3030
- name: Test Mixing Definitions
31-
run: sbt '++3.0.0-M3; macro-mix-app / run; ++2.13.4; macro-mix-app / run'
31+
run: sbt '++3.0.0-M1; macro-mix-app / run; ++2.13.4; macro-mix-app / run'
3232
sbt-migration:
3333
name: Test sbt Migration
3434
runs-on: ubuntu-latest

build.sbt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ lazy val website = project
88
mdocVariables := Map(
99
"scala30" -> scala30,
1010
"scala30Binary" -> scala30,
11+
"scala3M1" -> scala3M1,
12+
"scala3M1Binary" -> scala3M1,
1113
"scala213" -> scala213,
1214
"sbtDotty" -> sbtDotty,
1315
"catsCore" -> "2.3.0-M2",

docs/macros/migration-tutorial.md

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: Porting a Macro Library
66
In this tutorial we will learn two different approaches to migrate a macro library to Scala 3.0:
77
- [Cross-Building](#cross-building)
88
- [Mixing Macro Definitions in Scala 3.0](#mixing-macro-definitions)
9-
-
9+
1010
Each approach makes the library available in Scala 3.0 while maintaining Scala 2.13 compatibility.
1111

1212
## A Scala 2 Macro Definition
@@ -249,15 +249,16 @@ We eventually come up with this implementation:
249249

250250
package location
251251

252-
import scala.quoted.{QuoteContext, Expr}
252+
import scala.quoted.{Quotes, Expr}
253253

254254
object Macros:
255255
inline def location: Location = ${locationImpl}
256256

257-
def locationImpl(using ctx: QuoteContext): Expr[Location] =
258-
import ctx.reflect.rootPosition
259-
val file = Expr(rootPosition.sourceFile.jpath.toString)
260-
val line = Expr(rootPosition.startLine + 1)
257+
private def locationImpl(using ctx: Quotes): Expr[Location] =
258+
import ctx.reflect.Position
259+
val pos = Position.ofMacroExpansion
260+
val file = Expr(pos.sourceFile.jpath.toString)
261+
val line = Expr(pos.startLine + 1)
261262
'{new Location($file, $line)}
262263
```
263264

@@ -288,6 +289,8 @@ Then the Scala 2.13 compiler would be able to find that definition in the Scala
288289
This idea sets the ground to the mixing macros technique that we detail further below using the `location` example.
289290
It is compatible with any build tool that can mix Scala 2.13 and Scala 3.0 modules.
290291

292+
> This part of the tutorial is written for @scala3M1@ because it is the only Tasty Reader compatible version at the time of writing.
293+
291294
### 1 - Creating the Scala 3 module
292295

293296
Going back to the initial state of our `location` library we have:
@@ -319,7 +322,7 @@ We call it `macroLib`.
319322
lazy val macroLib = project
320323
.in(file("macro-lib"))
321324
.settings(
322-
scalaVersion := "@scala30@"
325+
scalaVersion := "@scala3M1@"
323326
)
324327
.dependsOn(lib)
325328
```
@@ -331,7 +334,11 @@ lazy val app = project
331334
.in(file("app"))
332335
.settings(
333336
scalaVersion := "@scala213@",
334-
crossScalaVersion := Seq("@scala213@", "@scala30@")
337+
crossScalaVersion := Seq("@scala213@", "@scala3M1@") ,
338+
scalacOptions ++= {
339+
if (isDotty.value) Seq()
340+
else Seq("-Ytasty-reader")
341+
}
335342
)
336343
.dependsOn(macroLib)
337344
```
@@ -353,6 +360,7 @@ Note that we moved it to a `Scala2Macros` object because we cannot have two `Mac
353360

354361
```scala
355362
// lib/src/main/scala/location/Macros.scala
363+
356364
package location
357365

358366
import scala.reflect.macros.blackbox.Context
@@ -418,8 +426,8 @@ Here the Scala 3.0 implementation throws a `NotImplementedError`, hence the answ
418426
We can try and see the exception being thrown at compile-time.
419427

420428
```shell
421-
sbt: location> ++@scala30@; app / run
422-
[info] compiling 1 Scala source to /loaction/app/target/scala-@scala30Binary@/classes ...
429+
sbt: location> ++@scala3M1@; app / run
430+
[info] compiling 1 Scala source to /loaction/app/target/scala-@scala3M1Binary@/classes ...
423431
[error] -- Error: /location/app/src/main/scala/app/Main.scala:6:10
424432
[error] 6 | println(location)
425433
[error] | ^^^^^^^^
@@ -460,7 +468,7 @@ object Macros:
460468
The `app` module can now be compiled and executed in Scala 3:
461469
462470
```
463-
sbt: location> ++@scala30@; app / run
471+
sbt: location> ++@scala3M1@; app / run
464472
[info] running app.Main
465473
Line 6 in /location/app/src/main/scala/app/Main.scala
466474
[info] app / run completed

macro-migration-tutorial.sbt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ lazy val `macro-mix-lib` = project
3636
lazy val `macro-mix-macro-lib` = project
3737
.in(file("macro-migration-tutorial/mix/macro-lib"))
3838
.settings(
39-
scalaVersion := scala30
39+
scalaVersion := scala3M1
4040
)
4141
.dependsOn(`macro-mix-lib`)
4242

4343
lazy val `macro-mix-app` = project
4444
.in(file("macro-migration-tutorial/mix/app"))
4545
.settings(
46-
scalaVersion := scala30,
47-
crossScalaVersions := Seq(scala213, scala30),
46+
scalaVersion := scala3M1,
47+
crossScalaVersions := Seq(scala213, scala3M1),
4848
scalacOptions ++= {
4949
if (isDotty.value) Seq()
5050
else Seq("-Ytasty-reader")
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package location
22

3-
import scala.quoted.{QuoteContext, Expr}
3+
import scala.quoted.{Quotes, Expr}
44

55
object Macros:
66
inline def location: Location = ${locationImpl}
77

8-
private def locationImpl(using ctx: QuoteContext): Expr[Location] =
9-
import ctx.reflect.rootPosition
10-
val file = Expr(rootPosition.sourceFile.jpath.toString)
11-
val line = Expr(rootPosition.startLine + 1)
8+
private def locationImpl(using ctx: Quotes): Expr[Location] =
9+
import ctx.reflect.Position
10+
val pos = Position.ofMacroExpansion
11+
val file = Expr(pos.sourceFile.jpath.toString)
12+
val line = Expr(pos.startLine + 1)
1213
'{new Location($file, $line)}

project/Versions.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
object Versions {
22
val scala213 = "2.13.4"
33
val scala30 = "3.0.0-M3"
4+
// required for testing the Tasty reader
5+
val scala3M1 = "3.0.0-M1"
46
val sbtDotty = "0.4.7"
57
val osLib = "0.7.2"
68
val sourcecode = "0.2.3"

0 commit comments

Comments
 (0)