Skip to content

Commit

Permalink
Skip contexts for implicit search when resolving imports
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jun 13, 2021
1 parent fcd837a commit c50ae4b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
15 changes: 14 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Implicits.scala
Original file line number Diff line number Diff line change
Expand Up @@ -976,10 +976,23 @@ trait Implicits:
then return NoMatchingImplicitsFailure

val result0 =
try ImplicitSearch(pt, argument, span).bestImplicit
// If we are searching implicits when resolving an import symbol, start the search
// in the first enclosing context that does not have the same scope as the current
// context. Without that precaution, an eligible implicit in the current scope
// would cause a cyclic reference error (if the import is named) or cause a
// spurious import skip (if the import is a wildcard import). See i12802 for a test case.
var searchCtx = ctx
if ctx.owner.isImport then
while
searchCtx = searchCtx.outer
(searchCtx.scope eq ctx.scope) && (searchCtx.owner eq ctx.owner.owner)
do ()

try ImplicitSearch(pt, argument, span)(using searchCtx).bestImplicit
catch case ce: CyclicReference =>
ce.inImplicitSearch = true
throw ce
end result0

val result =
result0 match {
Expand Down
8 changes: 5 additions & 3 deletions tests/pos/i10295.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ def doSomething(body: M ?=> Unit) = body(using new M{})

def Test1 =
given M = new M{}
import m.*
val x: X = X.foo()
println(x)
locally {
import m.*
val x: X = X.foo()
println(x)
}

def Test2 =

Expand Down
9 changes: 9 additions & 0 deletions tests/pos/i12802.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._

object Boo:
def foo(using Quotes): Unit =
import quotes.reflect._
given Option[Symbol] = Some[Symbol](???)
def bar(using Quotes): Unit =
import quotes.reflect.Symbol
given Option[Symbol] = Some[Symbol](???)

0 comments on commit c50ae4b

Please sign in to comment.