Skip to content

Summoning covariant class leads to Nothing in type argument #17707

@soronpo

Description

@soronpo

Removing covariance of T in Fuzzy results in successful compilation.
Not sure that this is a bug, since this could be relevant to Scala's eager widening of non-invariant parameters, since Scala 2 has a similar issue.

Compiler version

v3.2.0-RC2

Minimized code

https://scastie.scala-lang.org/DB2gSGGzT5SLtkbSkskmNA

trait NotNothing[T]
given [T](using util.NotGiven[T <:< Nothing]): NotNothing[T] = new NotNothing[T] {}

trait Fuzzy[+T]
given [T](using ce: NotNothing[T]): Fuzzy[T] = new Fuzzy[T]{}

class Foo
summon[Fuzzy[Foo]] //error

The following code fails similarly in Scala 2 (and succeeds when covariance is removed):
https://scastie.scala-lang.org/hRFQ9LLCQf2Jbxi9jRy76g

class Foo
trait NotNothing[T]
implicit def notNothing1[T <: Nothing]: NotNothing[T] = new NotNothing[T] {}
implicit def notNothing2[T <: Nothing]: NotNothing[T] = new NotNothing[T] {}
implicit def is[T >: Foo]: NotNothing[T] = new NotNothing[T] {}

trait Fuzzy[+T]
implicit def fuzzy[T](implicit ce: NotNothing[T]): Fuzzy[T] = new Fuzzy[T]{}

implicitly[Fuzzy[Foo]]

Output

no given instance of type Playground.Fuzzy[Playground.Foo] was found for parameter x of method summon in object Predef.
I found:

    Playground.given_Fuzzy_T[T](
      Playground.given_NotNothing_T[T](
        /* missing */summon[util.NotGiven[T <:< Nothing]]
      )
    )

But no implicit values were found that match type util.NotGiven[T <:< Nothing].

Expectation

No error, if this is indeed a bug. If it's expected result from the widening mechanism, then we need a future language feature to specify the type argument must be precise/exact/narrow.

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