From 871de839c1dad025e2a0afb525b2e868b28048b1 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 16 Jan 2017 12:16:27 +1100 Subject: Fix NPE in Implicits We got on NPE when compiling the collection strawman under -Yno-imports. We did not preview that the contextual implicit scope stack could be empty. --- compiler/src/dotty/tools/dotc/typer/Implicits.scala | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index 303953e73..4ebc676ba 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -155,6 +155,14 @@ object Implicits { (ctx.scope eq outerImplicits.ctx.scope)) outerImplicits.level else outerImplicits.level + 1 + /** Is this the outermost implicits? This is the case if it either the implicits + * of NoContext, or the last one before it. + */ + private def isOuterMost = { + val finalImplicits = NoContext.implicits + (this eq finalImplicits) || (outerImplicits eq finalImplicits) + } + /** The implicit references that are eligible for type `tp`. */ def eligible(tp: Type): List[Candidate] = /*>|>*/ track(s"eligible in ctx") /*<|<*/ { if (tp.hash == NotCached) computeEligible(tp) @@ -162,7 +170,7 @@ object Implicits { case Some(eligibles) => def elided(ci: ContextualImplicits): Int = { val n = ci.refs.length - if (ci.outerImplicits == NoContext.implicits) n + if (ci.isOuterMost) n else n + elided(ci.outerImplicits) } if (monitored) record(s"elided eligible refs", elided(this)) @@ -183,7 +191,7 @@ object Implicits { private def computeEligible(tp: Type): List[Candidate] = /*>|>*/ ctx.traceIndented(i"computeEligible $tp in $refs%, %", implicitsDetailed) /*<|<*/ { if (monitored) record(s"check eligible refs in ctx", refs.length) val ownEligible = filterMatching(tp) - if (outerImplicits == NoContext.implicits) ownEligible + if (isOuterMost) ownEligible else ownEligible ::: { val shadowed = ownEligible.map(_.ref.name).toSet outerImplicits.eligible(tp).filterNot(cand => shadowed.contains(cand.ref.name)) @@ -192,7 +200,7 @@ object Implicits { override def toString = { val own = s"(implicits: ${refs mkString ","})" - if (outerImplicits == NoContext.implicits) own else own + "\n " + outerImplicits + if (isOuterMost) own else own + "\n " + outerImplicits } /** This context, or a copy, ensuring root import from symbol `root` -- cgit v1.2.3