Skip to content

Compiler forgets that scala.scalajs.js.UndefOr is covariant #11865

Closed
@japgolly

Description

@japgolly

Compiler version

3.0.0-RC1 with Scala.JS 1.5.0

Minimized code

This reproduction requires Scala.JS 1.5.0:

import scalajs.js.UndefOr

final class X[+A] {
  def hmmm: UndefOr[A] = ???
}

I tried to make a reproduction without Scala.JS (see below) but I couldn't get the bug to occur.

package object js {
  import scala.annotation.unchecked.uncheckedVariance
  sealed trait |[A, B]
  type UndefOr[+A] = (A @uncheckedVariance) | Unit
}

import js.UndefOr

final class X[+A] {
  def hmmm: UndefOr[A] = ???
}

Output

[error] 4 |  def hmmm: UndefOr[A] = ???
[error]   |  ^^^^^^^^^^^^^^^^^^^^^^^^^^
[error]   |covariant type A occurs in invariant position in type => scala.scalajs.js.UndefOr[A] of method hmmm

Expectation

It should compile (and does with Scala 2.x) because UndefOr is covariant.

/cc @sjrd

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions