summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSom Snytt <som.snytt@gmail.com>2014-07-14 07:32:03 -0700
committerSom Snytt <som.snytt@gmail.com>2014-07-14 07:32:03 -0700
commitb0dd01ebb4b0cc41a6629357522217bfe14821fb (patch)
tree775eeaf9386ed24024f028fe9a8f30640964bf55
parentaea6519685561ee076e7fdaac48c2bf970389b83 (diff)
downloadscala-b0dd01ebb4b0cc41a6629357522217bfe14821fb.tar.gz
scala-b0dd01ebb4b0cc41a6629357522217bfe14821fb.tar.bz2
scala-b0dd01ebb4b0cc41a6629357522217bfe14821fb.zip
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.
-rw-r--r--src/compiler/scala/tools/reflect/FormatInterpolator.scala6
-rw-r--r--test/files/run/stringinterpolation_macro-run.check4
-rw-r--r--test/files/run/stringinterpolation_macro-run.scala1
-rw-r--r--test/files/run/t8608-no-format.scala15
4 files changed, 24 insertions, 2 deletions
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
)
diff --git a/test/files/run/stringinterpolation_macro-run.check b/test/files/run/stringinterpolation_macro-run.check
index ead61e76ac..c7f46bac87 100644
--- a/test/files/run/stringinterpolation_macro-run.check
+++ b/test/files/run/stringinterpolation_macro-run.check
@@ -63,5 +63,9 @@ She is 4 feet tall.
05/26/12
05/26/12
%
+ mind
+------
+matter
+
7 7 9
7 9 9
diff --git a/test/files/run/stringinterpolation_macro-run.scala b/test/files/run/stringinterpolation_macro-run.scala
index a6def98540..e18375d521 100644
--- a/test/files/run/stringinterpolation_macro-run.scala
+++ b/test/files/run/stringinterpolation_macro-run.scala
@@ -115,6 +115,7 @@ println(f"""${"1234"}%TD""")
// literals and arg indexes
println(f"%%")
+println(f" mind%n------%nmatter%n")
println(f"${7}%d %<d ${9}%d")
println(f"${7}%d %2$$d ${9}%d")
diff --git a/test/files/run/t8608-no-format.scala b/test/files/run/t8608-no-format.scala
new file mode 100644
index 0000000000..71c369a7ea
--- /dev/null
+++ b/test/files/run/t8608-no-format.scala
@@ -0,0 +1,15 @@
+
+import scala.tools.partest.JavapTest
+
+object Test extends JavapTest {
+ def code = """
+ |f"hello, world"
+ |:javap -prv -
+ """.stripMargin
+
+ // no format
+ override def yah(res: Seq[String]) = {
+ // note: avoid the word "information"
+ res forall (!_.contains("StringOps.format"))
+ }
+}