Skip to content

Commit e6e549c

Browse files
committed
Use interpolation message kind also in s
1 parent b13d617 commit e6e549c

File tree

5 files changed

+71
-4
lines changed

5 files changed

+71
-4
lines changed

compiler/src/dotty/tools/dotc/transform/localopt/StringInterpolatorOpt.scala

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import dotty.tools.dotc.core.Contexts.*
1010
import dotty.tools.dotc.core.StdNames.*
1111
import dotty.tools.dotc.core.Symbols.*
1212
import dotty.tools.dotc.core.Types.*
13+
import dotty.tools.dotc.printing.Formatting.*
14+
import dotty.tools.dotc.reporting.BadFormatInterpolation
1315
import dotty.tools.dotc.transform.MegaPhase.MiniPhase
1416
import dotty.tools.dotc.typer.ConstFold
1517

@@ -22,16 +24,17 @@ import dotty.tools.dotc.typer.ConstFold
2224
*/
2325
class StringInterpolatorOpt extends MiniPhase:
2426
import tpd.*
27+
import StringInterpolatorOpt.*
2528

26-
override def phaseName: String = StringInterpolatorOpt.name
29+
override def phaseName: String = name
2730

2831
override def description: String = StringInterpolatorOpt.description
2932

3033
override def checkPostCondition(tree: tpd.Tree)(using Context): Unit =
3134
tree match
3235
case tree: RefTree =>
3336
val sym = tree.symbol
34-
assert(!StringInterpolatorOpt.isCompilerIntrinsic(sym),
37+
assert(!isCompilerIntrinsic(sym),
3538
i"$tree in ${ctx.owner.showLocated} should have been rewritten by phase $phaseName")
3639
case _ =>
3740

@@ -117,10 +120,10 @@ class StringInterpolatorOpt extends MiniPhase:
117120
!(tp =:= defn.StringType)
118121
&& {
119122
tp =:= defn.UnitType
120-
&& { report.warning("interpolated Unit value", t.srcPos); true }
123+
&& { report.warning(bfi"interpolated Unit value", t.srcPos); true }
121124
||
122125
!tp.isPrimitiveValueType
123-
&& { report.warning("interpolation uses toString", t.srcPos); true }
126+
&& { report.warning(bfi"interpolation uses toString", t.srcPos); true }
124127
}
125128
if ctx.settings.Whas.toStringInterpolated then
126129
checkIsStringify(t.tpe): Unit
@@ -186,3 +189,7 @@ object StringInterpolatorOpt:
186189
sym == defn.StringContext_s ||
187190
sym == defn.StringContext_f ||
188191
sym == defn.StringContext_raw
192+
193+
extension (sc: StringContext)
194+
def bfi(args: Shown*)(using Context): BadFormatInterpolation =
195+
BadFormatInterpolation(i(sc)(args*))

tests/run/i23693.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
k == K(42)
2+
\k == \K(42)
3+
k ==
4+
k == K(42)

tests/run/i23693.scala

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//> using options -Wtostring-interpolated
2+
3+
// verify warning messages and runtime result
4+
// never mind, the test rig doesn't log diagnostics! unlike beloved partest.
5+
6+
case class K(i: Int)
7+
8+
@main def Test =
9+
val k = K(42)
10+
println:
11+
s"k == $k"
12+
println:
13+
raw"\k == \$k"
14+
println:
15+
f"k == $k"
16+
println:
17+
f"k == $k%s"

tests/warn/i23693.check

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- [E209] Interpolation Warning: tests/warn/i23693.scala:11:12 ---------------------------------------------------------
2+
11 | s"k == $k" // warn
3+
| ^
4+
| interpolation uses toString
5+
-- [E209] Interpolation Warning: tests/warn/i23693.scala:13:16 ---------------------------------------------------------
6+
13 | raw"\k == \$k" // warn
7+
| ^
8+
| interpolation uses toString
9+
-- [E209] Interpolation Warning: tests/warn/i23693.scala:15:12 ---------------------------------------------------------
10+
15 | f"k == $k" // warn
11+
| ^
12+
| interpolation uses toString
13+
-- [E209] Interpolation Warning: tests/warn/i23693.scala:17:14 ---------------------------------------------------------
14+
17 | f"k == $k%s" // warn
15+
| ^
16+
| interpolation uses toString
17+
-- [E209] Interpolation Warning: tests/warn/i23693.scala:19:18 ---------------------------------------------------------
18+
19 | s"show == ${k.show}" // warn
19+
| ^^^^^^
20+
| interpolated Unit value

tests/warn/i23693.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//> using options -Wtostring-interpolated
2+
3+
// verify warning messages and runtime result
4+
5+
case class K(i: Int):
6+
def show: Unit = ()
7+
8+
@main def Test =
9+
val k = K(42)
10+
println:
11+
s"k == $k" // warn
12+
println:
13+
raw"\k == \$k" // warn
14+
println:
15+
f"k == $k" // warn
16+
println:
17+
f"k == $k%s" // warn
18+
println:
19+
s"show == ${k.show}" // warn

0 commit comments

Comments
 (0)