Skip to content

fix #1354: improve type test of union types #1415

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 1 commit into from
Jul 27, 2016
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
31 changes: 23 additions & 8 deletions src/dotty/tools/dotc/transform/TypeTestsCasts.scala
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package dotty.tools.dotc
package transform

import TreeTransforms._
import core.Denotations._
import core.SymDenotations._
import core.Contexts._
import core.Symbols._
import core.Types._
import core.Constants._
import core.StdNames._
import core.TypeErasure.isUnboundedGeneric
import typer.ErrorReporting._
import ast.Trees._
import Erasure.Boxing._
import core.TypeErasure._
Expand Down Expand Up @@ -92,14 +88,33 @@ trait TypeTestsCasts {
unbox(qual.ensureConforms(defn.ObjectType), argType)
else if (isDerivedValueClass(argCls)) {
qual // adaptToType in Erasure will do the necessary type adaptation
} else
}
else
derivedTree(qual, defn.Any_asInstanceOf, argType)
}
def erasedArg = erasure(tree.args.head.tpe)

/** Transform isInstanceOf OrType
*
* expr.isInstanceOf[A | B] ~~> expr.isInstanceOf[A] | expr.isInstanceOf[B]
*
* The transform happens before erasure of `argType`, thus cannot be merged
* with `transformIsInstanceOf`, which depends on erased type of `argType`.
*/
def transformOrTypeTest(qual: Tree, argType: Type): Tree = argType match {
case OrType(tp1, tp2) =>
evalOnce(qual) { fun =>
transformOrTypeTest(fun, tp1)
.select(nme.OR)
.appliedTo(transformOrTypeTest(fun, tp2))
}
case _ =>
transformIsInstanceOf(qual, erasure(argType))
}

if (sym eq defn.Any_isInstanceOf)
transformIsInstanceOf(qual, erasedArg)
transformOrTypeTest(qual, tree.args.head.tpe)
else if (sym eq defn.Any_asInstanceOf)
transformAsInstanceOf(erasedArg)
transformAsInstanceOf(erasure(tree.args.head.tpe))
else tree

case _ =>
Expand Down
6 changes: 6 additions & 0 deletions tests/run/i1354.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
0
false
5
1
true
true
27 changes: 27 additions & 0 deletions tests/run/i1354.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
object Test {
def foo(a: Int | Double) = a match {
case a: (Float | Boolean) => 1
case _ => 0
}

def typeTest(a: Int | Double) = a.isInstanceOf[Float | Boolean] // false

def typeCast(a: Int | Double) = a.asInstanceOf[Float | Boolean] // no error

def main(args: Array[String]): Unit = {
println(foo(4))

println(typeTest(4))

println(typeCast(5))

Boolean.box(true) match {
case a: (Float | Boolean) => println(1)
case _ => println(0)
}

println(Boolean.box(true).isInstanceOf[Float | Boolean])

println(Boolean.box(true).asInstanceOf[Float | Boolean])
}
}