Skip to content

Fix #2430: Make Erasure pass Ycheck #2927

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 31, 2017
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
44 changes: 31 additions & 13 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,44 @@ object Erasure {
* Casts from and to ErasedValueType are special, see the explanation
* in ExtensionMethods#transform.
*/
def cast(tree: Tree, pt: Type)(implicit ctx: Context): Tree = {
def cast(tree: Tree, pt: Type)(implicit ctx: Context): Tree = ctx.traceIndented(i"cast ${tree.tpe.widen} --> $pt", show = true) {
def wrap(tycon: TypeRef) =
ref(u2evt(tycon.typeSymbol.asClass)).appliedTo(tree)
def unwrap(tycon: TypeRef) =
ref(evt2u(tycon.typeSymbol.asClass)).appliedTo(tree)


assert(!pt.isInstanceOf[SingletonType], pt)
if (pt isRef defn.UnitClass) unbox(tree, pt)
else (tree.tpe, pt) match {
else (tree.tpe.widen, pt) match {
case (JavaArrayType(treeElem), JavaArrayType(ptElem))
if treeElem.widen.isPrimitiveValueType && !ptElem.isPrimitiveValueType =>
// See SI-2386 for one example of when this might be necessary.
cast(ref(defn.runtimeMethodRef(nme.toObjectArray)).appliedTo(tree), pt)
case (_, ErasedValueType(tycon, _)) =>
ref(u2evt(tycon.symbol.asClass)).appliedTo(tree)
case _ =>
tree.tpe.widen match {
case ErasedValueType(tycon, _) =>
ref(evt2u(tycon.symbol.asClass)).appliedTo(tree)
case _ =>
if (pt.isPrimitiveValueType)
primitiveConversion(tree, pt.classSymbol)
else
tree.asInstance(pt)

// When casting between two EVTs, we need to check which one underlies the other to determine
// wheter u2evt or evt2u should be used.
case (tp1 @ ErasedValueType(tycon1, underlying1), tp2 @ ErasedValueType(tycon2, underlying2)) =>
if (tp1 <:< underlying2)
// Cast EVT(tycon1, underlying1) to EVT(tycon2, EVT(tycon1, underlying1))
wrap(tycon2)
else {
assert(underlying1 <:< tp2, i"Non-sensical cast between unrelated types $tp1 and $tp2")
// Cast EVT(tycon1, EVT(tycon2, underlying2)) to EVT(tycon2, underlying2)
unwrap(tycon1)
}

// When only one type is an EVT then we already know that the other one is the underlying
case (_, ErasedValueType(tycon2, _)) =>
wrap(tycon2)
case (ErasedValueType(tycon1, _), _) =>
unwrap(tycon1)

case _ =>
if (pt.isPrimitiveValueType)
primitiveConversion(tree, pt.classSymbol)
else
tree.asInstance(pt)
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotc/tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class tests extends CompilerTest {
}

implicit val defaultOptions: List[String] = noCheckOptions ++ {
if (dotty.Properties.isRunByDrone) List("-Ycheck:tailrec,resolveSuper,mixin,getClass,restoreScopes,labelDef") // should be Ycheck:all, but #725
if (dotty.Properties.isRunByDrone) List("-Ycheck:tailrec,resolveSuper,erasure,mixin,getClass,restoreScopes,labelDef") // should be Ycheck:all, but #725
else List("-Ycheck:tailrec,resolveSuper,mixin,restoreScopes,labelDef,simplify")
} ++ checkOptions ++ classPath

Expand Down
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/vulpix/TestConfiguration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ object TestConfiguration {
Array("-classpath", paths)
}

private val yCheckOptions = Array("-Ycheck:tailrec,resolveSuper,mixin,getClass,restoreScopes,labelDef")
private val yCheckOptions = Array("-Ycheck:tailrec,resolveSuper,erasure,mixin,getClass,restoreScopes,labelDef")

val defaultUnoptimised = noCheckOptions ++ checkOptions ++ yCheckOptions ++ classPath
val defaultOptimised = defaultUnoptimised :+ "-optimise"
Expand Down