From b0dd01ebb4b0cc41a6629357522217bfe14821fb Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 14 Jul 2014 07:32:03 -0700 Subject: SI-8608 f interpolator emits constant strings When invoking `format` is obviated by a lack of formatting fields, then just degenerate to an unenhanced constant string. This means it doesn't cost anything to use f"$$ordinary" in place of "$ordinary", which may cause warnings under -Xlint. Note that certain format literals, in particular for line separator %n, are not actually literals and can't be replaced at compile time. --- src/compiler/scala/tools/reflect/FormatInterpolator.scala | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/reflect/FormatInterpolator.scala b/src/compiler/scala/tools/reflect/FormatInterpolator.scala index 57be1afdfe..4fa06be60f 100644 --- a/src/compiler/scala/tools/reflect/FormatInterpolator.scala +++ b/src/compiler/scala/tools/reflect/FormatInterpolator.scala @@ -183,11 +183,13 @@ abstract class FormatInterpolator { } //q"{..$evals; ${fstring.toString}.format(..$ids)}" - locally { + val format = fstring.toString + if (ids.isEmpty && !format.contains("%")) Literal(Constant(format)) + else { val expr = Apply( Select( - Literal(Constant(fstring.toString)), + Literal(Constant(format)), newTermName("format")), ids.toList ) -- cgit v1.2.3 From 146bdd7073e1184af36d3378595297f3cb92bccf Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Mon, 14 Jul 2014 08:02:41 -0700 Subject: SI-8608 f-interpolator inlines StringOps Instead of "hi".format(), emit new _root_.s.c.i.StringOps("hi").format(), to clarify intent and avoid picking up some other implicit enhancement. A further optimization would be to use String.format directly when that is possible. The ticket says it is not possible for ``` f"${BigDecimal(3.4)}%e" ``` --- src/compiler/scala/tools/reflect/FormatInterpolator.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/reflect/FormatInterpolator.scala b/src/compiler/scala/tools/reflect/FormatInterpolator.scala index 4fa06be60f..b445f1e2bb 100644 --- a/src/compiler/scala/tools/reflect/FormatInterpolator.scala +++ b/src/compiler/scala/tools/reflect/FormatInterpolator.scala @@ -182,15 +182,23 @@ abstract class FormatInterpolator { case (part, n) => copyPart(part, n) } - //q"{..$evals; ${fstring.toString}.format(..$ids)}" + //q"{..$evals; new StringOps(${fstring.toString}).format(..$ids)}" val format = fstring.toString if (ids.isEmpty && !format.contains("%")) Literal(Constant(format)) else { + val scalaPackage = Select(Ident(nme.ROOTPKG), TermName("scala")) + val newStringOps = Select( + New(Select(Select(Select(scalaPackage, + TermName("collection")), TermName("immutable")), TypeName("StringOps"))), + termNames.CONSTRUCTOR + ) val expr = Apply( Select( - Literal(Constant(format)), - newTermName("format")), + Apply( + newStringOps, + List(Literal(Constant(format)))), + TermName("format")), ids.toList ) val p = c.macroApplication.pos -- cgit v1.2.3