Skip to content

Commit db2b39d

Browse files
author
Sara Alemanno
committed
Add flags check for specials
1 parent 0f1c9e1 commit db2b39d

File tree

4 files changed

+15
-5
lines changed

4 files changed

+15
-5
lines changed

library/src-3.x/dotty/internal/StringContextMacro.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -544,12 +544,18 @@ object StringContextMacro {
544544
* @return reports an error if precision or width is specified for '%' or
545545
* if precision is specified for end of line
546546
*/
547-
def checkSpecials(partIndex : Int, conversionChar : Char, hasPrecision : Boolean, precision : Int, hasWidth : Boolean, width : Int) = conversionChar match {
547+
def checkSpecials(partIndex : Int, conversionChar : Char, hasPrecision : Boolean, precision : Int, hasWidth : Boolean, width : Int, flags : List[(Char, Int)]) = conversionChar match {
548548
case 'n' => {
549549
checkNotAllowedParameter(hasPrecision, partIndex, precision, "precision")
550550
checkNotAllowedParameter(hasWidth, partIndex, width, "width")
551+
val notAllowedFlagOnCondition = for (flag <- List('-', '#', '+', ' ', '0', ',', '(')) yield (flag, true, "flags not allowed")
552+
checkUniqueFlags(partIndex, flags, notAllowedFlagOnCondition : _*)
553+
}
554+
case '%' => {
555+
checkNotAllowedParameter(hasPrecision, partIndex, precision, "precision")
556+
val notAllowedFlagOnCondition = for (flag <- List('#', '+', ' ', '0', ',', '(')) yield (flag, true, "Illegal flag '" + flag + "'")
557+
checkFlags(partIndex, flags, notAllowedFlagOnCondition : _*)
551558
}
552-
case '%' => checkNotAllowedParameter(hasPrecision, partIndex, precision, "precision")
553559
case _ => // OK
554560
}
555561

@@ -588,7 +594,7 @@ object StringContextMacro {
588594
case 'e' | 'E' |'f' | 'g' | 'G' | 'a' | 'A' => checkFloatingPointConversion(partIndex, conversionChar, flags, hasPrecision, precision)
589595
case 't' | 'T' => checkTimeConversion(partIndex, part, conversion, flags, hasPrecision, precision)
590596
case 'b' | 'B' | 'h' | 'H' | 'S' | 's' => checkGeneralConversion(partIndex, argType, conversionChar, flags)
591-
case 'n' | '%' => checkSpecials(partIndex, conversionChar, hasPrecision, precision, hasWidth, width)
597+
case 'n' | '%' => checkSpecials(partIndex, conversionChar, hasPrecision, precision, hasWidth, width, flags)
592598
case illegal => reporter.partError("illegal conversion character '" + illegal + "'", partIndex, conversion)
593599
}
594600

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ object Test {
5454
(true, 0, 1, 3, "only use '(' for BigInt conversions to o, x, X")))
5555
assertEquals(foo"$f%,(a", List((true, 0, 1, 1, "',' not allowed for a, A"), (true, 0, 1, 2, "'(' not allowed for a, A")))
5656
assertEquals(foo"$t%#+ 0,(tT", List((true, 0, 1, 1, "Only '-' allowed for date/time conversions")))
57+
assertEquals(foo"%-#+ 0,(n", List((true, 0, 0, 1, "flags not allowed")))
58+
assertEquals(foo"%#+ 0,(%", List((true, 0, 0, 1, "Illegal flag '#'"), (true, 0, 0, 2, "Illegal flag '+'"),
59+
(true, 0, 0, 3, "Illegal flag ' '"), (true, 0, 0, 4, "Illegal flag '0'"), (true, 0, 0, 5, "Illegal flag ','"), (true, 0, 0, 6, "Illegal flag '('")))
5760
}
5861

5962
def badPrecisions(c : Char, d : Int, f : Double, t : java.util.Date) = {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ am
7878
08/31/82
7979
1982-08-31
8080
the percentage is 100 %
81-
we have a line separator now %n and now, we are on the next line
81+
we have a line separator now
82+
and now, we are on the next line
8283
a b b

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ object Test {
187187
def specificLiteralsTests = {
188188
def percentArgsTest = println(f"the percentage is 100 %%")
189189

190-
def lineSeparatorArgs = println(f"we have a line separator now %%n and now, we are on the next line")
190+
def lineSeparatorArgs = println(f"we have a line separator now %nand now, we are on the next line")
191191

192192
def nothingTest = println(f"we have nothing")
193193

0 commit comments

Comments
 (0)