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

Pattern matcher unsoundness when mixing covariance and nonvariance #7952

Open
scabug opened this issue Nov 6, 2013 · 5 comments
Open

Pattern matcher unsoundness when mixing covariance and nonvariance #7952

scabug opened this issue Nov 6, 2013 · 5 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) patmat
Milestone

Comments

@scabug
Copy link

scabug commented Nov 6, 2013

The following code will give a ClassCast exception.

package test
object patternUnsoundness extends App {

  class C[+T]

  case class D[S](_s: S) extends C[S] {
    var s: S = _s
  }

  val x = new D[String]("abc")
  val y: C[Object] = x

  y match {
    case d @ D(x) => d.s = new Integer(1)
  }

  val z: String = x.s // ClassCast exception
}

Unfortunately, it looks like a difficult fix, because the obvious fix (require that the type parameter S is determined uniquely) would break all :: patterns, because List is declared to be covariant whereas :: is declared to be non-variant. Of course :: could and should have been declared covariant so there's no unsoundness problem for Lists themselves.

(JZ: edited to s/Some/::/)

@scabug
Copy link
Author

scabug commented Nov 6, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7952?orig=1
Reporter: @odersky

@scabug
Copy link
Author

scabug commented Nov 7, 2013

@paulp said:
By 'Some' I take it you mean :: . It won't be as easy as making :: covariant, because :: is built on vars which are set from elsewhere (I assume this is why it's invariant.)

@scabug
Copy link
Author

scabug commented Nov 7, 2013

@paulp said:
Also, I think it will be an interesting challenge to do in a source-compatible way. There's no "deprecatedInvariance" AFAIK, but independently of that I'm not sure it can be done.

@scabug
Copy link
Author

scabug commented Nov 22, 2013

Stephen Compall (s11001001) said:
In 2.11.0-M7 with -Xstrict-inference:

7952.scala:13: type mismatch;
 found   : Integer
 required: S
    case d @ D(x) => d.s = new Integer(1)
                           ^

Changing new Integer(1) to d.s allows it to compile.

@adriaanm adriaanm removed their assignment Sep 28, 2018
@SethTisue SethTisue added this to the Backlog milestone Jun 18, 2023
@SethTisue
Copy link
Member

Scala 3.3.0:

[error] ./S.scala:14:28
[error] Found:    Integer
[error] Required: S$1
[error] 
[error] where:    S$1 is a type locally defined in object patternUnsoundness with bounds <: Object
[error] 
[error] Note that implicit conversions were not tried because the result of an implicit conversion
[error] must be more specific than S$1
[error]     case d @ D(x) => d.s = new Integer(1)
[error]                            ^^^^^^^^^^^^^^

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jun 18, 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/) patmat
Projects
None yet
Development

No branches or pull requests

3 participants