-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added -Xmacro-settings option and API with the same behaviour as in S…
…cala2 added test-case from #12039 with workarround against changes in compiler Co-authored-by: David Barri <japgolly@gmail.com> Co-authored-by: Nicolas Stucki <nicolas.stucki@gmail.com>
- Loading branch information
1 parent
0b4c6e7
commit d122b13
Showing
11 changed files
with
137 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
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
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
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
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
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 @@ | ||
I'm a info msg | ||
I'm a warn msg | ||
a = [] | ||
b = [1] | ||
c.b.a = [x.y.z=1] | ||
wat is not defined |
35 changes: 35 additions & 0 deletions
35
tests/run-custom-args/Xmacro-settings/compileTimeEnv/Logging.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,35 @@ | ||
import scala.compiletime.* | ||
import scala.quoted.* | ||
|
||
|
||
object Logging { | ||
|
||
// Just use your imagination for now :) | ||
private inline val Trace = 0 | ||
private inline val Debug = 1 | ||
private inline val Info = 2 | ||
private inline val Warn = 3 | ||
|
||
private transparent inline def chosenThreshold: Int = ${ | ||
choosenTresholdImpl | ||
} | ||
|
||
|
||
private def choosenTresholdImpl(using Quotes):Expr[Int] = | ||
import quotes.reflect.* | ||
MacroEnv.getInMacro("myLogger.level") match | ||
case Some("TRACE") => Expr(Trace) | ||
case Some("DEBUG") => Expr(Debug) | ||
case Some("INFO") => Expr(Info) | ||
case Some("WARN") => Expr(Warn) | ||
case Some(x) => report.errorAndAbort("Unsupported logging level: " + x) | ||
case None => Expr(Trace) | ||
|
||
private inline def log(inline lvl: Int, inline msg: String): Unit = | ||
inline if lvl >= chosenThreshold then println(msg) | ||
|
||
inline def trace(inline msg: String): Unit = log(Trace, msg) | ||
inline def debug(inline msg: String): Unit = log(Debug, msg) | ||
inline def info (inline msg: String): Unit = log(Info , msg) | ||
inline def warn (inline msg: String): Unit = log(Warn , msg) | ||
} |
25 changes: 25 additions & 0 deletions
25
tests/run-custom-args/Xmacro-settings/compileTimeEnv/MacroEnv.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,25 @@ | ||
import scala.quoted.* | ||
|
||
object MacroEnv { | ||
|
||
transparent inline def get(inline key:String):Option[String] = ${ | ||
getImpl('key) | ||
} | ||
|
||
def getImpl(key:Expr[String])(using Quotes):Expr[Option[String]] = { | ||
import quotes.reflect.* | ||
val retval = getInMacro(key.valueOrAbort) | ||
Expr(retval) | ||
} | ||
|
||
def getInMacro(key:String)(using Quotes):Option[String] = { | ||
import quotes.reflect.* | ||
val keyEq = key + "=" | ||
CompilationInfo.XmacroSettings.collectFirst{ | ||
case v if v == key => "" | ||
case v if v.startsWith(keyEq) => | ||
v.substring(keyEq.length) | ||
} | ||
} | ||
|
||
} |
30 changes: 30 additions & 0 deletions
30
tests/run-custom-args/Xmacro-settings/compileTimeEnv/Test.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,30 @@ | ||
import scala.compiletime.* | ||
|
||
object Test { | ||
import Logging.* | ||
|
||
def main(args: Array[String]): Unit = { | ||
runLog() | ||
runBasic() | ||
} | ||
|
||
def runLog(): Unit = { | ||
trace("I'm a trace msg") | ||
debug("I'm a debug msg") | ||
info("I'm a info msg") | ||
warn("I'm a warn msg") | ||
} | ||
|
||
def runBasic(): Unit = { | ||
printEnv("a") | ||
printEnv("b") | ||
printEnv("c.b.a") | ||
printEnv("wat") | ||
} | ||
|
||
inline def printEnv(inline k: String): Unit = | ||
inline MacroEnv.get(k) match | ||
case Some(v) => println(s"$k = [$v]") | ||
case None => println(k + " is not defined") | ||
|
||
} |
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,15 @@ | ||
package x | ||
|
||
import scala.quoted.* | ||
|
||
object M: | ||
|
||
inline def settingsContains(inline x:String): Boolean = ${ | ||
settingsContainsImpl('x) | ||
} | ||
|
||
def settingsContainsImpl(x:Expr[String])(using Quotes): Expr[Boolean] = | ||
import quotes.reflect.* | ||
val v = x.valueOrAbort | ||
val r = CompilationInfo.XmacroSettings.contains(v) | ||
Expr(r) |
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,10 @@ | ||
import x.* | ||
|
||
object Test { | ||
|
||
def main(args: Array[String]):Unit = | ||
assert(M.settingsContains("one")) | ||
assert(!M.settingsContains("notwo")) | ||
assert(M.settingsContains("two")) | ||
|
||
} |