Skip to content

Commit 3f1cb8a

Browse files
committed
Intrinsify f interpolator
1 parent ebb4799 commit 3f1cb8a

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,10 +631,17 @@ class Definitions {
631631
def StringContextS(implicit ctx: Context): Symbol = StringContextSR.symbol
632632
lazy val StringContextRawR: TermRef = StringContextClass.requiredMethodRef(nme.raw_)
633633
def StringContextRaw(implicit ctx: Context): Symbol = StringContextRawR.symbol
634+
lazy val StringContext_fR: TermRef = StringContextClass.requiredMethodRef(nme.f)
635+
def StringContext_f(implicit ctx: Context): Symbol = StringContext_fR.symbol
634636
def StringContextModule(implicit ctx: Context): Symbol = StringContextClass.companionModule
635637
lazy val StringContextModule_applyR: TermRef = StringContextModule.requiredMethodRef(nme.apply)
636638
def StringContextModule_apply(implicit ctx: Context): Symbol = StringContextModule_applyR.symbol
637639

640+
lazy val InternalStringContextModuleR: TermRef = ctx.requiredModuleRef("scala.internal.StringContext")
641+
def InternalStringContextModule(implicit ctx: Context): Symbol = InternalStringContextModuleR.termSymbol
642+
lazy val InternalStringContextModule_fR: TermRef = InternalStringContextModule.requiredMethodRef(nme.f)
643+
def InternalStringContextModule_f(implicit ctx: Context): Symbol = InternalStringContextModule_fR.symbol
644+
638645
lazy val PartialFunctionType: TypeRef = ctx.requiredClassRef("scala.PartialFunction")
639646
def PartialFunctionClass(implicit ctx: Context): ClassSymbol = PartialFunctionType.symbol.asClass
640647
lazy val PartialFunction_isDefinedAtR: TermRef = PartialFunctionClass.requiredMethodRef(nme.isDefinedAt)

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2656,8 +2656,14 @@ class Typer extends Namer
26562656
readaptSimplified(Inliner.inlineCall(tree, pt))
26572657
}
26582658
else if (tree.symbol.is(Macro, butNot = Inline)) {
2659-
ctx.error("Scala 2 macro cannot be used in Dotty. See http://dotty.epfl.ch/docs/reference/dropped-features/macros.html", tree.sourcePos)
2660-
tree
2659+
if (tree.symbol eq defn.StringContext_f) {
2660+
val Apply(TypeApply(Select(sc, _), _), args) = tree
2661+
val newCall = ref(defn.InternalStringContextModule_f).appliedTo(sc).appliedToArgs(args)
2662+
Inliner.inlineCall(newCall, pt)
2663+
} else {
2664+
ctx.error("Scala 2 macro cannot be used in Dotty. See http://dotty.epfl.ch/docs/reference/dropped-features/macros.html", tree.sourcePos)
2665+
tree
2666+
}
26612667
}
26622668
else if (tree.tpe <:< pt) {
26632669
if (pt.hasAnnotation(defn.InlineParamAnnot))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package scala.internal
2+
3+
import scala.quoted._
4+
5+
object StringContext {
6+
7+
inline def f(sc: => scala.StringContext)(args: Any*): String = ${ fImpl('sc, 'args) }
8+
9+
private def fImpl(sc: Expr[StringContext], args: Expr[Seq[Any]]): Expr[String] = {
10+
// TODO implement f interpolation checks and addapt sc.parts
11+
// See https://github.com/alemannosara/f-interpolators-in-Dotty-macros
12+
'{ $sc.parts.mkString.format($args: _*) }
13+
}
14+
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package scala.internal
2+
3+
object StringContext {
4+
5+
@forceInline def f(sc: => scala.StringContext)(args: Any*): String =
6+
throw new Exception("non-boostrapped library")
7+
8+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
abc
2+
Hello world
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
println(f"abc")
5+
println(f"Hello ${"world"}%s")
6+
}
7+
}

0 commit comments

Comments
 (0)