@@ -1676,19 +1676,38 @@ object desugar {
16761676 case PatDef (mods, pats, tpt, rhs) =>
16771677 val pats1 = if (tpt.isEmpty) pats else pats map (Typed (_, tpt))
16781678 flatTree(pats1 map (makePatDef(tree, mods, _, rhs)))
1679- case ParsedTry (body, handler, finalizer) =>
1680- handler match {
1681- case Match (EmptyTree , cases) => Try (body, cases, finalizer)
1682- case EmptyTree => Try (body, Nil , finalizer)
1683- case _ =>
1684- Try (body,
1685- List (CaseDef (Ident (nme.DEFAULT_EXCEPTION_NAME ), EmptyTree , Apply (handler, Ident (nme.DEFAULT_EXCEPTION_NAME )))),
1686- finalizer)
1687- }
16881679 }
16891680 desugared.withSpan(tree.span)
16901681 }
16911682
1683+ /** Turn a fucntion value `handlerFun` into a catch case for a try.
1684+ * If `handlerFun` is a partial function, translate to
1685+ *
1686+ * case ex =>
1687+ * val ev$1 = handlerFun
1688+ * if ev$1.isDefinedAt(ex) then ev$1.apply(ex) else throw ex
1689+ *
1690+ * Otherwise translate to
1691+ *
1692+ * case ex => handlerFun.apply(ex)
1693+ */
1694+ def makeTryCase (handlerFun : tpd.Tree )(using Context ): CaseDef =
1695+ val handler = TypedSplice (handlerFun)
1696+ val excId = Ident (nme.DEFAULT_EXCEPTION_NAME )
1697+ val rhs =
1698+ if handlerFun.tpe.widen.isRef(defn.PartialFunctionClass ) then
1699+ val tmpName = UniqueName .fresh()
1700+ val tmpId = Ident (tmpName)
1701+ val init = ValDef (tmpName, TypeTree (), handler)
1702+ val test = If (
1703+ Apply (Select (tmpId, nme.isDefinedAt), excId),
1704+ Apply (Select (tmpId, nme.apply), excId),
1705+ Throw (excId))
1706+ Block (init :: Nil , test)
1707+ else
1708+ Apply (Select (handler, nme.apply), excId)
1709+ CaseDef (excId, EmptyTree , rhs)
1710+
16921711 /** Create a class definition with the same info as the refined type given by `parent`
16931712 * and `refinements`.
16941713 *
0 commit comments