@@ -7,34 +7,6 @@ import dotty.tools.dotc.core.Contexts.*
77
88object FormatInterpolatorTransform :
99
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-
3810 /** For f"${arg}%xpart", check format conversions and return (format, args)
3911 * suitable for String.format(format, args).
4012 */
@@ -50,67 +22,18 @@ object FormatInterpolatorTransform:
5022 case _ =>
5123 report.error(" Expected statically known argument list" , args0.srcPos)
5224 (Nil , EmptyTree )
53- given reporter : InterpolationReporter = PartsReporter (fun, args0, partsExpr, args)
5425
5526 def literally (s : String ) = Literal (Constant (s))
56- inline val skip = false
5727 if parts.lengthIs != args.length + 1 then
58- reporter.strCtxError {
28+ val badParts =
5929 if parts.isEmpty then " there are no parts"
6030 else s " too ${if parts.lengthIs > args.length + 1 then " few" else " many" } arguments for interpolated string "
61- }
31+ report.error(badParts, fun.srcPos)
6232 (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)
6633 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))
7338 end checked
7439end 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