@@ -672,33 +672,30 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
672
672
* @return optionally, if match can be reduced to a matching case: A pair of
673
673
* bindings for all pattern-bound variables and the RHS of the case.
674
674
*/
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 = {
676
676
677
- val isImplicit = mtch.selector.isEmpty
677
+ val isImplicit = scrutType.isRef(defn. ImplicitScrutineeTypeSym )
678
678
val gadtSyms = typer.gadtSyms(scrutType)
679
679
680
680
/** Try to match pattern `pat` against scrutinee reference `scrut`. If successful add
681
681
* bindings for variables bound in this pattern to `bindingsBuf`.
682
682
*/
683
683
def reducePattern (bindingsBuf : mutable.ListBuffer [MemberDef ], scrut : TermRef , pat : Tree )(implicit ctx : Context ): Boolean = {
684
684
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 =
688
686
bindingsBuf += ValDef (sym, constToLiteral(rhs))
689
- sym
690
- }
691
687
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))
694
691
evidence.tpe match {
695
692
case fail : Implicits .AmbiguousImplicits =>
696
- ctx.error(typer .missingArgMsg(evidence, tpt.tpe, " " ), tpt.pos)
693
+ ctx.error(evTyper .missingArgMsg(evidence, tpt.tpe, " " ), tpt.pos)
697
694
true // hard error: return true to stop implicit search here
698
695
case fail : Implicits .SearchFailureType =>
699
696
false
700
697
case _ =>
701
- if (name != nme. WILDCARD ) newBinding(name, Implicit , evidence)
698
+ newBinding(sym , evidence)
702
699
true
703
700
}
704
701
}
@@ -718,8 +715,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
718
715
}
719
716
val boundVars = getBoundVars(Nil , tpt)
720
717
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 && {
723
719
for (bv <- boundVars) {
724
720
bv.info = TypeAlias (ctx.gadt.bounds(bv).lo)
725
721
// 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) {
732
728
reducePattern(bindingsBuf, scrut, pat1)
733
729
}
734
730
case pat @ Bind (name : TermName , Typed (_, tpt)) if isImplicit =>
735
- searchImplicit(name , tpt)
731
+ searchImplicit(pat.symbol.asTerm , tpt)
736
732
case pat @ Bind (name : TermName , body) =>
737
733
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))
739
735
true
740
736
}
741
737
case Ident (nme.WILDCARD ) =>
@@ -757,7 +753,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
757
753
def reduceSubPatterns (pats : List [Tree ], selectors : List [Tree ]): Boolean = (pats, selectors) match {
758
754
case (Nil , Nil ) => true
759
755
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)
761
758
reducePattern(bindingsBuf, elem.termRef, pat) &&
762
759
reduceSubPatterns(pats1, selectors1)
763
760
case _ => false
@@ -816,7 +813,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
816
813
case cdef :: cases1 => reduceCase(cdef) `orElse` recur(cases1)
817
814
}
818
815
819
- recur(mtch. cases)
816
+ recur(cases)
820
817
}
821
818
}
822
819
@@ -868,18 +865,20 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
868
865
errorTree(tree, em """ cannot reduce inline if
869
866
| its condition ${tree.cond}
870
867
| 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
+ }
873
872
}
874
873
875
874
override def typedApply (tree : untpd.Apply , pt : Type )(implicit ctx : Context ): Tree =
876
875
constToLiteral(betaReduce(super .typedApply(tree, pt)))
877
876
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 ) =
879
878
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)
881
880
else
882
- reduceInlineMatch(tree, sel, sel.tpe , this ) match {
881
+ reduceInlineMatch(sel, selType, cases , this ) match {
883
882
case Some ((caseBindings, rhs)) =>
884
883
var rhsCtx = ctx.fresh.setNewScope
885
884
for (binding <- caseBindings) {
@@ -896,7 +895,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
896
895
| patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
897
896
else
898
897
em """ cannot reduce inline match with
899
- | scrutinee: $sel : ${sel.tpe }
898
+ | scrutinee: $sel : ${selType }
900
899
| patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
901
900
errorTree(tree, msg)
902
901
}
0 commit comments