Skip to content

Pattern Matching generates incorrect code when matching a nested case class #431

Closed
@smarter

Description

@smarter

The following code:

case class Foo(val underlying: Int)

object O {
  def test(): Unit = {
    val x: Option[Foo] = Some(Foo(1))
    x match {
      case Some(m) =>
        println(m)
    }
  }
}

Generates after patternMatcher:

final module class O$() extends Object() { this: O.type => 
  def test(): Unit = {
    val x: Option[Foo] = Some.apply[Foo'](Foo.apply(1))
    {
      case val selector12: Option[Foo] = x
      {
        def case31(): Unit = {
          def matchFail21(): Unit = throw new MatchError(selector12)
          if selector12.isInstanceOf[Some[Foo']] then {
            val x21: Some[Foo'] = selector12.asInstanceOf[Some[Foo']]
            {
              val o41: Option[Foo] = 
                Some.unapply[Foo'](selector12.asInstanceOf[Some[Foo']])
              if o41.isDefined then {
                val x31: Foo = o41.get
                {
                  val m: Foo = x31._1.asInstanceOf[Foo]
                  {
                    {
                      println(m)
                    }
                  }
                }
              } else matchFail21()
            }
          } else matchFail21()
        }
        case31()
      }
    }
  }
}

Note that x31 has type Foo, but the next line calls x31._1.asInstanceOf[Foo] which does not make sense as the accessor of Foo has type Int, not Foo.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions