Skip to content

Strict equality is too strict in Scala 3.5 #819

@mbovel

Description

@mbovel

In Scala 3.5, assertEquals doesn't enable to compareSet of different types:

//> using dep org.scalameta::munit::1.0.1

class FooTest extends munit.FunSuite {
  test("foo") {
    assertEquals(Set(2: Any), Set(2))
  }
}
Compiling project (test, Scala 3.5.0, JVM (17))
[error] ./foo.test.scala:5:5
[error] Can't compare these two types:
[error]   First type:  Set[Any]
[error]   Second type: Set[Int]
[error] Possible ways to fix this error:
[error]   Alternative 1: provide an implicit instance for Compare[Set[Any], Set[Int]]
[error]   Alternative 2: upcast either type into `Any` or a shared supertype.
[error] I found:
[error] 
[error]     munit.Compare.compareSubtypeWithSupertype[Set[Any], Set[Int]](
[error]       /* missing */summon[Set[Any] <:< Set[Int]])
[error] 
[error] But no implicit values were found that match type Set[Any] <:< Set[Int].
[error]     assertEquals(Set(2: Any), Set(2))
[error]     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
[[syntax trees at end of                     typer]] // /Users/mbovel/munit-bug/foo.test.scala
package <empty> {
  class FooTest() extends munit.FunSuite() {
    this.test("foo")(
      {
        {
          val obtained$1: Set[Any] = Set.apply[Any]([2:Any : Any]*)
          val expected$1: Set[Int] = Set.apply[Int]([2 : Int]*)
          def clue$1: String = this.assertEquals$default$3[Set[Any], Set[Int]]
          this.assertEquals[Set[Any], Set[Int]](obtained$1, expected$1, clue$1)(
            loc = munit.Location.generate,
            munit.Compare.compareSubtypeWithSupertype[Set[Any], Set[Int]](
              /* missing */summon[Set[Any] <:< Set[Int]])
          )
        }
      }
    )
  }
}

Error compiling project (test, Scala 3.5.0, JVM (17))
Compilation failed

This is because in 3.5, the B type parameter of assertEquals is inferred to be Set[Int] instead of Set[Any] as in 3.3

assertEquals's signature:

def assertEquals[A, B](
obtained: A,
expected: B,
clue: => Any = "values are not the same"
)(implicit loc: Location, compare: Compare[A, B]): Unit = {

Instances of Compare are defined as:

trait ComparePriority1 extends ComparePriority2 {
implicit def compareSubtypeWithSupertype[A, B](implicit
ev: A <:< B
): Compare[A, B] = Compare.defaultCompare
}

trait ComparePriority2 {
implicit def compareSupertypeWithSubtype[A, B](implicit
ev: A <:< B
): Compare[B, A] = Compare.defaultCompare
}

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