Skip to content

Commit 5e0880f

Browse files
committed
Merge pull request scala#3909 from som-snytt/issue/8512
SI-8512 Infer a type for f"$args"
2 parents 7a69474 + 606a553 commit 5e0880f

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

src/library/scala/StringContext.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ case class StringContext(parts: String*) {
163163
*/
164164
// The implementation is hardwired to `scala.tools.reflect.MacroImplementations.macro_StringInterpolation_f`
165165
// Using the mechanism implemented in `scala.tools.reflect.FastTrack`
166-
def f(args: Any*): String = macro ???
166+
def f[A >: Any](args: A*): String = macro ???
167167
}
168168

169169
object StringContext {

src/reflect/scala/reflect/api/Quasiquotes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ trait Quasiquotes { self: Universe =>
1313
protected trait api {
1414
// implementation is hardwired to `dispatch` method of `scala.tools.reflect.quasiquotes.Quasiquotes`
1515
// using the mechanism implemented in `scala.tools.reflect.FastTrack`
16-
def apply[T](args: T*): Tree = macro ???
16+
def apply[A >: Any](args: A*): Tree = macro ???
1717
def unapply(scrutinee: Any): Any = macro ???
1818
}
1919
object q extends api
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
-Xlint -Xfatal-warnings
1+
-Xlint:missing-interpolator -Xfatal-warnings

test/files/pos/t8013.flags

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
-Xfatal-warnings -Xlint
1+
-Xfatal-warnings -Xlint:-infer-any,_

test/junit/scala/StringContextTest.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,17 @@ class StringContextTest {
6262
//assertEquals("????", s"????")
6363
assertEquals("!!!!", s"????") // OK to hijack core interpolator ids
6464
}
65+
66+
@Test def fIf() = {
67+
val res = f"${if (true) 2.5 else 2.5}%.2f"
68+
assertEquals("2.50", res)
69+
}
70+
@Test def fIfNot() = {
71+
val res = f"${if (false) 2.5 else 3.5}%.2f"
72+
assertEquals("3.50", res)
73+
}
74+
@Test def fHeteroArgs() = {
75+
val res = f"${3.14}%.2f rounds to ${3}%d"
76+
assertEquals("3.14 rounds to 3", res)
77+
}
6578
}

test/junit/scala/reflect/QTest.scala

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
2+
package scala.reflect
3+
4+
import org.junit.Test
5+
import org.junit.Assert._
6+
import org.junit.runner.RunWith
7+
import org.junit.runners.JUnit4
8+
9+
import scala.tools.testing.AssertUtil._
10+
11+
@RunWith(classOf[JUnit4])
12+
class QTest {
13+
14+
import reflect.runtime._
15+
import universe._
16+
@Test def qConstantsNotHomogenized() = {
17+
//Apply(Select(Literal(Constant(1.0)), TermName("$plus")), List(Literal(Constant(1.0))))
18+
val t = q"${1} + ${1.0}"
19+
val Apply(Select(Literal(Constant(i)), TermName("$plus")), List(Literal(Constant(j)))) = t
20+
assertEquals(1, i)
21+
assertEquals(1.0, j)
22+
}
23+
}

0 commit comments

Comments
 (0)