Strange type mismatch error

50 views
Skip to first unread message

Alexey Romanov

unread,
May 12, 2016, 10:23:57 AM5/12/16
to scala-user
I have a class Elem[A] and values UnitElement: Elem[Unit], IntElement: Elem[Int] etc. Given this,

protected def toLuaValue(x: Any, eX: Elem[_]): LuaValue = eX match {
case UnitElement => LuaValue.NIL
case BooleanElement => LuaValue.valueOf(x.asInstanceOf[Boolean])
...
}

should compile, but it doesn't. The error is

[error] /home/travis/build/scalan/scalan/lua-backend/core/src/main/scala/scalan/compilation/lua/LuaCompiler.scala:40: type mismatch;

[error] found : LuaCompiler.this.scalan.Elem[Unit]

[error] required: LuaCompiler.this.scalan.Elem[_$1] where type _$1

[error] case UnitElement => LuaValue.NIL

[error] ^

[error] /home/travis/build/scalan/scalan/lua-backend/core/src/main/scala/scalan/compilation/lua/LuaCompiler.scala:41: type mismatch;

[error] found : LuaCompiler.this.scalan.Elem[Boolean]

[error] required: LuaCompiler.this.scalan.Elem[_$1] where type _$1

[error] case BooleanElement => LuaValue.valueOf(x.asInstanceOf[Boolean])

[error] ^

...


which doesn't make sense to me: the types do match. In addition:


1. It used to work, and the commit which broke compilation just makes Elem extend an empty trait (https://github.com/scalan/scalan/commit/402aa0cb266c774bac39646193305f8215b3b905):


+ trait Dummy


- abstract class Elem[A] extends Serializable { _: scala.Equals =>
+ abstract class Elem[A] extends Serializable with Dummy { _: scala.Equals =>


2. Other similar matches on Elem[_] and Elem[A] weren't broken by this commit: https://github.com/scalan/scalan/blob/402aa0cb266c774bac39646193305f8215b3b905/core/src/main/scala/scalan/JNIExtractorOps.scala#L172-L182, https://github.com/scalan/scalan/blob/402aa0cb266c774bac39646193305f8215b3b905/core/src/main/scala/scalan/arrays/ArrayOps.scala#L359-L380, https://github.com/scalan/scalan/blob/402aa0cb266c774bac39646193305f8215b3b905/lms-backend/core/src/main/scala/scalan/compilation/lms/LmsBridge.scala#L295-L362


Is this a compiler bug? If so, is it known?

Alexey Romanov

unread,
May 17, 2016, 5:55:35 AM5/17/16
to scala-user
Minimized example:

trait Elems {

  trait Dummy


  abstract class Elem[A] extends Serializable with Dummy


  class BaseElem[A] extends Elem[A]

  implicit val BooleanElement: Elem[Boolean] = new BaseElem[Boolean]
  implicit val ByteElement: Elem[Byte] = new BaseElem[Byte]
  implicit val ShortElement: Elem[Short] = new BaseElem[Short]
  implicit val IntElement: Elem[Int] = new BaseElem[Int]
  implicit val LongElement: Elem[Long] = new BaseElem[Long]
  implicit val FloatElement: Elem[Float] = new BaseElem[Float]
  implicit val DoubleElement: Elem[Double] = new BaseElem[Double]
  implicit val UnitElement: Elem[Unit] = new BaseElem[Unit]
  implicit val StringElement: Elem[String] = new BaseElem[String]
  implicit val CharElement: Elem[Char] = new BaseElem[Char]
}

trait GoodMatch { self: Elems =>

  private def boxed_class(e: Elem[_]): Class[_] = e match {
    case BooleanElement => classOf[java.lang.Boolean]
    case ByteElement => classOf[java.lang.Byte]
    case ShortElement => classOf[java.lang.Short]
    case IntElement => classOf[java.lang.Integer]
    case LongElement => classOf[java.lang.Long]
    case FloatElement => classOf[java.lang.Float]
    case DoubleElement => classOf[java.lang.Double]
    case CharElement => classOf[java.lang.Character]
    case _ => ???
  }

}

abstract class BadMatch[+A <: Elems](scalan: A) {
  import scalan._

  protected def toLuaValue(x: Any, eX: Elem[_]): String = eX match {
    case UnitElement => ""
    case _ => ???
  }

  // should check type before conversion?
  protected def fromLuaValue[B](lv: Any, eA: Elem[B]): B = (eA match {
    case UnitElement => ()
  }).asInstanceOf[B]

}

Only BadMatch fails to compile, and removing "with Dummy" makes the errors go away.

Jason Zaugg

unread,
May 17, 2016, 8:33:07 AM5/17/16
to Alexey Romanov, scala-user
Hi Alexey,

I've whittled this down some more and identified two workarounds and what I think is a fix for the root cause: https://gist.github.com/retronym/41084b66d1df9904558d63797c579a2c

Could you please raise a ticket?

-jason

--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alexey Romanov

unread,
May 17, 2016, 9:39:31 AM5/17/16
to scala-user, alexey.v...@gmail.com
Thank you! This actually means the simple workaround for my purposes is just casting to Any before pattern-matching. Ticket at https://issues.scala-lang.org/browse/SI-9779.
Reply all
Reply to author
Forward
0 new messages