Skip to content

Re-type implicit candidate if expected type is context function #14219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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