-
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
e2e77b5
commit c54faa0
Showing
3 changed files
with
50 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,19 @@ | ||
import scala.quoted._ | ||
import scala.language.experimental.macros | ||
|
||
object MacroCompat { | ||
trait LocationMacro { | ||
inline implicit def generate: Location = ${ locationImpl() } | ||
implicit def generate: Location = macro MacroCompatScala2.locationImpl | ||
} | ||
|
||
def locationImpl()(using Quotes): Expr[Location] = '{ new Location(${Expr(0)}) } | ||
} | ||
|
||
object MacroCompatScala2 { | ||
def locationImpl(c: Context): c.Tree = { | ||
import c.universe._ | ||
val line = Literal(Constant(c.enclosingPosition.line)) | ||
New(c.mirror.staticClass(classOf[Location].getName()), 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,30 @@ | ||
trait Context: // Dummy scala.reflect.macros.Context | ||
type Tree = universe.Tree | ||
val universe: Universe | ||
|
||
trait Universe { | ||
type Tree >: Null <: AnyRef with TreeApi | ||
type Literal >: Null <: LiteralApi with TermTree | ||
type TermTree >: Null <: TermTreeApi with Tree | ||
|
||
trait TermTreeApi extends TreeApi { this: TermTree => } | ||
trait LiteralApi extends TermTreeApi { this: Literal => } | ||
trait TreeApi extends Product { this: Tree => } | ||
|
||
type Constant | ||
|
||
type Type | ||
|
||
def Literal(const: Constant): Tree | ||
def Constant(i: Int): Constant | ||
def New(tpe: Type, arg: Tree): Tree | ||
} | ||
|
||
def enclosingPosition: Location | ||
|
||
trait Mirror { | ||
def staticClass(name: String): universe.Type | ||
} | ||
val mirror: Mirror | ||
|
||
class Location(val line: Int) |
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 @@ | ||
object Test extends MacroCompat.LocationMacro |