diff options
author | Som Snytt <som.snytt@gmail.com> | 2014-08-02 14:24:54 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2014-08-12 08:41:14 -0700 |
commit | 2e3583b3644f3acd67933b54b9b61e6d60e9b6bb (patch) | |
tree | 44b86f84a504f109e3c524a98a92babc50c7af5e | |
parent | 7a6947487dc8ea551ff7489b07cc30366b39d25e (diff) | |
download | scala-2e3583b3644f3acd67933b54b9b61e6d60e9b6bb.tar.gz scala-2e3583b3644f3acd67933b54b9b61e6d60e9b6bb.tar.bz2 scala-2e3583b3644f3acd67933b54b9b61e6d60e9b6bb.zip |
SI-8512 Infer a type for f"$args"
The f-interpolator gets a type param that better be Any to avoid
unfortunate widenings.
Hey, it worked!
Unfortunately, when `Any` is inferred, `-Xlint:infer-any` takes notice.
This is probably a greater problem for the f-interpolator than for
quasiquotes, which are a more specialized tool.
-rw-r--r-- | src/library/scala/StringContext.scala | 2 | ||||
-rw-r--r-- | test/files/neg/t7848-interp-warn.flags | 2 | ||||
-rw-r--r-- | test/junit/scala/StringContextTest.scala | 13 |
3 files changed, 15 insertions, 2 deletions
diff --git a/src/library/scala/StringContext.scala b/src/library/scala/StringContext.scala index 2d2601c6fb..20a328ec8f 100644 --- a/src/library/scala/StringContext.scala +++ b/src/library/scala/StringContext.scala @@ -163,7 +163,7 @@ case class StringContext(parts: String*) { */ // The implementation is hardwired to `scala.tools.reflect.MacroImplementations.macro_StringInterpolation_f` // Using the mechanism implemented in `scala.tools.reflect.FastTrack` - def f(args: Any*): String = macro ??? + def f[A >: Any](args: A*): String = macro ??? } object StringContext { diff --git a/test/files/neg/t7848-interp-warn.flags b/test/files/neg/t7848-interp-warn.flags index 7949c2afa2..b0d7bc25cb 100644 --- a/test/files/neg/t7848-interp-warn.flags +++ b/test/files/neg/t7848-interp-warn.flags @@ -1 +1 @@ --Xlint -Xfatal-warnings +-Xlint:missing-interpolator -Xfatal-warnings diff --git a/test/junit/scala/StringContextTest.scala b/test/junit/scala/StringContextTest.scala index bb0e8c4252..608b82bd96 100644 --- a/test/junit/scala/StringContextTest.scala +++ b/test/junit/scala/StringContextTest.scala @@ -62,4 +62,17 @@ class StringContextTest { //assertEquals("????", s"????") assertEquals("!!!!", s"????") // OK to hijack core interpolator ids } + + @Test def fIf() = { + val res = f"${if (true) 2.5 else 2.5}%.2f" + assertEquals("2.50", res) + } + @Test def fIfNot() = { + val res = f"${if (false) 2.5 else 3.5}%.2f" + assertEquals("3.50", res) + } + @Test def fHeteroArgs() = { + val res = f"${3.14}%.2f rounds to ${3}%d" + assertEquals("3.14 rounds to 3", res) + } } |