Skip to content

PatMat does boxed equality checks #453

Closed
@smarter

Description

@smarter

The following code:

object O {
  Some(1) match {
    case Some(1) => true
  }
}

Becomes after patmat:

final lazy module val O: O$ = new O$()
final module class O$() extends Object() { this: O.type => 
  {
    case val selector11: Some[Int] = Some.apply[Int](1)
    {
      def case11(): Boolean = {
        def matchFail11(): Boolean = (throw new MatchError(selector11))
        {
          val o51: Option[Int] = Some.unapply[Int](selector11)
          (if o51.isDefined then {
            val x21: Int = o51.get
            {
              val p31: Int = x21
              if 1.equals(p31) then {
                val x41: Int(1) = p31.asInstanceOf[Int(1)]
                {
                  {
                    true
                  }
                }
              } else matchFail11()
            }
          } else matchFail11())
        }
      }
      case11()
    }
  }.asInstanceOf[Boolean(true)]

Note the use of 1.equals(p31) which after erasure becomes scala.Int.box(1).equals(scala.Int.box(p31)). This problem does not exist in scalac where pattern matching produces code that uses == instead of equals and thus does not need to be boxed:

object O extends scala.AnyRef {
  def <init>(): O.type = {
    O.super.<init>();
    ()
  };
  {
    case <synthetic> val x1: Some[Int] = scala.Some.apply[Int](1);
    case6(){
      if (x1.ne(null))
        {
          <synthetic> val p2: Int = x1.x;
          if (1.==(p2))
            matchEnd5(true)
          else
            case7()
        }
      else
        case7()
    };
    case7(){
      matchEnd5(throw new MatchError(x1))
    };
    matchEnd5(x: Boolean){
      x
    }
  }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions