Skip to content

Commit a945757

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 a945757

File tree

3 files changed

+25
-3
lines changed

3 files changed

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

+7-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 _ =>
@@ -2232,7 +2237,7 @@ trait Applications extends Compatibility {
22322237
false
22332238
val commonFormal =
22342239
if (isPartial) defn.PartialFunctionOf(commonParamTypes.head, WildcardType)
2235-
else defn.FunctionOf(commonParamTypes, WildcardType)
2240+
else defn.FunctionOf(commonParamTypes, WildcardType, isContextual = untpd.isContextualClosure(arg))
22362241
overload.println(i"pretype arg $arg with expected type $commonFormal")
22372242
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
22382243
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)