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

Name-based pattern matching gets confused #9029

Closed
scabug opened this issue Dec 5, 2014 · 5 comments
Closed

Name-based pattern matching gets confused #9029

scabug opened this issue Dec 5, 2014 · 5 comments
Assignees
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Dec 5, 2014

class Foo(val x: Bar) {
  def isEmpty = false
  def get = x
}

object Foo {
  def unapply(x: Foo) = x
}

class Bar(val x: Option[Int], val y: Option[Int]) {
  def isEmpty = false
  def get = this
  def _1 = x
  def _2 = y
}

object Bar {
  def unapply(x: Bar) = x
}

class Test {
  def nameBased: Unit = {
    val x: AnyRef = ???
    x match {
      case Foo(Bar(x1, x2)) => println(x1)
    }
  }
}
12:39 ~/Projects/211x/sandbox (2.11.x)$ s
Test.scala:25: warning: fruitless type test: a value of type (Option[Int], Option[Int]) cannot also be a Bar
      case Foo(Bar(x1, x2)) => println(x1)
                  ^
Test.scala:24: error: error during expansion of this match (this is a scalac bug).
The underlying error was: type mismatch;
 found   : Bar
 required: (Option[Int], Option[Int])
    x match {
      ^
one warning found
@scabug
Copy link
Author

scabug commented Dec 5, 2014

Imported From: https://issues.scala-lang.org/browse/SI-9029?orig=1
Reporter: @xeno-by
Affected Versions: 2.11.4

@scabug
Copy link
Author

scabug commented Dec 5, 2014

@xeno-by said:
Changing the type of Foo.x to AnyRef makes everything compile just fine.

@scabug
Copy link
Author

scabug commented Dec 5, 2014

@xeno-by said:
Making Bar.unapply return Option[(..., ...)] instead of Bar also makes the problem go away.

@scabug
Copy link
Author

scabug commented Aug 29, 2015

@retronym said:
[~travisbrown] reported a similar problem related to name based patmat that I'm fixing here: https://github.com/scala/scala/compare/2.11.x...retronym:ticket/9029?expand=1

class Y(val _2: Int, val _1: String)

object X { def unapply(u: Unit): Option[Y] = Some(new Y(42, "!")) }

object Test {
  def test1 = {
    val X(y) = ()
    val yy: Y = y
    assert(yy._1 == "!")
    assert(yy._2 == 42)
  }
  def main(args: Array[String]): Unit = {
    test1
  }
}

I'm planning to look into the issue reported here as well as part of this work.

@scabug
Copy link
Author

scabug commented Aug 31, 2015

@retronym said:
scala/scala#4720

@scabug scabug closed this as completed Sep 29, 2015
@scabug scabug added the patmat label Apr 7, 2017
@scabug scabug added this to the 2.11.8 milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants