Skip to content

Commit 033aa34

Browse files
committed
Optimize type related logic
1 parent ef19bcd commit 033aa34

File tree

2 files changed

+24
-18
lines changed

2 files changed

+24
-18
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,13 @@ class Definitions {
687687
@tu lazy val SerializableType: TypeRef = JavaSerializableClass.typeRef
688688
def SerializableClass(using Context): ClassSymbol = SerializableType.symbol.asClass
689689

690-
@tu lazy val JavaEnumClass: ClassSymbol = {
690+
@tu lazy val JavaBigIntegerClass: ClassSymbol = requiredClass("java.math.BigInteger")
691+
@tu lazy val JavaBigDecimalClass: ClassSymbol = requiredClass("java.math.BigDecimal")
692+
@tu lazy val JavaCalendarClass: ClassSymbol = requiredClass("java.util.Calendar")
693+
@tu lazy val JavaDateClass: ClassSymbol = requiredClass("java.util.Date")
694+
@tu lazy val JavaFormattableClass: ClassSymbol = requiredClass("java.util.Formattable")
695+
696+
@tu lazy val JavaEnumClass: ClassSymbol = {
691697
val cls = requiredClass("java.lang.Enum")
692698
// jl.Enum has a single constructor protected(name: String, ordinal: Int).
693699
// We remove the arguments from the primary constructor, and enter

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

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,8 @@ object StringContextChecker {
313313
* @return reports a type mismatch error if the actual type is not a subtype of any of the possibilities,
314314
* nothing otherwise
315315
*/
316-
def checkSubtype(actualType : Type, expectedType : String, argIndex : Int, possibilities : Type*) = {
317-
if (possibilities.find(actualType <:< _).isEmpty)
316+
def checkSubtype(actualType: Type, expectedType: String, argIndex: Int, possibilities: List[Type]) = {
317+
if !possibilities.exists(actualType <:< _) then
318318
reporter.argError("type mismatch;\n found : " + actualType.widen.show.stripPrefix("scala.Predef.").stripPrefix("java.lang.").stripPrefix("scala.") + "\n required: " + expectedType, argIndex)
319319
}
320320

@@ -584,31 +584,31 @@ object StringContextChecker {
584584
* nothing otherwise
585585
*/
586586
def checkTypeWithArgs(argument : (Type, Int), conversionChar : Char, partIndex : Int, flags : List[(Char, Int)]) = {
587-
val booleans = List(defn.BooleanType, defn.NullType)
588-
val dates = List(defn.LongType, requiredClass("java.util.Calendar").typeRef, requiredClass("java.util.Date").typeRef)
589-
val floatingPoints = List(defn.DoubleType, defn.FloatType, requiredClass("java.math.BigDecimal").typeRef)
590-
val integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, requiredClass("java.math.BigInteger").typeRef)
591-
val character = List(defn.CharType, defn.ByteType, defn.ShortType, defn.IntType)
587+
def booleans = List(defn.BooleanType, defn.NullType)
588+
def dates = List(defn.LongType, defn.JavaCalendarClass.typeRef, defn.JavaDateClass.typeRef)
589+
def floatingPoints = List(defn.DoubleType, defn.FloatType, defn.JavaBigDecimalClass.typeRef)
590+
def integral = List(defn.IntType, defn.LongType, defn.ShortType, defn.ByteType, defn.JavaBigIntegerClass.typeRef)
591+
def character = List(defn.CharType, defn.ByteType, defn.ShortType, defn.IntType)
592592

593593
val (argType, argIndex) = argument
594594
conversionChar match {
595-
case 'c' | 'C' => checkSubtype(argType, "Char", argIndex, character : _*)
595+
case 'c' | 'C' => checkSubtype(argType, "Char", argIndex, character)
596596
case 'd' | 'o' | 'x' | 'X' => {
597-
checkSubtype(argType, "Int", argIndex, integral : _*)
597+
checkSubtype(argType, "Int", argIndex, integral)
598598
if (conversionChar != 'd') {
599-
val notAllowedFlagOnCondition = List(('+', !(argType <:< requiredClass("java.math.BigInteger").typeRef), "only use '+' for BigInt conversions to o, x, X"),
600-
(' ', !(argType <:< requiredClass("java.math.BigInteger").typeRef), "only use ' ' for BigInt conversions to o, x, X"),
601-
('(', !(argType <:< requiredClass("java.math.BigInteger").typeRef), "only use '(' for BigInt conversions to o, x, X"),
599+
val notAllowedFlagOnCondition = List(('+', !(argType <:< defn.JavaBigIntegerClass.typeRef), "only use '+' for BigInt conversions to o, x, X"),
600+
(' ', !(argType <:< defn.JavaBigIntegerClass.typeRef), "only use ' ' for BigInt conversions to o, x, X"),
601+
('(', !(argType <:< defn.JavaBigIntegerClass.typeRef), "only use '(' for BigInt conversions to o, x, X"),
602602
(',', true, "',' only allowed for d conversion of integral types"))
603603
checkFlags(partIndex, flags, notAllowedFlagOnCondition : _*)
604604
}
605605
}
606-
case 'e' | 'E' |'f' | 'g' | 'G' | 'a' | 'A' => checkSubtype(argType, "Double", argIndex, floatingPoints : _*)
607-
case 't' | 'T' => checkSubtype(argType, "Date", argIndex, dates : _*)
608-
case 'b' | 'B' => checkSubtype(argType, "Boolean", argIndex, booleans : _*)
606+
case 'e' | 'E' |'f' | 'g' | 'G' | 'a' | 'A' => checkSubtype(argType, "Double", argIndex, floatingPoints)
607+
case 't' | 'T' => checkSubtype(argType, "Date", argIndex, dates)
608+
case 'b' | 'B' => checkSubtype(argType, "Boolean", argIndex, booleans)
609609
case 'h' | 'H' | 'S' | 's' =>
610-
if !(argType <:< requiredClass("java.util.Formattable").typeRef) then
611-
for {flag <- flags ; if (flag._1 == '#')}
610+
if !(argType <:< defn.JavaFormattableClass.typeRef) then
611+
for flag <- flags; if flag._1 == '#' do
612612
reporter.argError("type mismatch;\n found : " + argType.widen.show.stripPrefix("scala.Predef.").stripPrefix("java.lang.").stripPrefix("scala.") + "\n required: java.util.Formattable", argIndex)
613613
case 'n' | '%' =>
614614
case illegal =>

0 commit comments

Comments
 (0)