Skip to content
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

Two variance checking holes: constructors and default getters #8737

Open
scabug opened this issue Jul 20, 2014 · 3 comments
Open

Two variance checking holes: constructors and default getters #8737

scabug opened this issue Jul 20, 2014 · 3 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) should not compile
Milestone

Comments

@scabug
Copy link

scabug commented Jul 20, 2014

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
Copy link
Author

scabug commented Jul 20, 2014

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

@scabug
Copy link
Author

scabug commented Nov 8, 2015

@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
Copy link
Author

scabug commented Sep 26, 2016

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

@adriaanm adriaanm removed their assignment Sep 28, 2018
@smarter smarter added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Oct 19, 2018
@SethTisue SethTisue added this to the Backlog milestone Aug 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) should not compile
Projects
None yet
Development

No branches or pull requests

4 participants