Skip to content

type parameter inference behaves unexpectedly when a type parameter is used in a contravariant position #718

Closed
@smarter

Description

@smarter
class Foo[-T]

object Test {
  def foo[A](x: Foo[A]): Int = 0
  foo(new Foo[Int])
}

scalac infers A to be Int:

fsc -Xprint:typer try/brokeninf.scala
[[syntax trees at end of                     typer]] // brokeninf.scala
package <empty> {
  class Foo[-T] extends scala.AnyRef {
    def <init>(): Foo[T] = {
      Foo.super.<init>();
      ()
    }
  };
  object Test extends scala.AnyRef {
    def <init>(): Test.type = {
      Test.super.<init>();
      ()
    };
    def foo[A](x: Foo[A]): Int = 0;
    Test.this.foo[Int](new Foo[Int]())
  }
}

But dotty infers A to be Nothing:

./bin/dotc -Xprint:frontend try/brokeninf.scala
result of try/brokeninf.scala after frontend:
package <empty> {
  class Foo[T]() extends Object() { 
    type Foo$$T
    private[this] type T =- Foo$$T
  }
  final lazy module val Test: Test$ = new Test$()
  final module class Test$() extends Object() { this: Test.type => 
    def foo[A](x: Foo[A]): Int = 0
    Test.foo[Nothing](new Foo[Int][Int]())
  }
}

This is probably not what we want, especially for function types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions