From be9cd5ea0c5467eca72fa4cba17d7f47071e8fa9 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 27 Oct 2023 14:00:35 +0100 Subject: [PATCH] Allow inferred parameter types always, when eta-expanding Rather than drawing an exemption based on fully defined types, report the right function arity. This leads eta-expansion to leave off the type, so then the parameter type is inferred. In the test case pos/i18453 this leads to the type var ?A being instantiated to X, rather than ?A & ?B being constrained against X & Y, which leads to ?A being instantiated to X & Y - and then failing to find a Box[X & Y] in scope. --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 6 +----- tests/neg/i5976.scala | 4 ++-- tests/pos/i18453.scala | 8 ++++++++ tests/pos/i18453.workaround.scala | 8 ++++++++ 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/pos/i18453.scala create mode 100644 tests/pos/i18453.workaround.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 6a726432a4b0..da7cc84c3eec 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -4167,11 +4167,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer val funExpected = functionExpected val arity = if funExpected then - if !isFullyDefined(pt, ForceDegree.none) && isFullyDefined(wtp, ForceDegree.none) then - // if method type is fully defined, but expected type is not, - // prioritize method parameter types as parameter types of the eta-expanded closure - 0 - else defn.functionArity(ptNorm) + defn.functionArity(ptNorm) else val nparams = wtp.paramInfos.length if nparams > 1 diff --git a/tests/neg/i5976.scala b/tests/neg/i5976.scala index 8a9c29b85ae1..ef2e743e39fe 100644 --- a/tests/neg/i5976.scala +++ b/tests/neg/i5976.scala @@ -1,7 +1,7 @@ object Test { def f(i: => Int) = i + i - val res = List(42).map(f) // error + val res = List(42).map(f) val g: (=> Int) => Int = f val h: Int => Int = g // error -} \ No newline at end of file +} diff --git a/tests/pos/i18453.scala b/tests/pos/i18453.scala new file mode 100644 index 000000000000..40dd14935a10 --- /dev/null +++ b/tests/pos/i18453.scala @@ -0,0 +1,8 @@ +trait Box[T] + +class Test: + def f[A, B](c: A => A & B)(using ba: Box[A]): Unit = ??? + + def g[X, Y](using bx: Box[X]): Unit = + def d(t: X): X & Y = t.asInstanceOf[X & Y] + f(d) diff --git a/tests/pos/i18453.workaround.scala b/tests/pos/i18453.workaround.scala new file mode 100644 index 000000000000..2c562279f0e8 --- /dev/null +++ b/tests/pos/i18453.workaround.scala @@ -0,0 +1,8 @@ +trait Box[T] + +class Test: + def f[A, B](c: A => A & B)(using ba: Box[A]): Unit = ??? + + def g[X, Y](using bx: Box[X]): Unit = + def d(t: X): X & Y = t.asInstanceOf[X & Y] + f(u => d(u))