@@ -330,11 +330,25 @@ object Implicits:
330330 (this eq finalImplicits) || (outerImplicits eq finalImplicits)
331331 }
332332
333+ private def combineEligibles (ownEligible : List [Candidate ], outerEligible : List [Candidate ]): List [Candidate ] =
334+ if ownEligible.isEmpty then outerEligible
335+ else if outerEligible.isEmpty then ownEligible
336+ else
337+ val shadowed = ownEligible.map(_.ref.implicitName).toSet
338+ ownEligible ::: outerEligible.filterConserve(cand => ! shadowed.contains(cand.ref.implicitName))
339+
340+ def uncachedEligible (tp : Type )(using Context ): List [Candidate ] =
341+ Stats .record(" uncached eligible" )
342+ if monitored then record(s " check uncached eligible refs in irefCtx " , refs.length)
343+ val ownEligible = filterMatching(tp)
344+ if isOuterMost then ownEligible
345+ else combineEligibles(ownEligible, outerImplicits.uncachedEligible(tp))
346+
333347 /** The implicit references that are eligible for type `tp`. */
334348 def eligible (tp : Type ): List [Candidate ] =
335349 if (tp.hash == NotCached )
336350 Stats .record(i " compute eligible not cached ${tp.getClass}" )
337- Stats .record(i " compute eligible not cached " )
351+ Stats .record(" compute eligible not cached" )
338352 computeEligible(tp)
339353 else {
340354 val eligibles = eligibleCache.lookup(tp)
@@ -354,14 +368,8 @@ object Implicits:
354368 private def computeEligible (tp : Type ): List [Candidate ] = /* >|>*/ trace(i " computeEligible $tp in $refs%, % " , implicitsDetailed) /* <|<*/ {
355369 if (monitored) record(s " check eligible refs in irefCtx " , refs.length)
356370 val ownEligible = filterMatching(tp)
357- if (isOuterMost) ownEligible
358- else if ownEligible.isEmpty then outerImplicits.eligible(tp)
359- else
360- val outerEligible = outerImplicits.eligible(tp)
361- if outerEligible.isEmpty then ownEligible
362- else
363- val shadowed = ownEligible.map(_.ref.implicitName).toSet
364- ownEligible ::: outerEligible.filterConserve(cand => ! shadowed.contains(cand.ref.implicitName))
371+ if isOuterMost then ownEligible
372+ else combineEligibles(ownEligible, outerImplicits.eligible(tp))
365373 }
366374
367375 override def isAccessible (ref : TermRef )(using Context ): Boolean =
@@ -1444,7 +1452,12 @@ trait Implicits:
14441452 NoMatchingImplicitsFailure
14451453 else
14461454 val eligible =
1447- if contextual then ctx.implicits.eligible(wildProto)
1455+ if contextual then
1456+ if ctx.gadt.isNarrowing then
1457+ withoutMode(Mode .ImplicitsEnabled ) {
1458+ ctx.implicits.uncachedEligible(wildProto)
1459+ }
1460+ else ctx.implicits.eligible(wildProto)
14481461 else implicitScope(wildProto).eligible
14491462 searchImplicit(eligible, contextual) match
14501463 case result : SearchSuccess =>
0 commit comments