@@ -7,34 +7,6 @@ import dotty.tools.dotc.core.Contexts.*
7
7
8
8
object FormatInterpolatorTransform :
9
9
10
- class PartsReporter (fun : Tree , args0 : Tree , parts : List [Tree ], args : List [Tree ])(using Context ) extends InterpolationReporter :
11
- private var reported = false
12
- private var oldReported = false
13
- private def partPosAt (index : Int , offset : Int ) =
14
- val pos = parts(index).sourcePos
15
- pos.withSpan(pos.span.shift(offset))
16
- def partError (message : String , index : Int , offset : Int ): Unit =
17
- reported = true
18
- report.error(message, partPosAt(index, offset))
19
- def partWarning (message : String , index : Int , offset : Int ): Unit =
20
- reported = true
21
- report.warning(message, partPosAt(index, offset))
22
- def argError (message : String , index : Int ): Unit =
23
- reported = true
24
- report.error(message, args(index).srcPos)
25
- def strCtxError (message : String ): Unit =
26
- reported = true
27
- report.error(message, fun.srcPos)
28
- def argsError (message : String ): Unit =
29
- reported = true
30
- report.error(message, args0.srcPos)
31
- def hasReported : Boolean = reported
32
- def resetReported (): Unit =
33
- oldReported = reported
34
- reported = false
35
- def restoreReported (): Unit = reported = oldReported
36
- end PartsReporter
37
-
38
10
/** For f"${arg}%xpart", check format conversions and return (format, args)
39
11
* suitable for String.format(format, args).
40
12
*/
@@ -50,67 +22,18 @@ object FormatInterpolatorTransform:
50
22
case _ =>
51
23
report.error(" Expected statically known argument list" , args0.srcPos)
52
24
(Nil , EmptyTree )
53
- given reporter : InterpolationReporter = PartsReporter (fun, args0, partsExpr, args)
54
25
55
26
def literally (s : String ) = Literal (Constant (s))
56
- inline val skip = false
57
27
if parts.lengthIs != args.length + 1 then
58
- reporter.strCtxError {
28
+ val badParts =
59
29
if parts.isEmpty then " there are no parts"
60
30
else s " too ${if parts.lengthIs > args.length + 1 then " few" else " many" } arguments for interpolated string "
61
- }
31
+ report.error(badParts, fun.srcPos)
62
32
(literally(" " ), args0)
63
- else if skip then
64
- val checked = parts.head :: parts.tail.map(p => if p.startsWith(" %" ) then p else " %s" + p)
65
- (literally(checked.mkString), args0)
66
33
else
67
- val checker = TypedFormatChecker (args)
68
- val (checked, cvs) = checker.checked(parts)
69
- if reporter.hasReported then (literally(parts.mkString), args0)
70
- else
71
- assert(checker.argc == checker.actuals.size, s " Expected ${checker.argc}, actuals size is ${checker.actuals.size} for [ ${parts.mkString(" , " )}] " )
72
- (literally(checked.mkString), SeqLiteral (checker.actuals.toList, elemtpt))
34
+ val checker = TypedFormatChecker (partsExpr, parts, args)
35
+ val (format, formatArgs) = checker.checked
36
+ if format.isEmpty then (literally(parts.mkString), args0)
37
+ else (literally(format.mkString), SeqLiteral (formatArgs.toList, elemtpt))
73
38
end checked
74
39
end FormatInterpolatorTransform
75
-
76
- /** This trait defines a tool to report errors/warnings that do not depend on Position. */
77
- trait InterpolationReporter :
78
-
79
- /** Reports error/warning of size 1 linked with a part of the StringContext.
80
- *
81
- * @param message the message to report as error/warning
82
- * @param index the index of the part inside the list of parts of the StringContext
83
- * @param offset the index in the part String where the error is
84
- * @return an error/warning depending on the function
85
- */
86
- def partError (message : String , index : Int , offset : Int ): Unit
87
- def partWarning (message : String , index : Int , offset : Int ): Unit
88
-
89
- /** Reports error linked with an argument to format.
90
- *
91
- * @param message the message to report as error/warning
92
- * @param index the index of the argument inside the list of arguments of the format function
93
- * @return an error depending on the function
94
- */
95
- def argError (message : String , index : Int ): Unit
96
-
97
- /** Reports error linked with the list of arguments or the StringContext.
98
- *
99
- * @param message the message to report in the error
100
- * @return an error
101
- */
102
- def strCtxError (message : String ): Unit
103
- def argsError (message : String ): Unit
104
-
105
- /** Claims whether an error or a warning has been reported
106
- *
107
- * @return true if an error/warning has been reported, false
108
- */
109
- def hasReported : Boolean
110
-
111
- /** Stores the old value of the reported and reset it to false */
112
- def resetReported (): Unit
113
-
114
- /** Restores the value of the reported boolean that has been reset */
115
- def restoreReported (): Unit
116
- end InterpolationReporter
0 commit comments