Skip to content

Commit 6565219

Browse files
authored
Merge pull request #14219 from dotty-staging/fix-14214
Re-type implicit candidate if expected type is context function
2 parents 246b602 + 18e3cac commit 6565219

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ object ErrorReporting {
240240
def err(using Context): Errors = new Errors
241241
}
242242

243-
244243
class ImplicitSearchError(
245244
arg: tpd.Tree,
246245
pt: Type,
@@ -249,6 +248,7 @@ class ImplicitSearchError(
249248
ignoredInstanceNormalImport: => Option[SearchSuccess],
250249
importSuggestionAddendum: => String
251250
)(using ctx: Context) {
251+
252252
def missingArgMsg = arg.tpe match {
253253
case ambi: AmbiguousImplicits =>
254254
(ambi.alt1, ambi.alt2) match {

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,8 +1050,13 @@ trait Implicits:
10501050
val generated: Tree = tpd.ref(ref).withSpan(span.startPos)
10511051
val locked = ctx.typerState.ownedVars
10521052
val adapted =
1053-
if (argument.isEmpty)
1054-
adapt(generated, pt.widenExpr, locked)
1053+
if argument.isEmpty then
1054+
if defn.isContextFunctionType(pt) then
1055+
// need to go through typed, to build the context closure
1056+
typed(untpd.TypedSplice(generated), pt, locked)
1057+
else
1058+
// otherwise we can skip typing and go directly to adapt
1059+
adapt(generated, pt.widenExpr, locked)
10551060
else {
10561061
def untpdGenerated = untpd.TypedSplice(generated)
10571062
def producesConversion(info: Type): Boolean = info match

tests/pos/i14214.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Dummy
2+
given Dummy = ???
3+
trait Foo
4+
given foo: Foo = ???
5+
trait Bar
6+
given bar(using Dummy): Bar = ???
7+
8+
object Test:
9+
summon[Dummy ?=> Foo] // was error
10+
summon[Dummy ?=> Foo](using foo) // works
11+
summon[Dummy ?=> Foo](using (_: Dummy) ?=> foo) // works
12+
summon[Dummy ?=> Bar]
13+
summon[Dummy ?=> Bar](using bar) // works
14+
summon[Dummy ?=> Bar](using (_: Dummy) ?=> bar) // works
15+
16+

0 commit comments

Comments
 (0)