scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Two variance checking holes: constructors and default getters #8737

Open scabug opened 10 years ago

scabug commented 10 years ago

The variance validator does not check constructors or default methods. The test case here shows that both of these decisions are unsound (and I suspect the decision not to check case class apply/unapply methods also falls into this category).

The VarianceChecker class in dotty PR #147 detects both variance violations.

As far as default getters are concerned we need to be careful with default getters for case class copy methods. These are allowed to violate variance because of the particular way copy works. The Dotty PR achieves this by annotating the default getters of copy methods with @uncheckedVariance.

scabug commented 10 years ago

Imported From: https://issues.scala-lang.org/browse/SI-8737?orig=1 Reporter: @odersky Affected Versions: 2.11.1 Attachments:

scabug commented 8 years ago

@retronym said: From a re-report, #9549:

class C[+A] {

  private[this] var y: A = _
  def getY: A = y

  class Inner(x: A) {
    y = x
  }
}

object Test {

  def main(args: Array[String]) = {
    val x = new C[String]
    val y: C[Any] = x
    val i = new y.Inner(1)
    val s: String = x.getY
    println(s)
  }
}
scabug commented 8 years ago

Chung-Kil Hur (Gil) said (edited on Sep 26, 2016 4:16:14 AM UTC): Dear developers,

The following code also seems to show a related unsoundness.

sealed abstract class MyADT[+A]
case object MyNone extends MyADT[Nothing]
case class MyFun[A](fun: A=>A) extends MyADT[A]

def data : MyADT[Any] = MyFun((x:Int)=>x+1)

val foo : Any =
  data match {
    case MyFun(f) => f("a")
    case _ => 0
  }

In this code, I simply try to encode the following polymorphic ADT in such a way that MyNone does not require unnecessary parentheses ().

MyADT[A] = MyNone | MyFun of A->A

However, as the code shows, Scala unsoundly accepts the wrong code and produce a type error at run time. So, could you let me know whether there is a sound way to encode MyADT without requiring unnecessary parentheses in Scala?

Thanks, Gil