Skip to content

incorrect type checking in pattern matching #1255

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

Closed
liufengyun opened this issue May 16, 2016 · 3 comments
Closed

incorrect type checking in pattern matching #1255

liufengyun opened this issue May 16, 2016 · 3 comments
Assignees

Comments

@liufengyun
Copy link
Contributor

Following code passes type checking in dotty:

object Test {
  def foo(x: Option[Int]) = x match {
    case Some(_: Double) => true
    case None => true
  }
}

In scalac it generates following error message:

error: scrutinee is incompatible with pattern type;
 found   : Double
 required: Int
    case Some(_: Double) => true
                 ^
one error found
@felixmulder felixmulder self-assigned this May 19, 2016
felixmulder added a commit to dotty-staging/dotty that referenced this issue May 19, 2016
@DarkDimius
Copy link
Contributor

DarkDimius commented May 23, 2016

I propose to make it both more general and simpler by decoupling the transformation from typer and doing it after pattern matcher.

I propose to intercept sc.isInstanceOf[Sel]: and, depending on the erased type of sc and Sel, either:

  • report the error\warning if test is always failing;
  • replace the call be constant if the result is statically known;
  • leave as is.

Here is the table for transformation:

Sel\sc Trait Class final Class
Trait happens happens known statically
Class happens false if classes are unrelated known statically
Final class false if classes are unrelated false if classes are unrelated known statically

@liufengyun
Copy link
Contributor Author

liufengyun commented May 27, 2016

@felixmulder There is an interesting bug with scalac related to this issue, which you may want to add to the test set:

case class A(v: Int)
class B(v: Int) extends A(v)

object Test {
  val a = new B(1)
  a match { case A(_) => 1 }
}

The code above looks perfectly fine, but scalac generates an error:

error: constructor cannot be instantiated to expected type;
 found   : A
 required: B
  a match { case A(_) => 1 }
                 ^
one error found

However, if we annotate a with val a:A = new B(1), everything is fine.

SI-9795.

@felixmulder
Copy link
Contributor

This issue was resolved in #1275

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

No branches or pull requests

3 participants