Skip to content

Commit bb806c5

Browse files
authored
Merge pull request #12171 from dotty-staging/fix-11185
Fix #11185: cache pretyped argument with adaptation in FunProto
2 parents 3a8d2a6 + 1bf1b3f commit bb806c5

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2101,7 +2101,11 @@ trait Applications extends Compatibility {
21012101
else defn.FunctionOf(commonParamTypes, WildcardType)
21022102
overload.println(i"pretype arg $arg with expected type $commonFormal")
21032103
if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom)))
2104-
withMode(Mode.ImplicitsEnabled)(pt.typedArg(arg, commonFormal))
2104+
withMode(Mode.ImplicitsEnabled) {
2105+
// We can cache the adapted argument here because the expected type
2106+
// is a common type shared by all overloading candidates.
2107+
pt.cacheArg(arg, pt.typedArg(arg, commonFormal))
2108+
}
21052109
}
21062110
recur(altFormals.map(_.tail), args1)
21072111
case _ =>

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ object ProtoTypes {
424424
if (t == null) NoType else t.tpe
425425
}
426426

427+
/** Cache the typed argument */
428+
def cacheArg(arg: untpd.Tree, targ: Tree) =
429+
state.typedArg = state.typedArg.updated(arg, targ)
430+
427431
/** The same proto-type but with all arguments combined in a single tuple */
428432
def tupledDual: FunProto = state.tupledDual match {
429433
case pt: FunProto =>

tests/pos/i11185.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Test:
2+
def foo(a: Int, b: Int) = a + b
3+
4+
Map(1 -> 2).map(foo _)
5+
Map(1 -> 2).map(foo)
6+
7+
class Test2:
8+
def foo(a: Int, b: Int) = a + b
9+
10+
def bar(f: ((Int, Int)) => Int) = "ok"
11+
def bar(f: ((Int, Int)) => String)(using Int) = "ok"
12+
13+
bar(foo)
14+
bar(foo _)

0 commit comments

Comments
 (0)