@@ -665,25 +665,32 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
665665 type MatchRedux = Option [(List [MemberDef ], untpd.Tree )]
666666
667667 /** 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
672674 * @return optionally, if match can be reduced to a matching case: A pair of
673675 * bindings for all pattern-bound variables and the RHS of the case.
674676 */
675677 def reduceInlineMatch (scrutinee : Tree , scrutType : Type , cases : List [untpd.CaseDef ], typer : Typer )(implicit ctx : Context ): MatchRedux = {
676678
677- val isImplicit = scrutType.isRef(defn. ImplicitScrutineeTypeSym )
679+ val isImplicit = scrutinee.isEmpty
678680 val gadtSyms = typer.gadtSyms(scrutType)
679681
680682 /** Try to match pattern `pat` against scrutinee reference `scrut`. If successful add
681683 * bindings for variables bound in this pattern to `bindingsBuf`.
682684 */
683685 def reducePattern (bindingsBuf : mutable.ListBuffer [MemberDef ], scrut : TermRef , pat : Tree )(implicit ctx : Context ): Boolean = {
684686
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
686692 bindingsBuf += ValDef (sym, constToLiteral(rhs))
693+ }
687694
688695 def searchImplicit (sym : TermSymbol , tpt : Tree ) = {
689696 val evTyper = new Typer
@@ -771,7 +778,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
771778 yield constToLiteral(reduceProjection(ref(scrut).select(accessor).ensureApplied))
772779 caseAccessors.length == pats.length && reduceSubPatterns(pats, selectors)
773780 }
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
775782 val app = untpd.Apply (untpd.TypedSplice (unapp), untpd.ref(scrut))
776783 val app1 = typer.typedExpr(app)
777784 val args = tupleArgs(app1)
@@ -864,7 +871,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
864871 if (tree.isInline)
865872 errorTree(tree, em """ cannot reduce inline if
866873 | its condition ${tree.cond}
867- | is not a constant value. """ )
874+ | is not a constant value """ )
868875 else {
869876 val if1 = untpd.cpy.If (tree)(cond = untpd.TypedSplice (cond1))
870877 super .typedIf(if1, pt)
@@ -874,10 +881,11 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
874881 override def typedApply (tree : untpd.Apply , pt : Type )(implicit ctx : Context ): Tree =
875882 constToLiteral(betaReduce(super .typedApply(tree, pt)))
876883
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 ) =
878885 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
881889 reduceInlineMatch(sel, selType, cases, this ) match {
882890 case Some ((caseBindings, rhs)) =>
883891 var rhsCtx = ctx.fresh.setNewScope
@@ -892,13 +900,14 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
892900 val msg =
893901 if (tree.selector.isEmpty)
894902 em """ cannot reduce implicit match with
895- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
903+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
896904 else
897905 em """ cannot reduce inline match with
898906 | scrutinee: $sel : ${selType}
899- | patterns : ${tree.cases.map(patStr).mkString(" \n " )}. """
907+ | patterns : ${tree.cases.map(patStr).mkString(" \n " )}"""
900908 errorTree(tree, msg)
901909 }
910+ }
902911
903912 override def newLikeThis : Typer = new InlineTyper
904913 }
@@ -907,6 +916,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {
907916 * Inline def bindings that are used only once.
908917 */
909918 def dropUnusedDefs (bindings : List [MemberDef ], tree : Tree )(implicit ctx : Context ): (List [MemberDef ], Tree ) = {
919+ // inlining.println(i"drop unused $bindings%, % in $tree")
910920 val refCount = newMutableSymbolMap[Int ]
911921 val bindingOfSym = newMutableSymbolMap[MemberDef ]
912922 val dealiased = new java.util.IdentityHashMap [Type , Type ]()
0 commit comments