@@ -73,24 +73,21 @@ class ExpandSAMs extends MiniPhase {
7373 }
7474
7575 val applyRhs = applyDef.rhs
76- val applyFn = applyDef.symbol.asTerm
76+ val applyFn = applyDef.symbol
7777
78- val MethodTpe (paramNames, paramTypes, _) = applyFn.info
79- val isDefinedAtFn = applyFn.copy(
80- name = nme.isDefinedAt,
81- flags = Synthetic | Method ,
82- info = MethodType (paramNames, paramTypes, defn.BooleanType )).asTerm
83-
84- val applyOrElseFn = applyFn.copy(
85- name = nme.applyOrElse,
86- flags = Synthetic | Method ,
87- info = tpt.tpe.memberInfo(defn.PartialFunction_applyOrElse )).asTerm
78+ def overrideSym (sym : Symbol ) = sym.copy(
79+ owner = applyFn.owner,
80+ flags = Synthetic | Method | Final | Private ,
81+ info = tpt.tpe.memberInfo(sym),
82+ coord = tree.pos).asTerm
83+ val isDefinedAtFn = overrideSym(defn.PartialFunction_isDefinedAt )
84+ val applyOrElseFn = overrideSym(defn.PartialFunction_applyOrElse )
8885
8986 def isDefinedAtRhs (paramRefss : List [List [Tree ]]) = {
9087 val tru = Literal (Constant (true ))
9188 applyRhs match {
9289 case tree @ Match (_, cases) =>
93- def translateCase (cdef : CaseDef )=
90+ def translateCase (cdef : CaseDef ) =
9491 cpy.CaseDef (cdef)(body = tru).changeOwner(applyFn, isDefinedAtFn)
9592 val paramRef = paramRefss.head.head
9693 val defaultValue = Literal (Constant (false ))
@@ -109,15 +106,19 @@ class ExpandSAMs extends MiniPhase {
109106 val defaultValue = defaultRef.select(nme.apply).appliedTo(paramRef)
110107 translateMatch(tree, paramRef, cases.map(translateCase), defaultValue)
111108 case _ =>
112- ref(applyFn).appliedTo(paramRef)
109+ applyRhs
110+ .changeOwner(applyFn, applyOrElseFn)
111+ .subst(param.symbol :: Nil , paramRef.symbol :: Nil )
113112 }
114113 }
115114
116115 val isDefinedAtDef = transformFollowingDeep(DefDef (isDefinedAtFn, isDefinedAtRhs(_)))
117116 val applyOrElseDef = transformFollowingDeep(DefDef (applyOrElseFn, applyOrElseRhs(_)))
118117
119- val anonCls = AnonClass (tpt.tpe :: Nil , List (applyFn, isDefinedAtFn, applyOrElseFn), List (nme.apply, nme.isDefinedAt, nme.applyOrElse))
120- cpy.Block (tree)(List (applyDef, isDefinedAtDef, applyOrElseDef), anonCls)
118+ val tpArgs = tpt.tpe.baseType(defn.PartialFunctionClass ).argInfos
119+ val parent = defn.AbstractPartialFunctionType .appliedTo(tpArgs)
120+ val anonCls = AnonClass (parent :: Nil , List (isDefinedAtFn, applyOrElseFn), List (nme.isDefinedAt, nme.applyOrElse))
121+ cpy.Block (tree)(List (isDefinedAtDef, applyOrElseDef), anonCls)
121122 }
122123
123124 private def checkRefinements (tpe : Type , pos : Position )(implicit ctx : Context ): Type = tpe.dealias match {
0 commit comments