Skip to content

Commit 1045905

Browse files
dos65olsdavis
authored andcommitted
Do not harmonize constant args if their type doesn't affect resType
Fixes the following sample from scala#9939: ```scala scala> f"${3.14}%.2f rounds to ${3}%d" 1 |f"${3.14}%.2f rounds to ${3}%d" | ^ | type mismatch; | found : Double | required: Int | This location contains code that was inlined from rs$line$2:1 ``` At the moment when StringInterpolation transformation was performed instead of receiving `List(Constant(3.0), Constant(3))` arguments they were: `List(Constant(3.0), Constant(3.0))` (the second one was converted to double because of harmonization). That caused the reported type mismatch. In Scala2 harmonization doesn't happen if the resulting type is fully defined. For f-interp it shouldn't happen too as it's resulting type is a `String`: ```scala def f[A >: Any](args: A*): String = macro ??? ```
1 parent aa4f7ec commit 1045905

File tree

5 files changed

+14
-1
lines changed

5 files changed

+14
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,10 @@ trait Applications extends Compatibility {
744744
typedArgBuf += seqToRepeated(SeqLiteral(args, elemtpt))
745745
}
746746

747-
def harmonizeArgs(args: List[TypedArg]): List[Tree] = harmonize(args)
747+
def harmonizeArgs(args: List[TypedArg]): List[Tree] =
748+
// harmonize args only if resType depends on parameter types
749+
if (isFullyDefined(methodType.resType, ForceDegree.none)) args
750+
else harmonize(args)
748751

749752
override def appPos: SrcPos = app.srcPos
750753

tests/run-macros/f-interpolator-tests.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
integer: 5
22
string: l
33
5, 6, hello
4+
3.14 rounds to 3
45
5
56
6
67
Bob is 1 years old

tests/run-macros/f-interpolator-tests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ object Test {
1010
println(f"integer: ${5}%d")
1111
println(f"string: ${"l"}%s")
1212
println(f"${5}%s, ${6}%d, ${"hello"}%s")
13+
println(f"${3.14}%.2f rounds to ${3}%d")
1314

1415
val x = 5
1516
println(f"$x%d")

tests/run/i9939.check

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[3.0, 42]
2+
[3.0, 42.0]

tests/run/i9939.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def nonHarmonized[A >: Any](args: A*): String = args.mkString("[", ", ", "]")
2+
def harmonized[A >: Any](args: A*): List[A] = args.toList
3+
4+
@main def Test =
5+
println(nonHarmonized(3.0, 42))
6+
println(harmonized(3.0, 42).mkString("[", ", ", "]"))

0 commit comments

Comments
 (0)