@@ -860,46 +860,71 @@ class Inliner(val call: tpd.Tree)(using Context):
860860 case _ => sel.tpe
861861 }
862862 val selType = if (sel.isEmpty) wideSelType else selTyped(sel)
863- reduceInlineMatch(sel, selType, cases.asInstanceOf [List [CaseDef ]], this ) match {
864- case Some ((caseBindings, rhs0)) =>
865- // drop type ascriptions/casts hiding pattern-bound types (which are now aliases after reducing the match)
866- // note that any actually necessary casts will be reinserted by the typing pass below
867- val rhs1 = rhs0 match {
868- case Block (stats, t) if t.span.isSynthetic =>
869- t match {
870- case Typed (expr, _) =>
871- Block (stats, expr)
872- case TypeApply (sel@ Select (expr, _), _) if sel.symbol.isTypeCast =>
873- Block (stats, expr)
874- case _ =>
875- rhs0
863+
864+ /** Make an Inlined that has no bindings. */
865+ def flattenInlineBlock (tree : Tree ): Tree = {
866+ def inlineBlock (call : Tree , stats : List [Tree ], expr : Tree ): Block =
867+ def inlinedTree (tree : Tree ) = Inlined (call, Nil , tree).withSpan(tree.span)
868+ val stats1 = stats.map:
869+ case stat : ValDef => cpy.ValDef (stat)(rhs = inlinedTree(stat.rhs))
870+ case stat : DefDef => cpy.DefDef (stat)(rhs = inlinedTree(stat.rhs))
871+ case stat => inlinedTree(stat)
872+ cpy.Block (tree)(stats1, flattenInlineBlock(inlinedTree(expr)))
873+
874+ tree match
875+ case tree @ Inlined (call, bindings, expr) if ! bindings.isEmpty =>
876+ inlineBlock(call, bindings, expr)
877+ case tree @ Inlined (call, Nil , Block (stats, expr)) =>
878+ inlineBlock(call, stats, expr)
879+ case _ =>
880+ tree
881+ }
882+
883+ def reduceInlineMatchExpr (sel : Tree ): Tree = flattenInlineBlock(sel) match
884+ case Block (stats, expr) =>
885+ cpy.Block (sel)(stats, reduceInlineMatchExpr(expr))
886+ case _ =>
887+ reduceInlineMatch(sel, selType, cases.asInstanceOf [List [CaseDef ]], this ) match {
888+ case Some ((caseBindings, rhs0)) =>
889+ // drop type ascriptions/casts hiding pattern-bound types (which are now aliases after reducing the match)
890+ // note that any actually necessary casts will be reinserted by the typing pass below
891+ val rhs1 = rhs0 match {
892+ case Block (stats, t) if t.span.isSynthetic =>
893+ t match {
894+ case Typed (expr, _) =>
895+ Block (stats, expr)
896+ case TypeApply (sel@ Select (expr, _), _) if sel.symbol.isTypeCast =>
897+ Block (stats, expr)
898+ case _ =>
899+ rhs0
900+ }
901+ case _ => rhs0
876902 }
877- case _ => rhs0
878- }
879- val rhs2 = rhs1 match {
880- case Typed (expr, tpt) if rhs1.span.isSynthetic => constToLiteral(expr)
881- case _ => constToLiteral(rhs1)
903+ val rhs2 = rhs1 match {
904+ case Typed (expr, tpt) if rhs1.span.isSynthetic => constToLiteral(expr)
905+ case _ => constToLiteral(rhs1)
906+ }
907+ val (usedBindings, rhs3) = dropUnusedDefs(caseBindings, rhs2)
908+ val rhs = seq(usedBindings, rhs3)
909+ inlining.println(i """ --- reduce:
910+ | $tree
911+ |--- to:
912+ | $rhs""" )
913+ typedExpr(rhs, pt)
914+ case None =>
915+ def guardStr (guard : untpd.Tree ) = if (guard.isEmpty) " " else i " if $guard"
916+ def patStr (cdef : untpd.CaseDef ) = i " case ${cdef.pat}${guardStr(cdef.guard)}"
917+ val msg =
918+ if (tree.selector.isEmpty)
919+ em """ cannot reduce summonFrom with
920+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
921+ else
922+ em """ cannot reduce inline match with
923+ | scrutinee: $sel : ${selType}
924+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
925+ errorTree(tree, msg)
882926 }
883- val (usedBindings, rhs3) = dropUnusedDefs(caseBindings, rhs2)
884- val rhs = seq(usedBindings, rhs3)
885- inlining.println(i """ --- reduce:
886- | $tree
887- |--- to:
888- | $rhs""" )
889- typedExpr(rhs, pt)
890- case None =>
891- def guardStr (guard : untpd.Tree ) = if (guard.isEmpty) " " else i " if $guard"
892- def patStr (cdef : untpd.CaseDef ) = i " case ${cdef.pat}${guardStr(cdef.guard)}"
893- val msg =
894- if (tree.selector.isEmpty)
895- em """ cannot reduce summonFrom with
896- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
897- else
898- em """ cannot reduce inline match with
899- | scrutinee: $sel : ${selType}
900- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
901- errorTree(tree, msg)
902- }
927+ reduceInlineMatchExpr(sel)
903928 }
904929
905930 override def newLikeThis (nestingLevel : Int ): Typer = new InlineTyper (initialErrorCount, nestingLevel)
0 commit comments