Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c88c64e

Browse files
retronymmilessabin
authored andcommitted
Drop by-name before creating ImplicitSearch, rather than internally
1 parent eb05421 commit c88c64e

File tree

2 files changed

+26
-23
lines changed

2 files changed

+26
-23
lines changed

src/compiler/scala/tools/nsc/typechecker/Implicits.scala

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ trait Implicits {
9090
if (shouldPrint)
9191
typingStack.printTyping(tree, "typing implicit: %s %s".format(tree, context.undetparamsString))
9292
val implicitSearchContext = context.makeImplicit(reportAmbiguous)
93-
val search = new ImplicitSearch(tree, pt, isView, implicitSearchContext, pos)
93+
val dpt = if (isView) pt else dropByName(pt)
94+
val isByName = dpt ne pt
95+
val search = new ImplicitSearch(tree, dpt, isView, isByName, implicitSearchContext, pos)
9496
pluginsNotifyImplicitSearch(search)
9597
val result = search.bestImplicit
9698
pluginsNotifyImplicitSearchResult(result)
@@ -152,7 +154,7 @@ trait Implicits {
152154
val tvars = tpars map (TypeVar untouchable _)
153155
val tpSubsted = tp.subst(tpars, tvars)
154156

155-
val search = new ImplicitSearch(EmptyTree, functionType(List(tpSubsted), AnyTpe), true, context.makeImplicit(reportAmbiguousErrors = false))
157+
val search = new ImplicitSearch(EmptyTree, functionType(List(tpSubsted), AnyTpe), true, false, context.makeImplicit(reportAmbiguousErrors = false))
156158

157159
search.allImplicitsPoly(tvars)
158160
}
@@ -291,11 +293,16 @@ trait Implicits {
291293
private def isView_=(value: Boolean): Unit = _isView = value
292294

293295
private[this] var _isView: Boolean = false
296+
297+
def isByName: Boolean = _isByName
298+
private def isByName_=(value: Boolean): Unit = _isByName = value
299+
private[this] var _isByName: Boolean = false
294300
}
295301
object OpenImplicit {
296-
def apply(info: ImplicitInfo, pt: Type, tree: Tree, isView: Boolean): OpenImplicit = {
302+
def apply(info: ImplicitInfo, pt: Type, tree: Tree, isView: Boolean, isByName: Boolean): OpenImplicit = {
297303
val result = new OpenImplicit(info, pt, tree)
298304
result.isView = isView
305+
result.isByName = isByName
299306
result
300307
}
301308
}
@@ -382,7 +389,7 @@ trait Implicits {
382389
* (useful when we infer synthetic stuff and pass EmptyTree in the `tree` argument)
383390
* If it's set to NoPosition, then position-based services will use `tree.pos`
384391
*/
385-
class ImplicitSearch(val tree: Tree, val pt: Type, val isView: Boolean, val context0: Context, val pos0: Position = NoPosition) extends Typer(context0) with ImplicitsContextErrors {
392+
class ImplicitSearch(val tree: Tree, val pt: Type, val isView: Boolean, val isByNamePt: Boolean, val context0: Context, val pos0: Position = NoPosition) extends Typer(context0) with ImplicitsContextErrors {
386393
val searchId = implicitSearchId()
387394
private def typingLog(what: String, msg: => String) = {
388395
if (printingOk(tree))
@@ -532,9 +539,7 @@ trait Implicits {
532539
val existsDominatedImplicit: Boolean =
533540
if(tree == EmptyTree) false
534541
else {
535-
val ptByName = isByNameParamType(pt)
536-
val dpt = if (ptByName) dropByName(pt) else pt
537-
lazy val spt = stripped(core(dpt))
542+
lazy val spt = stripped(core(pt))
538543
lazy val sptSyms = allSymbols(spt)
539544
// Are all the symbols of the stripped core of pt contained in the stripped core of tp?
540545
def coversPt(tp: Type): Boolean = {
@@ -547,51 +552,49 @@ trait Implicits {
547552
ois match {
548553
case Nil => false
549554
case (hd@OpenImplicit(info1, tp, tree1)) :: tl =>
550-
val byName = isByNameParamType(tp)
551-
val dtp = if (byName) dropByName(tp) else tp
552555
(if (!info1.sym.isMacro && tree1.symbol == tree.symbol) {
553-
if(belowByName && (dtp =:= dpt)) Some(false) // if there is a byname argument between tp and pt we can tie the knot
554-
else if (dominates(dpt, dtp) && coversPt(dtp)) Some(true)
556+
if(belowByName && (tp =:= pt)) Some(false) // if there is a byname argument between tp and pt we can tie the knot
557+
else if (dominates(pt, tp) && coversPt(tp)) Some(true)
555558
else None
556559
} else None) match {
557560
case Some(res) => res
558-
case None => loop(tl, byName || belowByName)
561+
case None => loop(tl, hd.isByName || belowByName)
559562
}
560563
}
561564
}
562-
loop(context.openImplicits, ptByName)
565+
loop(context.openImplicits, this.isByNamePt)
563566
}
564567

565568
if(existsDominatedImplicit) {
566569
//println("Pending implicit "+pending+" dominates "+pt+"/"+undetParams) //@MDEBUG
567570
DivergentSearchFailure
568571
} else {
569-
val ref = context.refByNameImplicit(dropByName(pt))
572+
val ref = context.refByNameImplicit(pt)
570573
if(ref != EmptyTree)
571574
new SearchResult(ref, EmptyTreeTypeSubstituter, Nil)
572575
else {
573576
val recursiveImplicit: Option[OpenImplicit] =
574577
if(tree == EmptyTree) None
575578
else context.openImplicits find {
576-
case OpenImplicit(info, tp, tree1) =>
577-
(isByNameParamType(tp) || isByNameParamType(pt)) && dropByName(tp) <:< dropByName(pt)
579+
case oi @ OpenImplicit(info, tp, tree1) =>
580+
(oi.isByName || isByNamePt) && oi.pt <:< pt
578581
}
579582

580583
recursiveImplicit match {
581584
case Some(rec) =>
582-
val ref = atPos(pos.focus)(context.linkByNameImplicit(dropByName(rec.pt)))
585+
val ref = atPos(pos.focus)(context.linkByNameImplicit(rec.pt))
583586
new SearchResult(ref, EmptyTreeTypeSubstituter, Nil)
584587
case None =>
585588
try {
586-
context.openImplicits = OpenImplicit(info, pt, tree, isView) :: context.openImplicits
589+
context.openImplicits = OpenImplicit(info, pt, tree, isView, isByNamePt) :: context.openImplicits
587590
//println(" "*context.openImplicits.length+"typed implicit "+info+" for "+pt) //@MDEBUG
588591
val result = typedImplicit0(info, ptChecked, isLocalToCallsite)
589592
if (result.isDivergent) {
590593
//println("DivergentImplicit for pt:"+ pt +", open implicits:"+context.openImplicits) //@MDEBUG
591594
if (context.openImplicits.tail.isEmpty && !pt.isErroneous)
592595
DivergingImplicitExpansionError(tree, pt, info.sym)(context)
593596
result
594-
} else context.defineByNameImplicit(dropByName(pt), result)
597+
} else context.defineByNameImplicit(pt, result)
595598
} finally {
596599
context.openImplicits = context.openImplicits.tail
597600
}
@@ -688,7 +691,7 @@ trait Implicits {
688691
case NullaryMethodType(restpe) => loop(restpe, pt)
689692
case PolyType(_, restpe) => loop(restpe, pt)
690693
case ExistentialType(_, qtpe) => if (fast) loop(qtpe, pt) else normalize(tp) <:< pt // is !fast case needed??
691-
case _ => if (fast) isPlausiblySubType(tp, if(isView) pt else dropByName(pt)) else tp <:< dropByName(pt)
694+
case _ => if (fast) isPlausiblySubType(tp, pt) else tp <:< pt
692695
}
693696
loop(tp0, pt0)
694697
}
@@ -758,7 +761,7 @@ trait Implicits {
758761
typingLog("considering", typeDebug.ptTree(itree1))
759762

760763
@inline def fail(reason: => String): SearchResult = failure(itree0, reason)
761-
def fallback = typed1(itree1, EXPRmode, dropByName(wildPt))
764+
def fallback = typed1(itree1, EXPRmode, wildPt)
762765
try {
763766
val itree2 = if (!isView) fallback else pt match {
764767
case Function1(arg1, arg2) =>
@@ -796,7 +799,7 @@ trait Implicits {
796799
if (StatisticsStatics.areSomeColdStatsEnabled) statistics.incCounter(typedImplicits)
797800

798801
val itree3 = if (isView) treeInfo.dissectApplied(itree2).callee
799-
else adapt(itree2, EXPRmode, dropByName(wildPt))
802+
else adapt(itree2, EXPRmode, wildPt)
800803

801804
typingStack.showAdapt(itree0, itree3, pt, context)
802805

src/interactive/scala/tools/nsc/interactive/Global.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ class Global(settings: Settings, _reporter: Reporter, projectName: String = "")
11331133
val applicableViews: List[SearchResult] =
11341134
if (ownerTpe.isErroneous) List()
11351135
else new ImplicitSearch(
1136-
tree, functionType(List(ownerTpe), AnyTpe), isView = true,
1136+
tree, functionType(List(ownerTpe), AnyTpe), isView = true, isByNamePt = false,
11371137
context0 = context.makeImplicit(reportAmbiguousErrors = false)).allImplicits
11381138
for (view <- applicableViews) {
11391139
val vtree = viewApply(view)

0 commit comments

Comments
 (0)