@@ -665,25 +665,32 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
665
665
type MatchRedux = Option [(List [MemberDef ], untpd.Tree )]
666
666
667
667
/** Reduce an inline match
668
- * @param mtch the match tree
669
- * @param scrutinee the scrutinee expression, assumed to be pure
670
- * @param scrutType its fully defined type
671
- * @param typer The current inline typer
668
+ * @param mtch the match tree
669
+ * @param scrutinee the scrutinee expression, assumed to be pure, or
670
+ * EmptyTree for an implicit match
671
+ * @param scrutType its fully defined type, or
672
+ * ImplicitScrutineeTypeRef for an implicit match
673
+ * @param typer The current inline typer
672
674
* @return optionally, if match can be reduced to a matching case: A pair of
673
675
* bindings for all pattern-bound variables and the RHS of the case.
674
676
*/
675
677
def reduceInlineMatch (scrutinee : Tree , scrutType : Type , cases : List [untpd.CaseDef ], typer : Typer )(implicit ctx : Context ): MatchRedux = {
676
678
677
- val isImplicit = scrutType.isRef(defn. ImplicitScrutineeTypeSym )
679
+ val isImplicit = scrutinee.isEmpty
678
680
val gadtSyms = typer.gadtSyms(scrutType)
679
681
680
682
/** Try to match pattern `pat` against scrutinee reference `scrut`. If successful add
681
683
* bindings for variables bound in this pattern to `bindingsBuf`.
682
684
*/
683
685
def reducePattern (bindingsBuf : mutable.ListBuffer [MemberDef ], scrut : TermRef , pat : Tree )(implicit ctx : Context ): Boolean = {
684
686
685
- def newBinding (sym : TermSymbol , rhs : Tree ): Unit =
687
+ /** Create a binding of a pattern bound variable with matching part of
688
+ * scrutinee as RHS and type that corresponds to RHS.
689
+ */
690
+ def newBinding (sym : TermSymbol , rhs : Tree ): Unit = {
691
+ sym.info = rhs.tpe.widenTermRefExpr
686
692
bindingsBuf += ValDef (sym, constToLiteral(rhs))
693
+ }
687
694
688
695
def searchImplicit (sym : TermSymbol , tpt : Tree ) = {
689
696
val evTyper = new Typer
@@ -771,7 +778,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
771
778
yield constToLiteral(reduceProjection(ref(scrut).select(accessor).ensureApplied))
772
779
caseAccessors.length == pats.length && reduceSubPatterns(pats, selectors)
773
780
}
774
- else if (unapp.symbol.isInlineMethod) { // @@@ not sure that's needed / can be supported
781
+ else if (unapp.symbol.isInlineMethod) { // TODO: Adapt to typed setting
775
782
val app = untpd.Apply (untpd.TypedSplice (unapp), untpd.ref(scrut))
776
783
val app1 = typer.typedExpr(app)
777
784
val args = tupleArgs(app1)
@@ -864,7 +871,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
864
871
if (tree.isInline)
865
872
errorTree(tree, em """ cannot reduce inline if
866
873
| its condition ${tree.cond}
867
- | is not a constant value. """ )
874
+ | is not a constant value """ )
868
875
else {
869
876
val if1 = untpd.cpy.If (tree)(cond = untpd.TypedSplice (cond1))
870
877
super .typedIf(if1, pt)
@@ -874,10 +881,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
874
881
override def typedApply (tree : untpd.Apply , pt : Type )(implicit ctx : Context ): Tree =
875
882
constToLiteral(betaReduce(super .typedApply(tree, pt)))
876
883
877
- override def typedMatchFinish (tree : untpd.Match , sel : Tree , selType : Type , cases : List [untpd.CaseDef ], pt : Type )(implicit ctx : Context ) =
884
+ override def typedMatchFinish (tree : untpd.Match , sel : Tree , wideSelType : Type , cases : List [untpd.CaseDef ], pt : Type )(implicit ctx : Context ) =
878
885
if (! tree.isInline || ctx.owner.isInlineMethod) // don't reduce match of nested inline method yet
879
- super .typedMatchFinish(tree, sel, selType, cases, pt)
880
- else
886
+ super .typedMatchFinish(tree, sel, wideSelType, cases, pt)
887
+ else {
888
+ val selType = if (sel.isEmpty) wideSelType else sel.tpe
881
889
reduceInlineMatch(sel, selType, cases, this ) match {
882
890
case Some ((caseBindings, rhs)) =>
883
891
var rhsCtx = ctx.fresh.setNewScope
@@ -892,13 +900,14 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
892
900
val msg =
893
901
if (tree.selector.isEmpty)
894
902
em """ cannot reduce implicit match with
895
- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
903
+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
896
904
else
897
905
em """ cannot reduce inline match with
898
906
| scrutinee: $sel : ${selType}
899
- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
907
+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
900
908
errorTree(tree, msg)
901
909
}
910
+ }
902
911
903
912
override def newLikeThis : Typer = new InlineTyper
904
913
}
@@ -907,6 +916,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
907
916
* Inline def bindings that are used only once.
908
917
*/
909
918
def dropUnusedDefs (bindings : List [MemberDef ], tree : Tree )(implicit ctx : Context ): (List [MemberDef ], Tree ) = {
919
+ // inlining.println(i"drop unused $bindings%, % in $tree")
910
920
val refCount = newMutableSymbolMap[Int ]
911
921
val bindingOfSym = newMutableSymbolMap[MemberDef ]
912
922
val dealiased = new java.util.IdentityHashMap [Type , Type ]()
0 commit comments