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

Unsoundness in the virtual pattern matcher #6070

Closed
scabug opened this issue Jul 12, 2012 · 2 comments
Closed

Unsoundness in the virtual pattern matcher #6070

scabug opened this issue Jul 12, 2012 · 2 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

@scabug scabug commented Jul 12, 2012

This is my clumsy attempt to exploit the fact that the new pattern matcher does not store case class members in local variables, but instead it goes through getters each time it's referenced in the rhs.

For a while I thought it is only annoying when debugging, until I realized that case classes can have mutable members. So here is a ClassCastException based on the fact that the type checker assumes pattern-bound identifiers to be stable:

abstract class Bomb {
	type T
	val x: T

	def size(that: T): Int
}

class StringBomb extends Bomb {
	type T = String
	val x = "abc"
	def size(that: String): Int = that.length
}

class IntBomb extends Bomb { 
	type T = Int
	val x = 10

	def size(that: Int) = x + that
}

case class Mean(var bomb: Bomb)

object Main extends App {
	def foo(x: Mean) = x match {
		case Mean(b) => 
			// b is assumed to be a stable identifier, but it can actually be mutated
			println(b.size({ mutate(); b.x }))
	}

	def mutate() {
	   	m.bomb = new IntBomb
	}

	val m = Mean(new StringBomb)
	foo(m)
}
@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 12, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6070?orig=1
Reporter: @dragos
Affected Versions: 2.10.0
See #5158

@scabug

This comment has been minimized.

Copy link
Author

@scabug scabug commented Jul 18, 2012

@adriaanm said (edited on Jul 20, 2012 8:51:17 AM UTC):
scala/scala#937

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.