Skip to content

Commit 58d8e71

Browse files
committed
Various fixes to inlining
Some of them taken by reverting again: Remove remaining bits from untyped inliner (reverted from commit 35646ae) # Conflicts: # compiler/src/dotty/tools/dotc/transform/FirstTransform.scala
1 parent 6848fac commit 58d8e71

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -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]()

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,11 @@ object ProtoTypes {
7575
* achieved by replacing expected type parameters with wildcards.
7676
*/
7777
def constrainResult(meth: Symbol, mt: Type, pt: Type)(implicit ctx: Context): Boolean =
78-
constrainResult(mt, pt)
78+
if (Inliner.isInlineable(meth)) {
79+
constrainResult(mt, wildApprox(pt))
80+
true
81+
}
82+
else constrainResult(mt, pt)
7983
}
8084

8185
object NoViewsAllowed extends Compatibility {

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -993,8 +993,8 @@ class Typer extends Namer
993993
}
994994

995995
// Overridden in InlineTyper for inline matches
996-
def typedMatchFinish(tree: untpd.Match, sel: Tree, selType: Type, cases: List[untpd.CaseDef], pt: Type)(implicit ctx: Context): Tree = {
997-
val cases1 = harmonic(harmonize)(typedCases(cases, selType, pt.notApplied))
996+
def typedMatchFinish(tree: untpd.Match, sel: Tree, wideSelType: Type, cases: List[untpd.CaseDef], pt: Type)(implicit ctx: Context): Tree = {
997+
val cases1 = harmonic(harmonize)(typedCases(cases, wideSelType, pt.notApplied))
998998
.asInstanceOf[List[CaseDef]]
999999
assignType(cpy.Match(tree)(sel, cases1), sel, cases1)
10001000
}
@@ -2473,8 +2473,8 @@ class Typer extends Namer
24732473
else if (Inliner.isInlineable(tree) &&
24742474
!ctx.settings.YnoInline.value &&
24752475
!ctx.isAfterTyper &&
2476-
!ctx.reporter.hasErrors &&
2477-
tree.tpe <:< pt) {
2476+
!ctx.reporter.hasErrors) {
2477+
tree.tpe <:< wildApprox(pt)
24782478
readaptSimplified(Inliner.inlineCall(tree, pt))
24792479
}
24802480
else if (tree.tpe <:< pt) {

0 commit comments

Comments
 (0)