Skip to content

Inference of higher-kinded type parameters is not precise enough #732

Closed
@smarter

Description

@smarter

In the following code:

class Foo[A](x: A) { override def toString = "Foo(" + x + ")" }
class Bar[A](x: A) extends Foo[A](x) { override def toString = "Bar(" + x + ")" }

object Foo {
  implicit val f: Foo[Int] = new Foo(42)
}

object Bar {
  implicit val b: Bar[Int] = new Bar(42)
}

object Test {
  def printImplicit[CC[X] <: Foo[X]](x: CC[Int])(implicit ev: CC[Int]) = println(ev)

  def main(args: Array[String]) = {
    printImplicit(new Foo(1)) // print "Foo(42)"
    printImplicit[Bar](new Bar(1)) // print "Bar(42)"
    printImplicit(new Bar(1)) // dotty: print "Foo(42)" || scalac: print "Bar(42)"
  }
}

The third implicit in mainis not correctly inferred, because the type parameter is not precise enough:

result of try/hk.scala after frontend:
...
def main(args: Array[String]): Unit = {
  Test.printImplicit[([HK$0] =>  <: Foo[HK$0])](new Foo[Int](1))(Foo.f)
  Test.printImplicit[Bar](new Bar[Int](1))(Bar.b)
  Test.printImplicit[([HK$0] =>  <: Foo[HK$0])](new Bar[Int](1))(Foo.f)
}

scalac handles this correctly:

[syntax trees at end of                     typer]] // hk.scala
...
def main(args: Array[String]): Unit = {
  Test.this.printImplicit[Foo](new Foo[Int](1))(Foo.f);
  Test.this.printImplicit[Bar](new Bar[Int](1))(Bar.b);
  Test.this.printImplicit[Bar](new Bar[Int](1))(Bar.b)
}

Fixing this is necessary to replace #716 by a proper fix, see the work-in-progress at https://github.com/smarter/dotty/commits/fix/missing-implicits .

@odersky : Note that this also happens after applying #731 .

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