From a16edc4eaa33aebcede467b35faba0ed5eab0c66 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Wed, 21 Apr 2021 13:59:33 +0200 Subject: [PATCH 1/2] Fix #11185: cache pretyped argument with adaptation in FunProto --- compiler/src/dotty/tools/dotc/typer/Applications.scala | 6 +++++- compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala | 4 ++++ tests/pos/i11185.scala | 5 +++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i11185.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 4f87d0741be3..2d30b925a7eb 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -2101,7 +2101,11 @@ trait Applications extends Compatibility { else defn.FunctionOf(commonParamTypes, WildcardType) overload.println(i"pretype arg $arg with expected type $commonFormal") if (commonParamTypes.forall(isFullyDefined(_, ForceDegree.flipBottom))) - withMode(Mode.ImplicitsEnabled)(pt.typedArg(arg, commonFormal)) + withMode(Mode.ImplicitsEnabled) { + // We can cache the adapted argument here because the expected type + // is a common type shared by all overloading candidates. + pt.cacheArg(arg, pt.typedArg(arg, commonFormal)) + } } recur(altFormals.map(_.tail), args1) case _ => diff --git a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala index 0d2c60c677b0..bc479b51b052 100644 --- a/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala +++ b/compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala @@ -424,6 +424,10 @@ object ProtoTypes { if (t == null) NoType else t.tpe } + /** Cache the typed argument */ + def cacheArg(arg: untpd.Tree, targ: Tree) = + state.typedArg = state.typedArg.updated(arg, targ) + /** The same proto-type but with all arguments combined in a single tuple */ def tupledDual: FunProto = state.tupledDual match { case pt: FunProto => diff --git a/tests/pos/i11185.scala b/tests/pos/i11185.scala new file mode 100644 index 000000000000..8bb407b6cebb --- /dev/null +++ b/tests/pos/i11185.scala @@ -0,0 +1,5 @@ +class Test: + def foo(a: Int, b: Int) = a + b + + Map(1 -> 2).map(foo _) + Map(1 -> 2).map(foo) From 1bf1b3fab3da0b6af7ac13036a45b47c0450e9f8 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Wed, 21 Apr 2021 15:17:58 +0200 Subject: [PATCH 2/2] Add another test: in case Map.map becomes non-overloaded (thanks @Jasper-M) --- tests/pos/i11185.scala | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/pos/i11185.scala b/tests/pos/i11185.scala index 8bb407b6cebb..3b4a308bbc9e 100644 --- a/tests/pos/i11185.scala +++ b/tests/pos/i11185.scala @@ -3,3 +3,12 @@ class Test: Map(1 -> 2).map(foo _) Map(1 -> 2).map(foo) + +class Test2: + def foo(a: Int, b: Int) = a + b + + def bar(f: ((Int, Int)) => Int) = "ok" + def bar(f: ((Int, Int)) => String)(using Int) = "ok" + + bar(foo) + bar(foo _)