Skip to content

strange MatchError if match body has generic method call without explicit type param #12976

@xuwei-k

Description

@xuwei-k

Compiler version

3.0.0, 3.0.1-RC2 and 3.0.2-RC1-bin-20210626-f33bc8d-NIGHTLY

Minimized code

package example

case class A(s: String)

class B {
  def b1[X](str: String): String = str

  def b2[X](str: String): X = null.asInstanceOf[X]
}

object Main {

  def main(args: Array[String]): Unit = {
    val a = A("aaa")
    val b = new B

    // no error
    a match {
      case A(s) =>
        b.b1(s)
    }

    // no error if add explicit type param
    a match {
      case A(s) =>
        b.b2[Boolean](s)
    }

    // scala.MatchError: A(aaa)
    a match {
      case A(s) =>
        b.b2(s)
    }
  }

}

Output

[info] running example.Main 
[error] (run-main-5) scala.MatchError: A(aaa) (of class example.A)
[error] scala.MatchError: A(aaa) (of class example.A)
[error] 	at example.Main$.main(Main.scala:32)
[error] 	at example.Main.main(Main.scala)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error] 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error] 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error] 	at java.lang.reflect.Method.invoke(Method.java:498)

Expectation

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions