@@ -295,21 +295,27 @@ object Implicits:
295295 * name, b, whereas the name of the symbol is the original name, a.
296296 * @param outerCtx the next outer context that makes visible further implicits
297297 */
298- class ContextualImplicits (val refs : List [ImplicitRef ], val outerImplicits : ContextualImplicits )(initctx : Context ) extends ImplicitRefs (initctx) {
298+ class ContextualImplicits (
299+ val refs : List [ImplicitRef ],
300+ val outerImplicits : ContextualImplicits ,
301+ isImport : Boolean )(initctx : Context ) extends ImplicitRefs (initctx) {
299302 private val eligibleCache = EqHashMap [Type , List [Candidate ]]()
300303
301304 /** The level increases if current context has a different owner or scope than
302305 * the context of the next-outer ImplicitRefs. This is however disabled under
303306 * Scala2 mode, since we do not want to change the implicit disambiguation then.
304307 */
305308 override val level : Int =
309+ def isSameOwner = irefCtx.owner eq outerImplicits.irefCtx.owner
310+ def isSameScope = irefCtx.scope eq outerImplicits.irefCtx.scope
311+ def isLazyImplicit = refs.head.implicitName.is(LazyImplicitName )
312+
306313 if outerImplicits == null then 1
307314 else if migrateTo3(using irefCtx)
308- || (irefCtx.owner eq outerImplicits.irefCtx.owner)
309- && (irefCtx.scope eq outerImplicits.irefCtx.scope)
310- && ! refs.head.implicitName.is(LazyImplicitName )
315+ || isSameOwner && (isImport || isSameScope && ! isLazyImplicit)
311316 then outerImplicits.level
312317 else outerImplicits.level + 1
318+ end level
313319
314320 /** Is this the outermost implicits? This is the case if it either the implicits
315321 * of NoContext, or the last one before it.
@@ -370,7 +376,7 @@ object Implicits:
370376 val outerExcluded = outerImplicits exclude root
371377 if (irefCtx.importInfo.site.termSymbol == root) outerExcluded
372378 else if (outerExcluded eq outerImplicits) this
373- else new ContextualImplicits (refs, outerExcluded)(irefCtx)
379+ else new ContextualImplicits (refs, outerExcluded, isImport )(irefCtx)
374380 }
375381 }
376382
0 commit comments