Skip to content

Wrong type inference from lambda function's parameter #18453

Open
@KuceraMartin

Description

@KuceraMartin

Compiler version

Scala 3.3.0

Minimized code

trait Box[T]

def f[T, U](c: T => T & U)(using Box[T]): Unit = ???

def g[T, U](using Box[T]): Unit =
  def c(t: T) = t.asInstanceOf[T & U]
  f(c)

Output

[error] ./inference.scala:7:7
[error] No given instance of type Box[T & U] was found for parameter x$2 of method f
[error]   f(c)
[error]       ^

Note that the implicit Box[T] is only used to showcase a compilation failure. The real problem is that when calling f from g, the type parameters get inferred as f[T & U, T & U] as can be seen after the typer phase:

def g[T >: Nothing <: Any, U >: Nothing <: Any](using x$1: Box[T]): Unit =
  {
    def c(t: T): T & U = t.asInstanceOf[T & U]
    f[T & U, T & U](
      {
        def $anonfun(t: T): T & U & (T & U) = c(t)
        closure($anonfun)
      }
    )(/* missing */summon[Box[T & U]])
  }

Expectation

The first type parameter should be inferred as T and thus the code should compile.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions