Skip to content

Commit ca5a264

Browse files
authored
Handle context function arguments in overloading resolution (#16511)
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.
2 parents 4d44638 + e84c6ee commit ca5a264

File tree

3 files changed

+22
-3
lines changed

3 files changed

+22
-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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1923,7 +1923,9 @@ 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)), typeShape(body),
1928+
isContextual = untpd.isContextualClosure(tree))
19271929
case Match(EmptyTree, _) =>
19281930
defn.PartialFunctionClass.typeRef.appliedTo(defn.AnyType :: defn.NothingType :: Nil)
19291931
case _ =>
@@ -2232,7 +2234,7 @@ trait Applications extends Compatibility {
22322234
false
22332235
val commonFormal =
22342236
if (isPartial) defn.PartialFunctionOf(commonParamTypes.head, WildcardType)
2235-
else defn.FunctionOf(commonParamTypes, WildcardType)
2237+
else defn.FunctionOf(commonParamTypes, WildcardType, isContextual = untpd.isContextualClosure(arg))
22362238
overload.println(i"pretype arg $arg with expected type $commonFormal")
22372239
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
22382240
withMode(Mode.ImplicitsEnabled) {

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)