Skip to content

Commit ec8fc45

Browse files
committed
Skip contexts for implicit search when resolving imports
1 parent fcd837a commit ec8fc45

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,10 +976,23 @@ trait Implicits:
976976
then return NoMatchingImplicitsFailure
977977

978978
val result0 =
979-
try ImplicitSearch(pt, argument, span).bestImplicit
979+
// If we are searching implicits when resolving an import symbol, start the search
980+
// in the first enclosing context that does not have the same scope as the current
981+
// context. Without that precaution, an eligible implicit in the current scope
982+
// would cause a cyclic reference error (if the import is named) or cause a
983+
// spurious import skip (if the import is a wildcard import). See i1282 for a test case.
984+
var searchCtx = ctx
985+
if ctx.owner.isImport then
986+
while
987+
searchCtx = searchCtx.outer
988+
(searchCtx.scope eq ctx.scope) && (searchCtx.owner eq ctx.owner.owner)
989+
do ()
990+
991+
try ImplicitSearch(pt, argument, span)(using searchCtx).bestImplicit
980992
catch case ce: CyclicReference =>
981993
ce.inImplicitSearch = true
982994
throw ce
995+
end result0
983996

984997
val result =
985998
result0 match {

tests/pos/i10295.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ def doSomething(body: M ?=> Unit) = body(using new M{})
1010

1111
def Test1 =
1212
given M = new M{}
13-
import m.*
14-
val x: X = X.foo()
15-
println(x)
13+
locally {
14+
import m.*
15+
val x: X = X.foo()
16+
println(x)
17+
}
1618

1719
def Test2 =
1820

tests/pos/i12802.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import scala.quoted._
2+
3+
object Boo:
4+
def foo(using Quotes): Unit =
5+
import quotes.reflect._
6+
given Option[Symbol] = Some[Symbol](???)
7+
def bar(using Quotes): Unit =
8+
import quotes.reflect.Symbol
9+
given Option[Symbol] = Some[Symbol](???)

0 commit comments

Comments
 (0)