Skip to content

Bad type inference for default parameter #18456

Open
@KuceraMartin

Description

@KuceraMartin

Compiler version

3.3.0

Minimized code

class Box[T](t: T)

def f[T](l: List[Box[T]] = List.empty): Unit = ???

def g[T] = f[T]()

Output

[error] ./inference2.scala:5:12
[error] Found:    List[Box[Nothing]]
[error] Required: List[Box[T]]
[error] def g[T] = f[T]()
[error]            ^^^^

Syntax tree after typer:

package <empty> {
  trait Box[T >: Nothing <: Any]() extends Object {
    T
  }
  final lazy module val inference2$package: inference2$package =
    new inference2$package()
  final module class inference2$package() extends Object() {
    this: inference2$package.type =>
    def f[T >: Nothing <: Any](l: List[Box[T]]): Unit = ???
    def f$default$1[T >: Nothing <: Any]: List[Box[Nothing]] =
      List.empty[Box[Nothing]]
    def g[T >: Nothing <: Any]: Unit = f[T](inference2$package.f$default$1[T])
  }
}

Expectation

The return type of f$default$1 should not be List[Box[Nothing]]. I believe it should be List[Box[T]], although List[Nothing] would also work fine in this case.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions