Skip to content

Type variables in implicit result are narrowed too eagerly #6385

Closed
@neko-kai

Description

@neko-kai

Example:

trait Tc1[A]
trait Tc2[A] extends Tc1[A]

class PinTypeTo[K[_]]
object PinTypeTo {
  implicit val pinType: PinTypeTo[Tc2] = new PinTypeTo[Tc2]
}

class X
object X {
  implicit def Tc2Instance[F[x] >: Tc2[x]: PinTypeTo]: F[X] = new Tc2[X] {}
}

object app extends App {
  implicitly[Tc2[X]] // ok
  implicitly[Tc1[X]] // fails
}

Expected:
Search for Tc1 should succeed, since F[X] always matches Tc1[X] (Scala 2 behavior)

Actual:
Search fails with

//[error] 17 |  implicitly[Tc1[X]] // fails
//[error]    |                    ^
//[error]    |no implicit argument of type Tc1[X] was found for parameter ev of method implicitly in object DottyPredef.
//[error]    |I found:
//[error]    |
//[error]    |    X.Tc2Instance[F](/* missing */implicitly[PinTypeTo[Tc1]])
//[error]    |
//[error]    |But no implicit values were found that match type PinTypeTo[Tc1].

Seems like F is narrowed to target type Tc1 overeagerly, with all implicit summons then following that assumptions, although it should probably only be constrained to a subtype of Tc1 instead.

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