Skip to content

Commit 069ac2c

Browse files
committed
Handle context function arguments in overloading resolution
Fixes #16506 We now handle the cases where an argument of an overloaded method is a context closure. The closure must be given implicitly. It cannot be inferred since at the time where the argument is typed, there is no expected expected type to expand it to a context closure.
1 parent c3097af commit 069ac2c

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,10 @@ object desugar {
14641464
val param = makeSyntheticParameter(
14651465
tpt =
14661466
if params.exists(_.tpt.isEmpty) then TypeTree()
1467-
else Tuple(params.map(_.tpt)))
1467+
else Tuple(params.map(_.tpt)),
1468+
flags =
1469+
if params.nonEmpty && params.head.mods.is(Given) then SyntheticTermParam | Given
1470+
else SyntheticTermParam)
14681471
def selector(n: Int) =
14691472
if (isGenericTuple) Apply(Select(refOfDef(param), nme.apply), Literal(Constant(n)))
14701473
else Select(refOfDef(param), nme.selectorName(n))

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,12 @@ trait Applications extends Compatibility {
19231923
/** The shape of given tree as a type; cannot handle named arguments. */
19241924
def typeShape(tree: untpd.Tree): Type = tree match {
19251925
case untpd.Function(args, body) =>
1926-
defn.FunctionOf(args map Function.const(defn.AnyType), typeShape(body))
1926+
defn.FunctionOf(
1927+
args map Function.const(defn.AnyType),
1928+
typeShape(body),
1929+
isContextual = args match
1930+
case (param: untpd.ValDef) :: _ => param.mods.is(Given)
1931+
case _ => false)
19271932
case Match(EmptyTree, _) =>
19281933
defn.PartialFunctionClass.typeRef.appliedTo(defn.AnyType :: defn.NothingType :: Nil)
19291934
case _ =>
@@ -2032,7 +2037,8 @@ trait Applications extends Compatibility {
20322037
if isDetermined(alts2) then alts2
20332038
else
20342039
record("resolveOverloaded.narrowedByShape", alts2.length)
2035-
pretypeArgs(alts2, pt)
2040+
if args.exists(untpd.isFunctionWithUnknownParamType) then
2041+
pretypeArgs(alts2, pt)
20362042
narrowByTrees(alts2, pt.typedArgs(normArg(alts2, _, _)), resultType)
20372043

20382044
case pt @ PolyProto(targs1, pt1) =>

tests/pos/i16506.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.annotation.targetName
2+
3+
trait Ctx
4+
5+
def foo(f: Ctx => Int) = ???
6+
7+
@targetName("fooContextual")
8+
def foo(f: Ctx ?=> Int) = ???
9+
10+
def bar1 = foo(ctx => 123)
11+
def bar2 = foo((ctx: Ctx) => 123)
12+
def bar3 = foo(ctx ?=> 123)
13+
def bar4 = foo((ctx: Ctx) ?=> 123)
14+
// def bar5 = foo(123) does not work

0 commit comments

Comments
 (0)