3

Already asked at scala-user, didn't get an answer.

I expect the below to compile:

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]

}

And GoodMatch does, but BadMatch fails (in Scala 2.11.8):

[error] /tmp/rendererqu0xjasKpX/src/main/scala/test.scala:48: type mismatch;
[error]  found   : BadMatch.this.scalan.Elem[Unit]
[error]  required: BadMatch.this.scalan.Elem[_$3] where type _$3
[error]     case UnitElement => ""
[error]          ^
[error] /tmp/rendererqu0xjasKpX/src/main/scala/test.scala:63: type mismatch;
[error]  found   : BadMatch.this.scalan.Elem[Unit]
[error]  required: BadMatch.this.scalan.Elem[B]
[error]     case UnitElement => ()
[error]          ^

Removing with Dummy makes BadMatch compile as well.

Is this a Scala bug? If so, is it a known one?

9
  • 4
    I think it would further help if you posted a minimal reproduce of this (perhaps including UnitElement and BooleanElement declaration instead of pointing to the gist). Commented May 15, 2016 at 8:46
  • Unfortunately, this is a large project and initial attempts to minimize didn't work :( I should be able to do it after a deadline on Monday. Commented May 15, 2016 at 9:14
  • Maybe in the ball park of issues.scala-lang.org/browse/SI-5900 which I haven't studied but it got a recent bump. Commented May 15, 2016 at 9:27
  • Quick guess: try replacing eX: Elem[_] with eX: Elem[Any]. Commented May 15, 2016 at 20:37
  • I couldn't build scalan/lms-backend after publish-local of lms from dev-0.9.x branch. CoreBridge.scala:295: value long_to_int is not a member of scalan.compilation.lms.CoreLmsBackend. I didn't try hard; just FYI. Commented May 15, 2016 at 21:39

1 Answer 1

1

Yes, it's a Scala compiler bug: https://issues.scala-lang.org/browse/SI-9779.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.