Skip to content

Type inference for lambda parameters should be able to take the body of the lambda into account #17771

Closed
@civp

Description

@civp

Compiler version

3.1.1

Minimized code

class A
class B extends A

class C(b: B)
class D(b: B)(x: Int)

def c(b: B): B = b
def d(b: B)(x: Int): B = b

def f[S <: A, T](g: S => T): S => T = g

object Main {
  def main(args: Array[String]): Unit =
    // f(x => C(x)) // compiles
    f(x => D(x)(0)) // doesn't compile
    // f(x => c(x)) // compiles
    f(x => d(x)(0)) // doesn't compile
}

Output

-- [E007] Type Mismatch Error: Test.scala:15:13 --------------------------------
15 |    f(x => D(x)(0)) // doesn't compile
   |             ^
   |             Found:    (x : A)
   |             Required: B

longer explanation available when compiling with `-explain`
-- [E007] Type Mismatch Error: Test.scala:17:13 --------------------------------
17 |    f(x => d(x)(0)) // doesn't compile
   |             ^
   |             Found:    (x : A)
   |             Required: B

longer explanation available when compiling with `-explain`
2 errors found

Expectation

The result of type inference should be (x: B) becaue B is a subclass of A.

The program should compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    stat:needs triageEvery issue needs to have an "area" and "itype" label

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions