Skip to content

Commit

Permalink
Merge pull request #14219 from dotty-staging/fix-14214
Browse files Browse the repository at this point in the history
Re-type implicit candidate if expected type is context function
  • Loading branch information
odersky committed Jan 12, 2022
2 parents 246b602 + 18e3cac commit 6565219
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Expand Up @@ -240,7 +240,6 @@ object ErrorReporting {
def err(using Context): Errors = new Errors
}


class ImplicitSearchError(
arg: tpd.Tree,
pt: Type,
Expand All @@ -249,6 +248,7 @@ class ImplicitSearchError(
ignoredInstanceNormalImport: => Option[SearchSuccess],
importSuggestionAddendum: => String
)(using ctx: Context) {

def missingArgMsg = arg.tpe match {
case ambi: AmbiguousImplicits =>
(ambi.alt1, ambi.alt2) match {
Expand Down
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/Implicits.scala
Expand Up @@ -1050,8 +1050,13 @@ trait Implicits:
val generated: Tree = tpd.ref(ref).withSpan(span.startPos)
val locked = ctx.typerState.ownedVars
val adapted =
if (argument.isEmpty)
adapt(generated, pt.widenExpr, locked)
if argument.isEmpty then
if defn.isContextFunctionType(pt) then
// need to go through typed, to build the context closure
typed(untpd.TypedSplice(generated), pt, locked)
else
// otherwise we can skip typing and go directly to adapt
adapt(generated, pt.widenExpr, locked)
else {
def untpdGenerated = untpd.TypedSplice(generated)
def producesConversion(info: Type): Boolean = info match
Expand Down
16 changes: 16 additions & 0 deletions tests/pos/i14214.scala
@@ -0,0 +1,16 @@
class Dummy
given Dummy = ???
trait Foo
given foo: Foo = ???
trait Bar
given bar(using Dummy): Bar = ???

object Test:
summon[Dummy ?=> Foo] // was error
summon[Dummy ?=> Foo](using foo) // works
summon[Dummy ?=> Foo](using (_: Dummy) ?=> foo) // works
summon[Dummy ?=> Bar]
summon[Dummy ?=> Bar](using bar) // works
summon[Dummy ?=> Bar](using (_: Dummy) ?=> bar) // works


0 comments on commit 6565219

Please sign in to comment.