Skip to content

Commit 2c229a6

Browse files
authored
Merge pull request #1903 from dotty-staging/fix-npe-implicits
Fix NPE in Implicits
2 parents eed4f2f + 871de83 commit 2c229a6

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,22 @@ object Implicits {
155155
(ctx.scope eq outerImplicits.ctx.scope)) outerImplicits.level
156156
else outerImplicits.level + 1
157157

158+
/** Is this the outermost implicits? This is the case if it either the implicits
159+
* of NoContext, or the last one before it.
160+
*/
161+
private def isOuterMost = {
162+
val finalImplicits = NoContext.implicits
163+
(this eq finalImplicits) || (outerImplicits eq finalImplicits)
164+
}
165+
158166
/** The implicit references that are eligible for type `tp`. */
159167
def eligible(tp: Type): List[Candidate] = /*>|>*/ track(s"eligible in ctx") /*<|<*/ {
160168
if (tp.hash == NotCached) computeEligible(tp)
161169
else eligibleCache get tp match {
162170
case Some(eligibles) =>
163171
def elided(ci: ContextualImplicits): Int = {
164172
val n = ci.refs.length
165-
if (ci.outerImplicits == NoContext.implicits) n
173+
if (ci.isOuterMost) n
166174
else n + elided(ci.outerImplicits)
167175
}
168176
if (monitored) record(s"elided eligible refs", elided(this))
@@ -183,7 +191,7 @@ object Implicits {
183191
private def computeEligible(tp: Type): List[Candidate] = /*>|>*/ ctx.traceIndented(i"computeEligible $tp in $refs%, %", implicitsDetailed) /*<|<*/ {
184192
if (monitored) record(s"check eligible refs in ctx", refs.length)
185193
val ownEligible = filterMatching(tp)
186-
if (outerImplicits == NoContext.implicits) ownEligible
194+
if (isOuterMost) ownEligible
187195
else ownEligible ::: {
188196
val shadowed = ownEligible.map(_.ref.name).toSet
189197
outerImplicits.eligible(tp).filterNot(cand => shadowed.contains(cand.ref.name))
@@ -192,7 +200,7 @@ object Implicits {
192200

193201
override def toString = {
194202
val own = s"(implicits: ${refs mkString ","})"
195-
if (outerImplicits == NoContext.implicits) own else own + "\n " + outerImplicits
203+
if (isOuterMost) own else own + "\n " + outerImplicits
196204
}
197205

198206
/** This context, or a copy, ensuring root import from symbol `root`

0 commit comments

Comments
 (0)