Skip to content

Commit 5e287cd

Browse files
committed
Address reviewer feedback
* Use NoType instead of Option[Type] * Move a Set to a val to avoid recomputing it on every call
1 parent c355424 commit 5e287cd

File tree

3 files changed

+21
-12
lines changed

3 files changed

+21
-12
lines changed

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ class Definitions {
234234
@tu lazy val CompiletimeTesting_ErrorKind_Parser: Symbol = CompiletimeTesting_ErrorKind.requiredMethod("Parser")
235235
@tu lazy val CompiletimeTesting_ErrorKind_Typer: Symbol = CompiletimeTesting_ErrorKind.requiredMethod("Typer")
236236
@tu lazy val CompiletimeOpsPackageObject: Symbol = ctx.requiredModule("scala.compiletime.ops.package")
237+
@tu lazy val CompiletimeOpsPackageObjectInt: Symbol = ctx.requiredModule("scala.compiletime.ops.package.int")
238+
@tu lazy val CompiletimeOpsPackageObjectString: Symbol = ctx.requiredModule("scala.compiletime.ops.package.string")
237239

238240
/** The `scalaShadowing` package is used to safely modify classes and
239241
* objects in scala so that they can be used from dotty. They will
@@ -899,15 +901,17 @@ class Definitions {
899901
final def isCompiletime_S(sym: Symbol)(implicit ctx: Context): Boolean =
900902
sym.name == tpnme.S && sym.owner == CompiletimePackageObject.moduleClass
901903

904+
private val compiletimePackageTypes: Set[Name] = Set(
905+
tpnme.Equals, tpnme.NotEquals,
906+
tpnme.Plus, tpnme.Minus, tpnme.Times, tpnme.Div, tpnme.Mod,
907+
tpnme.Lt, tpnme.Gt, tpnme.Ge, tpnme.Le,
908+
tpnme.Abs, tpnme.Negate, tpnme.Min, tpnme.Max, tpnme.ToString,
909+
tpnme.Not, tpnme.Xor, tpnme.And, tpnme.Or
910+
)
911+
902912
final def isCompiletimeAppliedType(sym: Symbol)(implicit ctx: Context): Boolean = {
903913
def isOpsPackageObjectAppliedType: Boolean =
904-
sym.owner == CompiletimeOpsPackageObject.moduleClass && Set(
905-
tpnme.Equals, tpnme.NotEquals,
906-
tpnme.Plus, tpnme.Minus, tpnme.Times, tpnme.Div, tpnme.Mod,
907-
tpnme.Lt, tpnme.Gt, tpnme.Ge, tpnme.Le,
908-
tpnme.Abs, tpnme.Negate, tpnme.Min, tpnme.Max, tpnme.ToString,
909-
tpnme.Not, tpnme.Xor, tpnme.And, tpnme.Or
910-
).contains(sym.name)
914+
sym.owner == CompiletimeOpsPackageObject.moduleClass && compiletimePackageTypes.contains(sym.name)
911915

912916
sym.isType && (isCompiletime_S(sym) || isOpsPackageObjectAppliedType)
913917
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,10 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w
10431043
*/
10441044
def compareCompiletimeAppliedType(tp: AppliedType, other: Type, fromBelow: Boolean): Boolean = {
10451045
if (defn.isCompiletime_S(tp.tycon.typeSymbol)) compareS(tp, other, fromBelow)
1046-
else tp.tryCompiletimeConstantFold.exists(folded => if (fromBelow) recur(other, folded) else recur(folded, other))
1046+
else {
1047+
val folded = tp.tryCompiletimeConstantFold
1048+
if (fromBelow) recur(other, folded) else recur(folded, other)
1049+
}
10471050
}
10481051

10491052
/** Like tp1 <:< tp2, but returns false immediately if we know that

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3596,13 +3596,13 @@ object Types {
35963596
NoType
35973597
}
35983598

3599-
tryCompiletimeConstantFold.getOrElse(tryMatchAlias)
3599+
tryCompiletimeConstantFold.orElse(tryMatchAlias)
36003600

36013601
case _ =>
36023602
NoType
36033603
}
36043604

3605-
def tryCompiletimeConstantFold(implicit ctx: Context): Option[Type] = tycon match {
3605+
def tryCompiletimeConstantFold(implicit ctx: Context): Type = tycon match {
36063606
case tycon: TypeRef if defn.isCompiletimeAppliedType(tycon.symbol) =>
36073607
def constValue(tp: Type): Option[Any] = tp match {
36083608
case ConstantType(Constant(n)) => Some(n)
@@ -3631,7 +3631,7 @@ object Types {
36313631
} yield ConstantType(Constant(op(a, b)))
36323632

36333633
trace(i"compiletime constant fold $this", typr, show = true) {
3634-
if (args.length == 1) tycon.symbol.name match {
3634+
val constantType = if (args.length == 1) tycon.symbol.name match {
36353635
case tpnme.S => constantFold1(natValue, _ + 1)
36363636
case tpnme.Abs => constantFold1(intValue, _.abs)
36373637
case tpnme.Negate => constantFold1(intValue, x => -x)
@@ -3663,9 +3663,11 @@ object Types {
36633663
case tpnme.Xor => constantFold2(boolValue, _ ^ _)
36643664
case _ => None
36653665
} else None
3666+
3667+
constantType.getOrElse(NoType)
36663668
}
36673669

3668-
case _ => None
3670+
case _ => NoType
36693671
}
36703672

36713673
def lowerBound(implicit ctx: Context): Type = tycon.stripTypeVar match {

0 commit comments

Comments
 (0)