@@ -461,24 +461,19 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
461
461
}
462
462
463
463
// Drop unused bindings
464
- val matchBindings = reducer.matchBindingsBuf.toList
465
- val (finalBindings, finalExpansion) = dropUnusedDefs(bindingsBuf.toList ++ matchBindings, expansion1)
466
- val (finalMatchBindings, finalArgBindings) = finalBindings.partition(matchBindings.contains(_))
464
+ val (finalBindings, finalExpansion) = dropUnusedDefs(bindingsBuf.toList, expansion1)
467
465
468
466
if (inlinedMethod == defn.Typelevel_error ) issueError()
469
467
470
468
// Take care that only argument bindings go into `bindings`, since positions are
471
469
// different for bindings from arguments and bindings from body.
472
- tpd.Inlined (call, finalArgBindings, seq(finalMatchBindings, finalExpansion) )
470
+ tpd.Inlined (call, finalBindings, finalExpansion)
473
471
}
474
472
}
475
473
476
474
/** A utility object offering methods for rewriting inlined code */
477
475
object reducer {
478
476
479
- /** Additional bindings established by reducing match expressions */
480
- val matchBindingsBuf = new mutable.ListBuffer [MemberDef ]
481
-
482
477
/** An extractor for terms equivalent to `new C(args)`, returning the class `C`,
483
478
* a list of bindings, and the arguments `args`. Can see inside blocks and Inlined nodes and can
484
479
* follow a reference to an inline value binding to its right hand side.
@@ -599,7 +594,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
599
594
def unapply (tree : Trees .Ident [_])(implicit ctx : Context ): Option [Tree ] = {
600
595
def search (buf : mutable.ListBuffer [MemberDef ]) = buf.find(_.name == tree.name)
601
596
if (paramProxies.contains(tree.typeOpt))
602
- search(bindingsBuf).orElse(search(matchBindingsBuf)) match {
597
+ search(bindingsBuf) match {
603
598
case Some (vdef : ValDef ) if vdef.symbol.is(Inline ) =>
604
599
Some (integrate(vdef.rhs, vdef.symbol))
605
600
case Some (ddef : DefDef ) =>
@@ -611,7 +606,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
611
606
}
612
607
613
608
object ConstantValue {
614
- def unapply (tree : Tree )(implicit ctx : Context ): Option [Any ] = tree.tpe.widenTermRefExpr match {
609
+ def unapply (tree : Tree )(implicit ctx : Context ): Option [Any ] = tree.tpe.widenTermRefExpr.normalized match {
615
610
case ConstantType (Constant (x)) => Some (x)
616
611
case _ => None
617
612
}
@@ -662,7 +657,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
662
657
* for the pattern-bound variables and the RHS of the selected case.
663
658
* Returns `None` if no case was selected.
664
659
*/
665
- type MatchRedux = Option [(List [MemberDef ], untpd .Tree )]
660
+ type MatchRedux = Option [(List [MemberDef ], tpd .Tree )]
666
661
667
662
/** Reduce an inline match
668
663
* @param mtch the match tree
@@ -674,7 +669,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
674
669
* @return optionally, if match can be reduced to a matching case: A pair of
675
670
* bindings for all pattern-bound variables and the RHS of the case.
676
671
*/
677
- def reduceInlineMatch (scrutinee : Tree , scrutType : Type , cases : List [untpd. CaseDef ], typer : Typer )(implicit ctx : Context ): MatchRedux = {
672
+ def reduceInlineMatch (scrutinee : Tree , scrutType : Type , cases : List [CaseDef ], typer : Typer )(implicit ctx : Context ): MatchRedux = {
678
673
679
674
val isImplicit = scrutinee.isEmpty
680
675
val gadtSyms = typer.gadtSyms(scrutType)
@@ -712,7 +707,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
712
707
val getBoundVars = new TreeAccumulator [List [TypeSymbol ]] {
713
708
def apply (syms : List [TypeSymbol ], t : Tree )(implicit ctx : Context ) = {
714
709
val syms1 = t match {
715
- case t : Bind if t.symbol.isType && t.name != tpnme. WILDCARD =>
710
+ case t : Bind if t.symbol.isType =>
716
711
t.symbol.asType :: syms
717
712
case _ =>
718
713
syms
@@ -739,7 +734,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
739
734
// ConstraintHandler#approximation does. However, this only works for constrained paramrefs
740
735
// not GADT-bound variables. Hopefully we will get some way to improve this when we
741
736
// re-implement GADTs in terms of constraints.
742
- bindingsBuf += TypeDef (bv)
737
+ if (bv.name != nme. WILDCARD ) bindingsBuf += TypeDef (bv)
743
738
}
744
739
reducePattern(bindingsBuf, scrut, pat1)
745
740
}
@@ -805,7 +800,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
805
800
val scrutineeSym = newSym(InlineScrutineeName .fresh(), Synthetic , scrutType).asTerm
806
801
val scrutineeBinding = normalizeBinding(ValDef (scrutineeSym, scrutinee))
807
802
808
- def reduceCase (cdef : untpd. CaseDef ): MatchRedux = {
803
+ def reduceCase (cdef : CaseDef ): MatchRedux = {
809
804
val caseBindingsBuf = new mutable.ListBuffer [MemberDef ]()
810
805
def guardOK (implicit ctx : Context ) = cdef.guard.isEmpty || {
811
806
val guardCtx = ctx.fresh.setNewScope
@@ -824,7 +819,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
824
819
None
825
820
}
826
821
827
- def recur (cases : List [untpd. CaseDef ]): MatchRedux = cases match {
822
+ def recur (cases : List [CaseDef ]): MatchRedux = cases match {
828
823
case Nil => None
829
824
case cdef :: cases1 => reduceCase(cdef) `orElse` recur(cases1)
830
825
}
@@ -895,14 +890,15 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
895
890
super .typedMatchFinish(tree, sel, wideSelType, cases, pt)
896
891
else {
897
892
val selType = if (sel.isEmpty) wideSelType else sel.tpe
898
- reduceInlineMatch(sel, selType, cases, this ) match {
899
- case Some ((caseBindings, rhs)) =>
900
- var rhsCtx = ctx.fresh.setNewScope
901
- for (binding <- caseBindings) {
902
- matchBindingsBuf += binding
903
- rhsCtx.enter(binding.symbol)
904
- }
905
- typedExpr(rhs, pt)(rhsCtx)
893
+ reduceInlineMatch(sel, selType, cases.asInstanceOf [List [CaseDef ]], this ) match {
894
+ case Some ((caseBindings, rhs0)) =>
895
+ val (usedBindings, rhs1) = dropUnusedDefs(caseBindings, rhs0)
896
+ val rhs = seq(usedBindings, rhs1)
897
+ inlining.println(i """ --- reduce:
898
+ | $tree
899
+ |--- to:
900
+ | $rhs""" )
901
+ typedExpr(rhs, pt)
906
902
case None =>
907
903
def guardStr (guard : untpd.Tree ) = if (guard.isEmpty) " " else i " if $guard"
908
904
def patStr (cdef : untpd.CaseDef ) = i " case ${cdef.pat}${guardStr(cdef.guard)}"
@@ -993,7 +989,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
993
989
val dealiasedType = dealias(t.tpe)
994
990
val t1 = t match {
995
991
case t : RefTree =>
996
- if (boundTypes.contains(t.symbol)) TypeTree (dealiasedType).withPos(t.pos)
992
+ if (t.name != nme. WILDCARD && boundTypes.contains(t.symbol)) TypeTree (dealiasedType).withPos(t.pos)
997
993
else t.withType(dealiasedType)
998
994
case t : DefTree =>
999
995
t.symbol.info = dealias(t.symbol.info)
0 commit comments