Skip to content

Refining a return type in a sub-trait loses type information #10725

@szeiger

Description

@szeiger

In this following code the overridden declaration of fact in Iterable makes type inference fail for g and h The version in f where the additional type information is deliberately dropped, compiles successfully.

object IterableTest {
  trait IterableOps[+CC[_]] {
    def fact: IterableFactory[CC]
  }

  trait Iterable[+A] extends IterableOps[Iterable] {
    override def fact: IterableFactory[Iterable]
  }

  trait IterableFactory[+CC[_]]

  def f[CC[_] <: Iterable[_] with IterableOps[CC]](from: CC[_]): IterableFactory[CC] = (from:                  IterableOps[CC]).fact
  def g[CC[_] <: Iterable[_] with IterableOps[CC]](from: CC[_]): IterableFactory[CC] = (from: Iterable[_] with IterableOps[CC]).fact
  def h[CC[_] <: Iterable[_] with IterableOps[CC]](from: CC[_]): IterableFactory[CC] = (from                                  ).fact
}

This fails to compile (tested with Scala 2.12.2, 2.12.4 and 2.13.0-M3) with:

IterableTest.scala:13: error: type mismatch;
 found   : IterableTest.IterableFactory[IterableTest.Iterable]
 required: IterableTest.IterableFactory[CC]
  def g[CC[_] <: Iterable[_] with IterableOps[CC]](from: CC[_]): IterableFactory[CC] = (from: Iterable[_] with IterableOps[CC]).fact
                                                                                                                                ^
IterableTest.scala:14: error: type mismatch;
 found   : IterableTest.IterableFactory[IterableTest.Iterable]
 required: IterableTest.IterableFactory[CC]
  def h[CC[_] <: Iterable[_] with IterableOps[CC]](from: CC[_]): IterableFactory[CC] = (from                                  ).fact

It works in Dotty (current master branch).

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions