File tree 3 files changed +50
-0
lines changed
3 files changed +50
-0
lines changed Original file line number Diff line number Diff line change
1
+ import scala .quoted ._
2
+ import scala .language .experimental .macros
3
+
4
+ object MacroCompat {
5
+ trait LocationMacro {
6
+ inline implicit def generate : Location = $ { locationImpl() }
7
+ implicit def generate : Location = macro MacroCompatScala2 .locationImpl
8
+ }
9
+
10
+ def locationImpl ()(using Quotes ): Expr [Location ] = ' { new Location ($ {Expr (0 )}) }
11
+ }
12
+
13
+ object MacroCompatScala2 {
14
+ def locationImpl (c : Context ): c.Tree = {
15
+ import c .universe ._
16
+ val line = Literal (Constant (c.enclosingPosition.line))
17
+ New (c.mirror.staticClass(classOf [Location ].getName()), line)
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ trait Context : // Dummy scala.reflect.macros.Context
2
+ type Tree = universe.Tree
3
+ val universe : Universe
4
+
5
+ trait Universe {
6
+ type Tree >: Null <: AnyRef with TreeApi
7
+ type Literal >: Null <: LiteralApi with TermTree
8
+ type TermTree >: Null <: TermTreeApi with Tree
9
+
10
+ trait TermTreeApi extends TreeApi { this : TermTree => }
11
+ trait LiteralApi extends TermTreeApi { this : Literal => }
12
+ trait TreeApi extends Product { this : Tree => }
13
+
14
+ type Constant
15
+
16
+ type Type
17
+
18
+ def Literal (const : Constant ): Tree
19
+ def Constant (i : Int ): Constant
20
+ def New (tpe : Type , arg : Tree ): Tree
21
+ }
22
+
23
+ def enclosingPosition : Location
24
+
25
+ trait Mirror {
26
+ def staticClass (name : String ): universe.Type
27
+ }
28
+ val mirror : Mirror
29
+
30
+ class Location (val line : Int )
Original file line number Diff line number Diff line change
1
+ object Test extends MacroCompat .LocationMacro
You can’t perform that action at this time.
0 commit comments