Skip to content

Commit 11c1791

Browse files
committed
added examples from scala#12039 with workarround against scala#14245
1 parent 27a6193 commit 11c1791

File tree

7 files changed

+99
-1
lines changed

7 files changed

+99
-1
lines changed

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,8 @@ class CompilationTests {
207207
compileFile("tests/run-custom-args/defaults-serizaliable-no-forwarders.scala", defaultOptions and "-Xmixin-force-forwarders:false"),
208208
compileFilesInDir("tests/run-custom-args/erased", defaultOptions.and("-language:experimental.erasedDefinitions")),
209209
compileFilesInDir("tests/run-custom-args/fatal-warnings", defaultOptions.and("-Xfatal-warnings")),
210-
compileDir("tests/run-custom-args/Xmacro-settings", defaultOptions.and("-Xmacro-settings:one,two,three")),
210+
compileDir("tests/run-custom-args/Xmacro-settings/simple", defaultOptions.and("-Xmacro-settings:one,two,three")),
211+
compileDir("tests/run-custom-args/Xmacro-settings/compileTimeEnv", defaultOptions.and("-Xmacro-settings:a,b=1,c.b.a=x.y.z=1,myLogger.level=INFO")),
211212
compileFilesInDir("tests/run-deep-subtype", allowDeepSubtypes),
212213
compileFilesInDir("tests/run", defaultOptions.and("-Ysafe-init"))
213214
).checkRuns()
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
I'm a info msg
2+
I'm a warn msg
3+
a = []
4+
b = [1]
5+
c.b.a = [x.y.z=1]
6+
wat is not defined
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import scala.compiletime.*
2+
import scala.quoted.*
3+
4+
5+
object Logging {
6+
7+
// Just use your imagination for now :)
8+
private inline val Trace = 0
9+
private inline val Debug = 1
10+
private inline val Info = 2
11+
private inline val Warn = 3
12+
13+
private transparent inline def chosenThreshold: Int = ${
14+
choosenTresholdImpl
15+
}
16+
17+
18+
private def choosenTresholdImpl(using Quotes):Expr[Int] =
19+
import quotes.reflect.*
20+
MacroEnv.getInMacro("myLogger.level") match
21+
case Some("TRACE") => Expr(Trace)
22+
case Some("DEBUG") => Expr(Debug)
23+
case Some("INFO") => Expr(Info)
24+
case Some("WARN") => Expr(Warn)
25+
case Some(x) => report.errorAndAbort("Unsupported logging level: " + x)
26+
case None => Expr(Trace)
27+
28+
private inline def log(inline lvl: Int, inline msg: String): Unit =
29+
inline if lvl >= chosenThreshold then println(msg)
30+
31+
inline def trace(inline msg: String): Unit = log(Trace, msg)
32+
inline def debug(inline msg: String): Unit = log(Debug, msg)
33+
inline def info (inline msg: String): Unit = log(Info , msg)
34+
inline def warn (inline msg: String): Unit = log(Warn , msg)
35+
}
36+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import scala.quoted.*
2+
3+
object MacroEnv {
4+
5+
inline def get(inline key:String):Option[String] = ${
6+
getImpl('key)
7+
}
8+
9+
def getImpl(key:Expr[String])(using Quotes):Expr[Option[String]] = {
10+
import quotes.reflect.*
11+
val retval = getInMacro(key.valueOrAbort)
12+
Expr(retval)
13+
}
14+
15+
def getInMacro(key:String)(using Quotes):Option[String] = {
16+
import quotes.reflect.*
17+
val keyEq = key + "="
18+
CompilationInfo.XmacroSettings.collectFirst{
19+
case v if v == key => ""
20+
case v if v.startsWith(keyEq) =>
21+
v.substring(keyEq.length)
22+
}
23+
}
24+
25+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import scala.compiletime.*
2+
3+
object Test {
4+
import Logging.*
5+
6+
def main(args: Array[String]): Unit = {
7+
runLog()
8+
runBasic()
9+
}
10+
11+
def runLog(): Unit = {
12+
trace("I'm a trace msg")
13+
debug("I'm a debug msg")
14+
info("I'm a info msg")
15+
warn("I'm a warn msg")
16+
}
17+
18+
def runBasic(): Unit = {
19+
printEnv("a")
20+
printEnv("b")
21+
printEnv("c.b.a")
22+
printEnv("wat")
23+
}
24+
25+
inline def printEnv(inline k: String): Unit =
26+
MacroEnv.get(k) match
27+
case Some(v) => println(s"$k = [$v]")
28+
case None => println(k + " is not defined")
29+
30+
}

0 commit comments

Comments
 (0)