-
Notifications
You must be signed in to change notification settings - Fork 21
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
name-based pattern matching could lead to undefined behaviour in runtime #9003
Comments
Imported From: https://issues.scala-lang.org/browse/SI-9003?orig=1 |
@retronym said (edited on Nov 25, 2014 1:39:57 PM UTC): See |
@DarkDimius said: package scala.tools.nsc.backend.jvm
object test {
object TestInt {
type Tree = Product2[Any, Int]
object Block {
def _1: Any = field._1
def _2: Int = field._2
var field: Tree = null
def get: this.type = this
def isEmpty: Boolean = field == null
def isDefined = !isEmpty
def unapply(s: Tree): this.type ={
field = s
this
}
}
}
def genBlock(tree: Any): Unit = tree match{
case TestInt.Block(a, b) =>
genBlock(a)
print(b)
case _ =>
}
def main(args: Array[String]): Unit = {
genBlock(((1,2), 3))
}
} if this code is compiled with |
@gkossakowski said: |
@retronym said: Here's a minimal repro: object NamedBasedPatMat {
var i = 0
def isEmpty = false
def get = i
def unapply(a: NamedBasedPatMat.type) = this
}
object Test {
def main(args: Array[String]): Unit = {
NamedBasedPatMat match {
case NamedBasedPatMat(i) =>
NamedBasedPatMat.i = 1
assert(i == 0) // fails under -optimize
}
}
} |
@retronym said: scala-hash v2.11.4 -optimize
Welcome to Scala version 2.11.4-20141023-110636-d783face36 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_25).
Type in expressions to have them evaluated.
Type :help for more information.
scala> val b = Buffer(1, 2, 3)
b: scala.collection.mutable.Buffer[Int] = ArrayBuffer(1, 2, 3)
scala> b match { case Buffer(1, i, 3) => b(1) = 42; i }
res0: Int = 42 |
@retronym said: |
@DarkDimius said: |
@gkossakowski said: We're building everything using stable version of the compiler. At the moment it's set to 2.11.2: https://github.com/scala/scala/blob/2.11.x/versions.properties#L7 I'll update it to point at 2.11.4. |
@adriaanm said: |
now if used in example
All references to e1 in this block will get rewritten to {code}Block._1{code}. But what happens if one calls foo on Block(Block(A, B), C). The value returned would be B instead of A.
The problem manifests itself only in run-time and does it silently. So marking this critical.
The text was updated successfully, but these errors were encountered: