@@ -66,15 +66,18 @@ class ExpandSAMs extends MiniPhase {
66
66
case PartialFunctionRHS (pf) =>
67
67
val anonSym = anon.symbol
68
68
69
+ val parents = List (defn.AbstractPartialFunctionType .appliedTo(tpe.argInfos), defn.SerializableType )
70
+ val pfSym = ctx.newNormalizedClassSymbol(anonSym.owner, tpnme.ANON_CLASS , Synthetic | Final , parents, coord = tree.pos)
71
+
69
72
def overrideSym (sym : Symbol ) = sym.copy(
70
- owner = anonSym.owner ,
71
- flags = Synthetic | Method | Final ,
73
+ owner = pfSym ,
74
+ flags = Synthetic | Method | Final | Override ,
72
75
info = tpe.memberInfo(sym),
73
- coord = tree.pos).asTerm
76
+ coord = tree.pos).asTerm.entered
74
77
val isDefinedAtFn = overrideSym(defn.PartialFunction_isDefinedAt )
75
78
val applyOrElseFn = overrideSym(defn.PartialFunction_applyOrElse )
76
79
77
- def translateMatch (tree : Match , pfParam : Symbol , cases : List [CaseDef ], defaultValue : Tree ) = {
80
+ def translateMatch (tree : Match , pfParam : Symbol , cases : List [CaseDef ], defaultValue : Tree )( implicit ctx : Context ) = {
78
81
val selector = tree.selector
79
82
val selectorTpe = selector.tpe.widen
80
83
val defaultSym = ctx.newSymbol(pfParam.owner, nme.WILDCARD , Synthetic , selectorTpe)
@@ -91,7 +94,7 @@ class ExpandSAMs extends MiniPhase {
91
94
// And we need to update all references to 'param'
92
95
}
93
96
94
- def isDefinedAtRhs (paramRefss : List [List [Tree ]]) = {
97
+ def isDefinedAtRhs (paramRefss : List [List [Tree ]])( implicit ctx : Context ) = {
95
98
val tru = Literal (Constant (true ))
96
99
def translateCase (cdef : CaseDef ) =
97
100
cpy.CaseDef (cdef)(body = tru).changeOwner(anonSym, isDefinedAtFn)
@@ -100,20 +103,19 @@ class ExpandSAMs extends MiniPhase {
100
103
translateMatch(pf, paramRef.symbol, pf.cases.map(translateCase), defaultValue)
101
104
}
102
105
103
- def applyOrElseRhs (paramRefss : List [List [Tree ]]) = {
106
+ def applyOrElseRhs (paramRefss : List [List [Tree ]])( implicit ctx : Context ) = {
104
107
val List (paramRef, defaultRef) = paramRefss.head
105
108
def translateCase (cdef : CaseDef ) =
106
109
cdef.changeOwner(anonSym, applyOrElseFn)
107
110
val defaultValue = defaultRef.select(nme.apply).appliedTo(paramRef)
108
111
translateMatch(pf, paramRef.symbol, pf.cases.map(translateCase), defaultValue)
109
112
}
110
113
111
- val isDefinedAtDef = transformFollowingDeep(DefDef (isDefinedAtFn, isDefinedAtRhs(_)))
112
- val applyOrElseDef = transformFollowingDeep(DefDef (applyOrElseFn, applyOrElseRhs(_)))
113
-
114
- val parents = List (defn.AbstractPartialFunctionType .appliedTo(tpe.argInfos), defn.SerializableType )
115
- val anonCls = AnonClass (parents, List (isDefinedAtFn, applyOrElseFn), List (nme.isDefinedAt, nme.applyOrElse))
116
- cpy.Block (tree)(List (isDefinedAtDef, applyOrElseDef), anonCls)
114
+ val constr = ctx.newConstructor(pfSym, Synthetic , Nil , Nil ).entered
115
+ val isDefinedAtDef = transformFollowingDeep(DefDef (isDefinedAtFn, isDefinedAtRhs(_)(ctx.withOwner(isDefinedAtFn))))
116
+ val applyOrElseDef = transformFollowingDeep(DefDef (applyOrElseFn, applyOrElseRhs(_)(ctx.withOwner(applyOrElseFn))))
117
+ val pfDef = ClassDef (pfSym, DefDef (constr), List (isDefinedAtDef, applyOrElseDef))
118
+ cpy.Block (tree)(pfDef :: Nil , New (pfSym.typeRef, Nil ))
117
119
118
120
case _ =>
119
121
val found = tpe.baseType(defn.FunctionClass (1 ))
0 commit comments