Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong type inference from lambda function's parameter #18453

Open
KuceraMartin opened this issue Aug 24, 2023 · 2 comments · Fixed by #18771
Open

Wrong type inference from lambda function's parameter #18453

KuceraMartin opened this issue Aug 24, 2023 · 2 comments · Fixed by #18771

Comments

@KuceraMartin
Copy link
Contributor

KuceraMartin commented Aug 24, 2023

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.

@KuceraMartin KuceraMartin added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 24, 2023
@som-snytt
Copy link
Contributor

Note f(c(_)) works.

@szymon-rd szymon-rd added area:typer and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Aug 25, 2023
@dwijnand dwijnand linked a pull request Oct 27, 2023 that will close this issue
@dwijnand
Copy link
Member

Reverted in #19189

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants