Skip to content

Commit aac4201

Browse files
Merge pull request #10313 from dotty-staging/replace-compiletime-code-with-codeof
Replace `compiletime.code` with `compiletime.codeOf`
2 parents 3c04f9b + 3ef9141 commit aac4201

File tree

8 files changed

+58
-97
lines changed

8 files changed

+58
-97
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ class Definitions {
224224
@tu lazy val ScalaXmlPackageClass: Symbol = getPackageClassIfDefined("scala.xml")
225225

226226
@tu lazy val CompiletimePackageObject: Symbol = requiredModule("scala.compiletime.package")
227-
@tu lazy val Compiletime_code: Symbol = CompiletimePackageObject.requiredMethod("code")
227+
@tu lazy val Compiletime_codeOf: Symbol = CompiletimePackageObject.requiredMethod("codeOf")
228228
@tu lazy val Compiletime_erasedValue : Symbol = CompiletimePackageObject.requiredMethod("erasedValue")
229229
@tu lazy val Compiletime_error : Symbol = CompiletimePackageObject.requiredMethod(nme.error)
230230
@tu lazy val Compiletime_requireConst: Symbol = CompiletimePackageObject.requiredMethod("requireConst")

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -333,31 +333,9 @@ object Inliner {
333333
val errors = compileForErrors(tree, false)
334334
packErrors(errors)
335335

336-
def code(strCtx: Tree, argsTree: Tree, pos: SrcPos)(using Context): Tree =
337-
object Consts:
338-
def unapply(trees: List[Tree]): Option[List[String]] =
339-
trees match
340-
case Nil => Some(Nil)
341-
case Literal(Constant(part: String)) :: Consts(rest) => Some(part :: rest)
342-
case _ => None
343-
strCtx match
344-
case Apply(stringContextApply, List(Typed(SeqLiteral(Consts(parts), _), _)))
345-
if stringContextApply.symbol == defn.StringContextModule_apply =>
346-
argsTree match
347-
case Typed(SeqLiteral(args, _), _) =>
348-
if parts.size == args.size + 1 then
349-
val argStrs = args.map(_.show)
350-
val showed = StringContext(parts: _*).s(argStrs: _*)
351-
Literal(Constant(showed)).withSpan(pos.span)
352-
else
353-
report.error("Wrong number of arguments for StringContext.s", strCtx.srcPos)
354-
ref(defn.Predef_undefined)
355-
case _ =>
356-
report.error("Exprected explicit arguments to code", strCtx.srcPos)
357-
ref(defn.Predef_undefined)
358-
case _ =>
359-
report.error("Exprected StringContext.apply with explicit `parts` arguments", strCtx.srcPos)
360-
ref(defn.Predef_undefined)
336+
/** Expand call to scala.compiletime.codeOf */
337+
def codeOf(arg: Tree, pos: SrcPos)(using Context): Tree =
338+
Literal(Constant(arg.show)).withSpan(pos.span)
361339
}
362340
}
363341

@@ -642,15 +620,16 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
642620
/** The Inlined node representing the inlined call */
643621
def inlined(sourcePos: SrcPos): Tree = {
644622

645-
// Special handling of `requireConst`
623+
// Special handling of `requireConst` and `codeOf`
646624
callValueArgss match
647-
case (arg :: Nil) :: Nil if inlinedMethod == defn.Compiletime_requireConst =>
648-
arg match
649-
case ConstantValue(_) | Inlined(_, Nil, Typed(ConstantValue(_), _)) => // ok
650-
case _ => report.error(em"expected a constant value but found: $arg", arg.srcPos)
651-
return Literal(Constant(())).withSpan(sourcePos.span)
652-
case (strCtx :: Nil) :: (args :: Nil) :: Nil if inlinedMethod == defn.Compiletime_code =>
653-
return Intrinsics.code(strCtx, args, call.srcPos)
625+
case (arg :: Nil) :: Nil =>
626+
if inlinedMethod == defn.Compiletime_requireConst then
627+
arg match
628+
case ConstantValue(_) | Inlined(_, Nil, Typed(ConstantValue(_), _)) => // ok
629+
case _ => report.error(em"expected a constant value but found: $arg", arg.srcPos)
630+
return Literal(Constant(())).withSpan(sourcePos.span)
631+
else if inlinedMethod == defn.Compiletime_codeOf then
632+
return Intrinsics.codeOf(arg, call.srcPos)
654633
case _ =>
655634

656635
// Special handling of `constValue[T]` and `constValueOpt[T]`

library/src/scala/compiletime/package.scala

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,40 +26,31 @@ package object compiletime {
2626
* ```
2727
* or
2828
* ```scala
29-
* error(code"My error of this code: ${println("foo")}")
30-
* ```
31-
* or
32-
* ```scala
3329
* inline def errorOnThisCode(inline x: Any) =
34-
* error(code"My error of this code: $x")
30+
* error("My error of this code: " + codeOf(x))
3531
* ```
3632
*/
3733
inline def error(inline msg: String): Nothing = ???
3834

39-
extension (inline self: StringContext):
40-
/** Returns the string representation of interpolated elaborated code:
41-
*
42-
* ```scala
43-
* inline def logged(inline p1: Any) = {
44-
* val c = code"code: $p1"
45-
* val res = p1
46-
* (c, p1)
47-
* }
48-
* logged(identity("foo"))
49-
* // above is equivalent to:
50-
* // ("code: scala.Predef.identity("foo")", identity("foo"))
51-
* ```
52-
*
53-
* The formatting of the code is not stable across version of the compiler.
54-
*
55-
* @note only `inline` arguments will be displayed as "code".
56-
* Other values may display unintutively.
57-
*/
58-
transparent inline def code (inline args: Any*): String =
59-
// implemented in dotty.tools.dotc.typer.Inliner.Intrinsics
60-
error("Compiler bug: `code` was not evaluated by the compiler")
61-
62-
end extension
35+
/** Returns the string representation of argument code:
36+
*
37+
* ```scala
38+
* inline def logged(inline p1: Any) =
39+
* ("code: " + codeOf(p1), p1)
40+
*
41+
* logged(identity("foo"))
42+
* // above is equivalent to:
43+
* // ("code: scala.Predef.identity("foo")", identity("foo"))
44+
* ```
45+
*
46+
* The formatting of the code is not stable across version of the compiler.
47+
*
48+
* @note only `inline` arguments will be displayed as "code".
49+
* Other values may display unintutively.
50+
*/
51+
transparent inline def codeOf(arg: Any): String =
52+
// implemented in dotty.tools.dotc.typer.Inliner.Intrinsics
53+
error("Compiler bug: `codeOf` was not evaluated by the compiler")
6354

6455
/** Checks at compiletime that the provided values is a constant after
6556
* inlining and constant folding.

tests/neg-macros/i6622f.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@ object Test {
66
fail(println("foo")) // error
77
}
88

9-
inline def fail(inline p1: Any) = error(code"failed: $p1 ...")
9+
inline def fail(inline p1: Any) = error("failed: " + codeOf(p1) + " ...")
1010

1111
}

tests/run-macros/beta-reduce-inline-result/Test_2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,9 @@ object Test {
4545
{ (i: Int) => i + 1 } : V
4646

4747
def main(argv : Array[String]) : Unit = {
48-
println(code"compile-time: ${Macros.betaReduce(dummy1)(3)}")
48+
println(s"compile-time: ${codeOf(Macros.betaReduce(dummy1)(3))}")
4949
println(s"run-time: ${Macros.betaReduce(dummy1)(3)}")
50-
println(code"compile-time: ${Macros.betaReduce(dummy2)(1)}")
50+
println(s"compile-time: ${codeOf(Macros.betaReduce(dummy2)(1))}")
5151
// paramrefs have to be properly substituted in this case
5252
println(s"run-time: ${Macros.betaReduce(dummy2)(1)}")
5353

tests/run-macros/i6622.check

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
11
abc println(34) ...
2-
abc println(34)
3-
println(34) ...
4-
println(34)
5-
...
6-

tests/run-macros/i6622.scala

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,9 @@ import scala.compiletime._
33
object Test {
44

55
def main(args: Array[String]): Unit = {
6-
println(code"abc ${println(34)} ...")
7-
println(code"abc ${println(34)}")
8-
println(code"${println(34)} ...")
9-
println(code"${println(34)}")
10-
println(code"...")
11-
println(testConstant(code""))
6+
println(s"abc ${codeOf(println(34))} ...")
127
}
138

9+
1410
inline def testConstant(inline msg: String): String = msg
1511
}

tests/run-macros/i8306.scala

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -56,38 +56,38 @@ object Test extends Test8 {
5656
}
5757

5858
def main(argv: Array[String]): Unit = {
59-
println(code"compile-time: ${test1}")
60-
println(s"run-time: ${test1}")
59+
println(s"compile-time: ${codeOf(test1)}")
60+
println(s"run-time: ${codeOf(test1)}")
6161

62-
println(code"compile-time: ${test2}")
63-
println(s"run-time: ${test2}")
62+
println(s"compile-time: ${codeOf(test2)}")
63+
println(s"run-time: ${codeOf(test2)}")
6464

65-
println(code"compile-time: ${test3}")
66-
println(s"run-time: ${test3}")
65+
println(s"compile-time: ${codeOf(test3)}")
66+
println(s"run-time: ${codeOf(test3)}")
6767

68-
println(code"compile-time: ${test4}")
69-
println(s"run-time: ${test4}")
68+
println(s"compile-time: ${codeOf(test4)}")
69+
println(s"run-time: ${codeOf(test4)}")
7070

7171
// this is the only test that should not be possible to fully inline,
7272
// because it references a non-inline value
73-
println(code"compile-time: ${test5}")
74-
println(s"run-time: ${test5}")
73+
println(s"compile-time: ${codeOf(test5)}")
74+
println(s"run-time: ${codeOf(test5)}")
7575

76-
println(code"compile-time: ${test6}")
77-
println(s"run-time: ${test6}")
76+
println(s"compile-time: ${codeOf(test6)}")
77+
println(s"run-time: ${codeOf(test6)}")
7878

79-
println(code"compile-time: ${test7}")
80-
println(s"run-time: ${test7}")
79+
println(s"compile-time: ${codeOf(test7)}")
80+
println(s"run-time: ${codeOf(test7)}")
8181

82-
println(code"compile-time: ${test8}")
83-
println(s"run-time: ${test8}")
82+
println(s"compile-time: ${codeOf(test8)}")
83+
println(s"run-time: ${codeOf(test8)}")
8484

85-
println(code"compile-time: ${test9}")
86-
println(s"run-time: ${test9}")
85+
println(s"compile-time: ${codeOf(test9)}")
86+
println(s"run-time: ${codeOf(test9)}")
8787

8888
// with type parameter
89-
println(code"compile-time: ${test10}")
90-
println(s"run-time: ${test10}")
89+
println(s"compile-time: ${codeOf(test10)}")
90+
println(s"run-time: ${codeOf(test10)}")
9191
}
9292
}
9393

0 commit comments

Comments
 (0)