Skip to content

Commit 7976598

Browse files
committed
Format interpolator messages get an ID
1 parent de7a6a2 commit 7976598

File tree

7 files changed

+67
-53
lines changed

7 files changed

+67
-53
lines changed

compiler/src/dotty/tools/dotc/reporting/ErrorMessageID.scala

+1
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ enum ErrorMessageID(val isActive: Boolean = true) extends java.lang.Enum[ErrorMe
222222
case EnumMayNotBeValueClassesID // errorNumber: 206
223223
case IllegalUnrollPlacementID // errorNumber: 207
224224
case ExtensionHasDefaultID // errorNumber: 208
225+
case FormatInterpolationErrorID // errorNumber: 209
225226

226227
def errorNumber = ordinal - 1
227228

compiler/src/dotty/tools/dotc/reporting/MessageKind.scala

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ enum MessageKind:
2323
case PotentialIssue
2424
case UnusedSymbol
2525
case Staging
26+
case Interpolation
2627

2728
/** Human readable message that will end up being shown to the user.
2829
* NOTE: This is only used in the situation where you have multiple words

compiler/src/dotty/tools/dotc/reporting/messages.scala

+5
Original file line numberDiff line numberDiff line change
@@ -3444,3 +3444,8 @@ extends DeclarationMsg(IllegalUnrollPlacementID):
34443444

34453445
def explain(using Context) = ""
34463446
end IllegalUnrollPlacement
3447+
3448+
class BadFormatInterpolation(errorText: String)(using Context) extends Message(FormatInterpolationErrorID):
3449+
def kind = MessageKind.Interpolation
3450+
def msg(using Context) = errorText
3451+
def explain(using Context) = ""

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

+11-4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import dotty.tools.dotc.core.Contexts.*
1010
import dotty.tools.dotc.core.Symbols.*
1111
import dotty.tools.dotc.core.Types.*
1212
import dotty.tools.dotc.core.Phases.typerPhase
13+
import dotty.tools.dotc.reporting.BadFormatInterpolation
1314
import dotty.tools.dotc.util.Spans.Span
1415
import dotty.tools.dotc.util.chaining.*
1516

@@ -296,10 +297,16 @@ class TypedFormatChecker(partsElems: List[Tree], parts: List[String], args: List
296297
val pos = partsElems(index).sourcePos
297298
val bgn = pos.span.start + offset
298299
val fin = if end < 0 then pos.span.end else pos.span.start + end
299-
pos.withSpan(Span(bgn, fin, bgn))
300+
pos.withSpan(Span(start = bgn, end = fin, point = bgn))
300301

301302
extension (r: report.type)
302-
def argError(message: String, index: Int): Unit = r.error(message, args(index).srcPos).tap(_ => reported = true)
303-
def partError(message: String, index: Int, offset: Int, end: Int = -1): Unit = r.error(message, partPosAt(index, offset, end)).tap(_ => reported = true)
304-
def partWarning(message: String, index: Int, offset: Int, end: Int = -1): Unit = r.warning(message, partPosAt(index, offset, end)).tap(_ => reported = true)
303+
def argError(message: String, index: Int): Unit =
304+
r.error(BadFormatInterpolation(message), args(index).srcPos)
305+
.tap(_ => reported = true)
306+
def partError(message: String, index: Int, offset: Int, end: Int = -1): Unit =
307+
r.error(BadFormatInterpolation(message), partPosAt(index, offset, end))
308+
.tap(_ => reported = true)
309+
def partWarning(message: String, index: Int, offset: Int, end: Int): Unit =
310+
r.warning(BadFormatInterpolation(message), partPosAt(index, offset, end))
311+
.tap(_ => reported = true)
305312
end TypedFormatChecker

tests/neg/f-interpolator-neg.check

+46-46
Original file line numberDiff line numberDiff line change
@@ -14,187 +14,187 @@
1414
7 | new StringContext("", "").f() // error
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
| too few arguments for interpolated string
17-
-- Warning: tests/neg/f-interpolator-neg.scala:11:9 --------------------------------------------------------------------
17+
-- [E209] Interpolation Warning: tests/neg/f-interpolator-neg.scala:11:9 -----------------------------------------------
1818
11 | f"$s%b" // warn only
1919
| ^
2020
| non-Boolean value formats as "true" for non-null references and boxed primitives, otherwise "false"
21-
-- Error: tests/neg/f-interpolator-neg.scala:12:7 ----------------------------------------------------------------------
21+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:12:7 -------------------------------------------------
2222
12 | f"$s%c" // error
2323
| ^
2424
| Found: (s : String), Required: Char, Byte, Short, Int
25-
-- Error: tests/neg/f-interpolator-neg.scala:13:7 ----------------------------------------------------------------------
25+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:13:7 -------------------------------------------------
2626
13 | f"$f%c" // error
2727
| ^
2828
| Found: (f : Double), Required: Char, Byte, Short, Int
29-
-- Error: tests/neg/f-interpolator-neg.scala:14:7 ----------------------------------------------------------------------
29+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:14:7 -------------------------------------------------
3030
14 | f"$s%x" // error
3131
| ^
3232
| Found: (s : String), Required: Int, Long, Byte, Short, BigInt
33-
-- Error: tests/neg/f-interpolator-neg.scala:15:7 ----------------------------------------------------------------------
33+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:15:7 -------------------------------------------------
3434
15 | f"$b%d" // error
3535
| ^
3636
| Found: (b : Boolean), Required: Int, Long, Byte, Short, BigInt
37-
-- Error: tests/neg/f-interpolator-neg.scala:16:7 ----------------------------------------------------------------------
37+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:16:7 -------------------------------------------------
3838
16 | f"$s%d" // error
3939
| ^
4040
| Found: (s : String), Required: Int, Long, Byte, Short, BigInt
41-
-- Error: tests/neg/f-interpolator-neg.scala:17:7 ----------------------------------------------------------------------
41+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:17:7 -------------------------------------------------
4242
17 | f"$f%o" // error
4343
| ^
4444
| Found: (f : Double), Required: Int, Long, Byte, Short, BigInt
45-
-- Error: tests/neg/f-interpolator-neg.scala:18:7 ----------------------------------------------------------------------
45+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:18:7 -------------------------------------------------
4646
18 | f"$s%e" // error
4747
| ^
4848
| Found: (s : String), Required: Double, Float, BigDecimal
49-
-- Error: tests/neg/f-interpolator-neg.scala:19:7 ----------------------------------------------------------------------
49+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:19:7 -------------------------------------------------
5050
19 | f"$b%f" // error
5151
| ^
5252
| Found: (b : Boolean), Required: Double, Float, BigDecimal
53-
-- Error: tests/neg/f-interpolator-neg.scala:20:9 ----------------------------------------------------------------------
53+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:20:9 -------------------------------------------------
5454
20 | f"$s%i" // error
5555
| ^
5656
| illegal conversion character 'i'
57-
-- Error: tests/neg/f-interpolator-neg.scala:24:9 ----------------------------------------------------------------------
57+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:24:9 -------------------------------------------------
5858
24 | f"$s%+ 0,(s" // error
5959
| ^^^^^
6060
| Illegal flag '+'
61-
-- Error: tests/neg/f-interpolator-neg.scala:25:9 ----------------------------------------------------------------------
61+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:25:9 -------------------------------------------------
6262
25 | f"$c%#+ 0,(c" // error
6363
| ^^^^^^
6464
| Only '-' allowed for c conversion
65-
-- Error: tests/neg/f-interpolator-neg.scala:26:9 ----------------------------------------------------------------------
65+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:26:9 -------------------------------------------------
6666
26 | f"$d%#d" // error
6767
| ^
6868
| # not allowed for d conversion
69-
-- Error: tests/neg/f-interpolator-neg.scala:27:9 ----------------------------------------------------------------------
69+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:27:9 -------------------------------------------------
7070
27 | f"$d%,x" // error
7171
| ^
7272
| ',' only allowed for d conversion of integral types
73-
-- Error: tests/neg/f-interpolator-neg.scala:28:9 ----------------------------------------------------------------------
73+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:28:9 -------------------------------------------------
7474
28 | f"$d%+ (x" // error
7575
| ^^^
7676
| only use '+' for BigInt conversions to o, x, X
77-
-- Error: tests/neg/f-interpolator-neg.scala:29:9 ----------------------------------------------------------------------
77+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:29:9 -------------------------------------------------
7878
29 | f"$f%,(a" // error
7979
| ^^
8080
| ',' not allowed for a, A
81-
-- Error: tests/neg/f-interpolator-neg.scala:30:9 ----------------------------------------------------------------------
81+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:30:9 -------------------------------------------------
8282
30 | f"$t%#+ 0,(tT" // error
8383
| ^^^^^^
8484
| Only '-' allowed for date/time conversions
85-
-- Error: tests/neg/f-interpolator-neg.scala:31:7 ----------------------------------------------------------------------
85+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:31:7 -------------------------------------------------
8686
31 | f"%-#+ 0,(n" // error
8787
| ^^^^^^^
8888
| flags not allowed
89-
-- Error: tests/neg/f-interpolator-neg.scala:32:7 ----------------------------------------------------------------------
89+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:32:7 -------------------------------------------------
9090
32 | f"%#+ 0,(%" // error
9191
| ^^^^^^
9292
| Illegal flag '#'
93-
-- Error: tests/neg/f-interpolator-neg.scala:36:9 ----------------------------------------------------------------------
93+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:36:9 -------------------------------------------------
9494
36 | f"$c%.2c" // error
9595
| ^^
9696
| precision not allowed
97-
-- Error: tests/neg/f-interpolator-neg.scala:37:9 ----------------------------------------------------------------------
97+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:37:9 -------------------------------------------------
9898
37 | f"$d%.2d" // error
9999
| ^^
100100
| precision not allowed
101-
-- Error: tests/neg/f-interpolator-neg.scala:38:7 ----------------------------------------------------------------------
101+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:38:7 -------------------------------------------------
102102
38 | f"%.2%" // error
103103
| ^^
104104
| precision not allowed
105-
-- Error: tests/neg/f-interpolator-neg.scala:39:7 ----------------------------------------------------------------------
105+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:39:7 -------------------------------------------------
106106
39 | f"%.2n" // error
107107
| ^^
108108
| precision not allowed
109-
-- Error: tests/neg/f-interpolator-neg.scala:40:9 ----------------------------------------------------------------------
109+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:40:9 -------------------------------------------------
110110
40 | f"$f%.2a" // error
111111
| ^^
112112
| precision not allowed
113-
-- Error: tests/neg/f-interpolator-neg.scala:41:9 ----------------------------------------------------------------------
113+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:41:9 -------------------------------------------------
114114
41 | f"$t%.2tT" // error
115115
| ^^
116116
| precision not allowed
117-
-- Error: tests/neg/f-interpolator-neg.scala:45:7 ----------------------------------------------------------------------
117+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:45:7 -------------------------------------------------
118118
45 | f"%<s" // error
119119
| ^
120120
| No last arg
121-
-- Error: tests/neg/f-interpolator-neg.scala:46:7 ----------------------------------------------------------------------
121+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:46:7 -------------------------------------------------
122122
46 | f"%<c" // error
123123
| ^
124124
| No last arg
125-
-- Error: tests/neg/f-interpolator-neg.scala:47:7 ----------------------------------------------------------------------
125+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:47:7 -------------------------------------------------
126126
47 | f"%<tT" // error
127127
| ^
128128
| No last arg
129-
-- Error: tests/neg/f-interpolator-neg.scala:48:21 ---------------------------------------------------------------------
129+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:48:21 ------------------------------------------------
130130
48 | f"${8}%d ${9}%d %3$$d" // error
131131
| ^
132132
| Argument index out of range
133-
-- Error: tests/neg/f-interpolator-neg.scala:49:20 ---------------------------------------------------------------------
133+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:49:20 ------------------------------------------------
134134
49 | f"${8}%d ${9}%d%0$$d" // error
135135
| ^
136136
| Argument index out of range
137-
-- Warning: tests/neg/f-interpolator-neg.scala:54:17 -------------------------------------------------------------------
137+
-- [E209] Interpolation Warning: tests/neg/f-interpolator-neg.scala:54:17 ----------------------------------------------
138138
54 | f"$s%s $s%s %1$$<s"
139139
| ^
140140
| Argument index ignored if '<' flag is present
141-
-- Error: tests/neg/f-interpolator-neg.scala:59:7 ----------------------------------------------------------------------
141+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:59:7 -------------------------------------------------
142142
59 | f"$s%#s" // error
143143
| ^
144144
| Found: (s : String), Required: java.util.Formattable
145-
-- Error: tests/neg/f-interpolator-neg.scala:63:10 ---------------------------------------------------------------------
145+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:63:10 ------------------------------------------------
146146
63 | f"$t%tG" // error
147147
| ^
148148
| 'G' doesn't seem to be a date or time conversion
149-
-- Error: tests/neg/f-interpolator-neg.scala:64:9 ----------------------------------------------------------------------
149+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:64:9 -------------------------------------------------
150150
64 | f"$t%t" // error
151151
| ^
152152
| Date/time conversion must have two characters
153-
-- Error: tests/neg/f-interpolator-neg.scala:65:8 ----------------------------------------------------------------------
153+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:65:8 -------------------------------------------------
154154
65 | f"$s%10.5" // error
155155
| ^^^^^
156156
| Missing conversion operator in '%10.5'; use %% for literal %, %n for newline
157-
-- Error: tests/neg/f-interpolator-neg.scala:69:29 ---------------------------------------------------------------------
157+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:69:29 ------------------------------------------------
158158
69 | f"${d}random-leading-junk%d" // error
159159
| ^^
160160
| conversions must follow a splice; use %% for literal %, %n for newline
161-
-- Error: tests/neg/f-interpolator-neg.scala:71:7 ----------------------------------------------------------------------
161+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:71:7 -------------------------------------------------
162162
71 | f"%1$$d" // error
163163
| ^
164164
| Argument index out of range
165-
-- Error: tests/neg/f-interpolator-neg.scala:72:23 ---------------------------------------------------------------------
165+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:72:23 ------------------------------------------------
166166
72 | f"blablablabla %% %.2d" // error
167167
| ^^
168168
| precision not allowed
169-
-- Error: tests/neg/f-interpolator-neg.scala:73:19 ---------------------------------------------------------------------
169+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:73:19 ------------------------------------------------
170170
73 | f"blablablabla %.2b %%" // error
171171
| ^^^^
172172
| conversions must follow a splice; use %% for literal %, %n for newline
173-
-- Error: tests/neg/f-interpolator-neg.scala:75:17 ---------------------------------------------------------------------
173+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:75:17 ------------------------------------------------
174174
75 | f"ana${3}%.2f%2${true}%bb" // error
175175
| ^^
176176
| Missing conversion operator in '%2'; use %% for literal %, %n for newline
177-
-- Error: tests/neg/f-interpolator-neg.scala:78:7 ----------------------------------------------------------------------
177+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:78:7 -------------------------------------------------
178178
78 | f"b%c.%2ii%iin" // error // error // error
179179
| ^^
180180
| conversions must follow a splice; use %% for literal %, %n for newline
181-
-- Error: tests/neg/f-interpolator-neg.scala:78:12 ---------------------------------------------------------------------
181+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:78:12 ------------------------------------------------
182182
78 | f"b%c.%2ii%iin" // error // error // error
183183
| ^
184184
| illegal conversion character 'i'
185-
-- Error: tests/neg/f-interpolator-neg.scala:78:15 ---------------------------------------------------------------------
185+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:78:15 ------------------------------------------------
186186
78 | f"b%c.%2ii%iin" // error // error // error
187187
| ^
188188
| illegal conversion character 'i'
189-
-- Error: tests/neg/f-interpolator-neg.scala:79:10 ---------------------------------------------------------------------
189+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:79:10 ------------------------------------------------
190190
79 | f"b}22%2.c<{%{" // error // error
191191
| ^^
192192
| Missing conversion operator in '%2'; use %% for literal %, %n for newline
193-
-- Error: tests/neg/f-interpolator-neg.scala:79:16 ---------------------------------------------------------------------
193+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:79:16 ------------------------------------------------
194194
79 | f"b}22%2.c<{%{" // error // error
195195
| ^
196196
| Missing conversion operator in '%'; use %% for literal %, %n for newline
197-
-- Error: tests/neg/f-interpolator-neg.scala:80:19 ---------------------------------------------------------------------
197+
-- [E209] Interpolation Error: tests/neg/f-interpolator-neg.scala:80:19 ------------------------------------------------
198198
80 | f"%%bci.2${'i'}%..2c2" // error
199199
| ^
200200
| Missing conversion operator in '%'; use %% for literal %, %n for newline

tests/neg/f-interpolator-tests.check

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
-- Error: tests/neg/f-interpolator-tests.scala:4:19 --------------------------------------------------------------------
1+
-- [E209] Interpolation Error: tests/neg/f-interpolator-tests.scala:4:19 -----------------------------------------------
22
4 | def `um uh` = f"$s%d" // error
33
| ^
44
| Found: (T.this.s : String), Required: Int, Long, Byte, Short, BigInt
5-
-- Error: tests/neg/f-interpolator-tests.scala:10:10 -------------------------------------------------------------------
5+
-- [E209] Interpolation Error: tests/neg/f-interpolator-tests.scala:10:10 ----------------------------------------------
66
10 | f"x % y = ${x % y}%d" // error: illegal conversion character 'y'
77
| ^
88
| illegal conversion character 'y'

tests/neg/fEscapes.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- Error: tests/neg/fEscapes.scala:5:18 --------------------------------------------------------------------------------
1+
-- [E209] Interpolation Error: tests/neg/fEscapes.scala:5:18 -----------------------------------------------------------
22
5 | val fEscape = f"\u$octal%04x" // error
33
| ^^
44
| invalid unicode escape at index 1 of \u

0 commit comments

Comments
 (0)