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

Can't match partial functions after typechecking with quasiquotes #8411

Closed
scabug opened this issue Mar 13, 2014 · 5 comments
Closed

Can't match partial functions after typechecking with quasiquotes #8411

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

Comments

@scabug
Copy link

scabug commented Mar 13, 2014

scala> tb.typecheck(q"{ case 1 => 2 }", pt = typeOf[PartialFunction[Int, Int]]) match { case q"{ case 1 => 2 }" => }
scala.MatchError: ({
  @SerialVersionUID(0) final <synthetic> class $anonfun extends scala.runtime.AbstractPartialFunction[Int,Int] with Serializable {
    def <init>(): <$anon: Int => Int> = {
      $anonfun.super.<init>();
      ()
    };
    final override def applyOrElse[A1 <: Int, B1 >: Int](x1: A1, default: A1 => B1): B1 = ((x1.asInstanceOf[Int]: Int): Int @unchecked) match {
      case 1 => 2
      case (defaultCase$ @ _) => default.apply(x1)
    };
    final def isDefinedAt(x1: Int): Boolean = ((x1.asInstanceOf[Int]: Int): Int @unchecked) match {
      case 1 => true
      case (defaultCase$ @ _) => false
    }
  };
  new $anonfun()
}: scala.PartialFunction[Int,Int]) (of class scala.reflect.internal.Trees$Typed)
  ... 32 elided
@scabug
Copy link
Author

scabug commented Mar 13, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8411?orig=1
Reporter: @xeno-by
Affected Versions: 2.11.0-RC1
Blocks #8416

@scabug
Copy link
Author

scabug commented Mar 14, 2014

@scabug
Copy link
Author

scabug commented Mar 17, 2014

@densh said (edited on Mar 17, 2014 1:32:59 PM UTC):
Clean fix requires binary incompatible change to reificationSupport :(

densh/scala@2b67909

@scabug
Copy link
Author

scabug commented Mar 17, 2014

@densh said (edited on Mar 17, 2014 1:23:32 PM UTC):
Workaround: use following extractor to match partial functions instead of using native syntax

import universe._, Flag._
object PartialFunction {
  private val AnonFun = TypeName("$anonfun")
  private val ApplyOrElse = TermName("applyOrElse")
  private val DefaultCase = TermName("defaultCase$")
  private val IsDefinedAt = TermName("isDefinedAt")
  def unapply(tree: Tree): Option[List[CaseDef]] = tree match {
    case Match(EmptyTree, cases) => Some(cases)
    case Typed(
           Block(
             List(ClassDef(clsMods, AnonFun, Nil, Template(
               List(abspf: TypeTree, ser: TypeTree), noSelfType, List(
                 DefDef(_, termNames.CONSTRUCTOR, _, _, _, _),
                 DefDef(_, ApplyOrElse, _, _, _,
                   Match(_, cases :+
                     CaseDef(Bind(DefaultCase, Ident(nme.WILDCARD)), _, _))),
                 DefDef(_, IsDefinedAt, _, _, _, _))))),
             Apply(Select(New(Ident(AnonFun)), termNames.CONSTRUCTOR), List())),
           pf: TypeTree)
      if pf.tpe != null && pf.tpe <:< typeOf[scala.PartialFunction[_, _]] &&
         abspf.tpe != null && abspf.tpe <:< typeOf[scala.runtime.AbstractPartialFunction[_, _]] &&
         ser.tpe != null && ser.tpe =:= typeOf[scala.Serializable] &&
         clsMods.hasFlag(FINAL) && clsMods.hasFlag(SYNTHETIC) =>
      Some(cases)
    case _ =>
      None
  }
}

@scabug
Copy link
Author

scabug commented Mar 22, 2014

@densh said:
scala/scala#3647

@scabug scabug closed this as completed Mar 25, 2014
@scabug scabug added the has PR label Apr 7, 2017
@scabug scabug added this to the 2.11.0-RC4 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