Skip to content

Add/simplify primitives #598

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ class Definitions {

// ----- primitive value class machinery ------------------------------------------

lazy val ScalaNumericValueClasses: collection.Set[Symbol] = Set(
lazy val ScalaNumericValueClassList = List(
ByteClass,
ShortClass,
CharClass,
Expand All @@ -548,6 +548,7 @@ class Definitions {
FloatClass,
DoubleClass)

lazy val ScalaNumericValueClasses: collection.Set[Symbol] = ScalaNumericValueClassList.toSet
lazy val ScalaValueClasses: collection.Set[Symbol] = ScalaNumericValueClasses + UnitClass + BooleanClass

lazy val ScalaBoxedClasses = ScalaValueClasses map boxedClass
Expand Down
3 changes: 3 additions & 0 deletions src/dotty/tools/dotc/core/SymDenotations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,9 @@ object SymDenotations {
/** Is symbol a primitive value class? */
def isPrimitiveValueClass(implicit ctx: Context) = defn.ScalaValueClasses contains symbol

/** Is symbol a primitive numeric value class? */
def isNumericValueClass(implicit ctx: Context) = defn.ScalaNumericValueClasses contains symbol
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neither name nor comment explains what this method does. Comment says nothing about class being numeric, and name of the method does not say anything about class being primitive. Methods tests for both of those.


/** Is symbol a phantom class for which no runtime representation exists? */
def isPhantomClass(implicit ctx: Context) = defn.PhantomClasses contains symbol

Expand Down
51 changes: 50 additions & 1 deletion src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ trait Applications extends Compatibility { self: Typer =>
*/
protected def makeVarArg(n: Int, elemFormal: Type): Unit

/** If all `args` have primitive numeric types, make sure it's the same one */
protected def harmonizeArgs(args: List[TypedArg]): List[TypedArg]

/** Signal failure with given message at position of given argument */
protected def fail(msg: => String, arg: Arg): Unit

Expand Down Expand Up @@ -334,7 +337,14 @@ trait Applications extends Compatibility { self: Typer =>
addTyped(arg, formal)
case _ =>
val elemFormal = formal.widenExpr.argTypesLo.head
args foreach (addTyped(_, elemFormal))
val origConstraint = ctx.typerState.constraint
var typedArgs = args.map(typedArg(_, elemFormal))
val harmonizedArgs = harmonizeArgs(typedArgs)
if (harmonizedArgs ne typedArgs) {
ctx.typerState.constraint = origConstraint
typedArgs = harmonizedArgs
}
typedArgs.foreach(addArg(_, elemFormal))
makeVarArg(args.length, elemFormal)
}
else args match {
Expand Down Expand Up @@ -389,6 +399,7 @@ trait Applications extends Compatibility { self: Typer =>
def argType(arg: Tree, formal: Type): Type = normalize(arg.tpe, formal)
def treeToArg(arg: Tree): Tree = arg
def isVarArg(arg: Tree): Boolean = tpd.isWildcardStarArg(arg)
def harmonizeArgs(args: List[Tree]) = harmonize(args)
}

/** Subclass of Application for applicability tests with type arguments and value
Expand All @@ -405,6 +416,7 @@ trait Applications extends Compatibility { self: Typer =>
def argType(arg: Type, formal: Type): Type = arg
def treeToArg(arg: Tree): Type = arg.tpe
def isVarArg(arg: Type): Boolean = arg.isRepeatedParam
def harmonizeArgs(args: List[Type]) = harmonizeTypes(args)
}

/** Subclass of Application for type checking an Apply node, where
Expand All @@ -430,6 +442,8 @@ trait Applications extends Compatibility { self: Typer =>
typedArgBuf += seqToRepeated(seqLit)
}

def harmonizeArgs(args: List[TypedArg]) = harmonize(args)

override def appPos = app.pos

def fail(msg: => String, arg: Trees.Tree[T]) = {
Expand Down Expand Up @@ -1024,6 +1038,41 @@ trait Applications extends Compatibility { self: Typer =>
result
}
}

private def harmonizeWith[T <: AnyRef](ts: List[T])(tpe: T => Type, adapt: (T, Type) => T)(implicit ctx: Context): List[T] = {
def numericClasses(ts: List[T], acc: Set[Symbol]): Set[Symbol] = ts match {
case t :: ts1 =>
val sym = tpe(t).widen.classSymbol
if (sym.isNumericValueClass) numericClasses(ts1, acc + sym)
else Set()
case Nil =>
acc
}
val clss = numericClasses(ts, Set())
if (clss.size > 1) {
val lub = defn.ScalaNumericValueClassList.find(lubCls =>
clss.forall(defn.isValueSubClass(_, lubCls))).get.typeRef
ts.mapConserve(adapt(_, lub))
}
else ts
}

/** If `trees` all have numeric value types, and they do not have all the same type,
* pick a common numeric supertype and convert all trees to this type.
*/
def harmonize(trees: List[Tree])(implicit ctx: Context): List[Tree] = {
def adapt(tree: Tree, pt: Type): Tree = tree match {
case cdef: CaseDef => tpd.cpy.CaseDef(cdef)(body = adapt(cdef.body, pt))
case _ => adaptInterpolated(tree, pt, tree)
}
harmonizeWith(trees)(_.tpe, adapt)
}

/** If all `types` are numeric value types, and they are not all the same type,
* pick a common numeric supertype and return it instead of every original type.
*/
def harmonizeTypes(tpes: List[Type])(implicit ctx: Context): List[Type] =
harmonizeWith(tpes)(identity, (tp, pt) => pt)
}

/*
Expand Down
10 changes: 7 additions & 3 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
val cond1 = typed(tree.cond, defn.BooleanType)
val thenp1 = typed(tree.thenp, pt)
val elsep1 = typed(tree.elsep orElse untpd.unitLiteral withPos tree.pos, pt)
assignType(cpy.If(tree)(cond1, thenp1, elsep1), thenp1, elsep1)
val thenp2 :: elsep2 :: Nil = harmonize(thenp1 :: elsep1 :: Nil)
assignType(cpy.If(tree)(cond1, thenp2, elsep2), thenp2, elsep2)
}

def typedFunction(tree: untpd.Function, pt: Type)(implicit ctx: Context) = track("typedFunction") {
Expand Down Expand Up @@ -629,7 +630,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
fullyDefinedType(sel1.tpe, "pattern selector", tree.pos))

val cases1 = typedCases(tree.cases, selType, pt)
assignType(cpy.Match(tree)(sel1, cases1), cases1)
val cases2 = harmonize(cases1).asInstanceOf[List[CaseDef]]
assignType(cpy.Match(tree)(sel1, cases2), cases2)
}
}

Expand Down Expand Up @@ -737,7 +739,9 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
val expr1 = typed(tree.expr, pt)
val cases1 = typedCases(tree.cases, defn.ThrowableType, pt)
val finalizer1 = typed(tree.finalizer, defn.UnitType)
assignType(cpy.Try(tree)(expr1, cases1, finalizer1), expr1, cases1)
val expr2 :: cases2x = harmonize(expr1 :: cases1)
val cases2 = cases2x.asInstanceOf[List[CaseDef]]
assignType(cpy.Try(tree)(expr2, cases2, finalizer1), expr2, cases2)
}

def typedThrow(tree: untpd.Throw)(implicit ctx: Context): Tree = track("typedThrow") {
Expand Down
28 changes: 28 additions & 0 deletions tests/pos/harmonize.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
object Test {

def main(args: Array[String]) = {
val x = true
val n = 1
val y = if (x) 'A' else n
val z: Int = y

val yy = n match {
case 1 => 'A'
case 2 => n
case 3 => 1.0
}
val zz: Double = yy

val a = try {
'A'
} catch {
case ex: Exception => n
case ex: Error => 3L
}
val b: Long = a

val xs = List(1.0, n, 'c')
val ys: List[Double] = xs
}

}