Skip to content

Commit

Permalink
Allow inferred parameter types always, when eta-expanding
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dwijnand committed Oct 27, 2023
1 parent 38559d7 commit be9cd5e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
6 changes: 1 addition & 5 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions tests/neg/i5976.scala
Original file line number Diff line number Diff line change
@@ -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
}
}
8 changes: 8 additions & 0 deletions tests/pos/i18453.scala
Original file line number Diff line number Diff line change
@@ -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)
8 changes: 8 additions & 0 deletions tests/pos/i18453.workaround.scala
Original file line number Diff line number Diff line change
@@ -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))

0 comments on commit be9cd5e

Please sign in to comment.