@@ -672,33 +672,30 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
672672 * @return optionally, if match can be reduced to a matching case: A pair of
673673 * bindings for all pattern-bound variables and the RHS of the case.
674674 */
675- def reduceInlineMatch (mtch : untpd. Match , scrutinee : Tree , scrutType : Type , typer : Typer )(implicit ctx : Context ): MatchRedux = {
675+ def reduceInlineMatch (scrutinee : Tree , scrutType : Type , cases : List [untpd. CaseDef ] , typer : Typer )(implicit ctx : Context ): MatchRedux = {
676676
677- val isImplicit = mtch.selector.isEmpty
677+ val isImplicit = scrutType.isRef(defn. ImplicitScrutineeTypeSym )
678678 val gadtSyms = typer.gadtSyms(scrutType)
679679
680680 /** Try to match pattern `pat` against scrutinee reference `scrut`. If successful add
681681 * bindings for variables bound in this pattern to `bindingsBuf`.
682682 */
683683 def reducePattern (bindingsBuf : mutable.ListBuffer [MemberDef ], scrut : TermRef , pat : Tree )(implicit ctx : Context ): Boolean = {
684684
685- def newBinding (name : TermName , flags : FlagSet , rhs : Tree ): Symbol = {
686- val info = if (flags `is` Implicit ) rhs.tpe.widen else rhs.tpe.widenTermRefExpr
687- val sym = newSym(name, flags, info).asTerm
685+ def newBinding (sym : TermSymbol , rhs : Tree ): Unit =
688686 bindingsBuf += ValDef (sym, constToLiteral(rhs))
689- sym
690- }
691687
692- def searchImplicit (name : TermName , tpt : Tree ) = {
693- val evidence = typer.inferImplicitArg(tpt.tpe, tpt.pos)
688+ def searchImplicit (sym : TermSymbol , tpt : Tree ) = {
689+ val evTyper = new Typer
690+ val evidence = evTyper.inferImplicitArg(tpt.tpe, tpt.pos)(ctx.fresh.setTyper(evTyper))
694691 evidence.tpe match {
695692 case fail : Implicits .AmbiguousImplicits =>
696- ctx.error(typer .missingArgMsg(evidence, tpt.tpe, " " ), tpt.pos)
693+ ctx.error(evTyper .missingArgMsg(evidence, tpt.tpe, " " ), tpt.pos)
697694 true // hard error: return true to stop implicit search here
698695 case fail : Implicits .SearchFailureType =>
699696 false
700697 case _ =>
701- if (name != nme. WILDCARD ) newBinding(name, Implicit , evidence)
698+ newBinding(sym , evidence)
702699 true
703700 }
704701 }
@@ -718,8 +715,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
718715 }
719716 val boundVars = getBoundVars(Nil , tpt)
720717 for (bv <- boundVars) ctx.gadt.setBounds(bv, bv.info.bounds)
721- if (isImplicit) searchImplicit(nme.WILDCARD , tpt)
722- else scrut <:< tpt.tpe && {
718+ scrut <:< tpt.tpe && {
723719 for (bv <- boundVars) {
724720 bv.info = TypeAlias (ctx.gadt.bounds(bv).lo)
725721 // FIXME: This is very crude. We should approximate with lower or higher bound depending
@@ -732,10 +728,10 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
732728 reducePattern(bindingsBuf, scrut, pat1)
733729 }
734730 case pat @ Bind (name : TermName , Typed (_, tpt)) if isImplicit =>
735- searchImplicit(name , tpt)
731+ searchImplicit(pat.symbol.asTerm , tpt)
736732 case pat @ Bind (name : TermName , body) =>
737733 reducePattern(bindingsBuf, scrut, body) && {
738- if (name != nme.WILDCARD ) newBinding(name, EmptyFlags , ref(scrut))
734+ if (name != nme.WILDCARD ) newBinding(pat.symbol.asTerm , ref(scrut))
739735 true
740736 }
741737 case Ident (nme.WILDCARD ) =>
@@ -757,7 +753,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
757753 def reduceSubPatterns (pats : List [Tree ], selectors : List [Tree ]): Boolean = (pats, selectors) match {
758754 case (Nil , Nil ) => true
759755 case (pat :: pats1, selector :: selectors1) =>
760- val elem = newBinding(InlineBinderName .fresh(), Synthetic , selector)
756+ val elem = newSym(InlineBinderName .fresh(), Synthetic , selector.tpe.widenTermRefExpr).asTerm
757+ newBinding(elem, selector)
761758 reducePattern(bindingsBuf, elem.termRef, pat) &&
762759 reduceSubPatterns(pats1, selectors1)
763760 case _ => false
@@ -816,7 +813,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
816813 case cdef :: cases1 => reduceCase(cdef) `orElse` recur(cases1)
817814 }
818815
819- recur(mtch. cases)
816+ recur(cases)
820817 }
821818 }
822819
@@ -868,18 +865,20 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
868865 errorTree(tree, em """ cannot reduce inline if
869866 | its condition ${tree.cond}
870867 | is not a constant value. """ )
871- val if1 = untpd.cpy.If (tree)(cond = untpd.TypedSplice (cond1))
872- super .typedIf(if1, pt)
868+ else {
869+ val if1 = untpd.cpy.If (tree)(cond = untpd.TypedSplice (cond1))
870+ super .typedIf(if1, pt)
871+ }
873872 }
874873
875874 override def typedApply (tree : untpd.Apply , pt : Type )(implicit ctx : Context ): Tree =
876875 constToLiteral(betaReduce(super .typedApply(tree, pt)))
877876
878- override def typedMatchFinish (tree : untpd.Match , sel : Tree , selType : Type , pt : Type )(implicit ctx : Context ) =
877+ override def typedMatchFinish (tree : untpd.Match , sel : Tree , selType : Type , cases : List [untpd. CaseDef ], pt : Type )(implicit ctx : Context ) =
879878 if (! tree.isInline || ctx.owner.isInlineMethod) // don't reduce match of nested inline method yet
880- super .typedMatchFinish(tree, sel, selType, pt)
879+ super .typedMatchFinish(tree, sel, selType, cases, pt)
881880 else
882- reduceInlineMatch(tree, sel, sel.tpe , this ) match {
881+ reduceInlineMatch(sel, selType, cases , this ) match {
883882 case Some ((caseBindings, rhs)) =>
884883 var rhsCtx = ctx.fresh.setNewScope
885884 for (binding <- caseBindings) {
@@ -896,7 +895,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
896895 | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
897896 else
898897 em """ cannot reduce inline match with
899- | scrutinee: $sel : ${sel.tpe }
898+ | scrutinee: $sel : ${selType }
900899 | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
901900 errorTree(tree, msg)
902901 }
0 commit comments