ScalaMock icon indicating copy to clipboard operation
ScalaMock copied to clipboard

Update scala 3 version

Open hughsimpson opened this issue 6 months ago • 8 comments

Pull Request Checklist

  • [x] I agree to licence my contributions under the MIT licence
  • [x] I have added copyright headers to new files -- No new files
  • [ ] I have added tests for any changed functionality -- Not done, see comment under 'Purpose' section

Welp, embarrassingly, fixes nothing directly. It turns out that the scala bump was... not why this started working locally in downstream. Once I figured out why my local scala 3.3.0 version was acting janky, it turns out that actually my reproduction started working because the fix had been merged to scalamock since the 6.0.0 version was released 🤦.

I'm gonna leave this open for now in case there's any interest in bumping the scala version, but I would understand if this were closed as a wontfix 😅 . Original comment below:

Fixes

Usage in downstream projects on scala >= 3.3.1 fails to mock classes with parameterised constructor args. Minimal reproduction: build.sbt:

scalaVersion := "3.3.3"
libraryDependencies ++= Seq(
  "org.scalamock" %% "scalamock" % "6.0.0" % Test,
  "org.specs2" %% "specs2-core" % "4.20.8" % Test
)

src/test/scala/reproduce_bug/MockabilityTest.scala:

package reproduce_bug

import org.scalamock.specs2.IsolatedMockFactory
import org.specs2.mutable.Specification

class MockabilityTest extends Specification with IsolatedMockFactory {

  "MockableSubclass utils" >> {
    val ok: Unmockable = mock[Unmockable]
    classOf[Unmockable].isAssignableFrom(ok.getClass) must beTrue
  }
}

case class HasParam[T](t: T)

case class Unmockable(a: String, b: Int, c: HasParam[Double]) {
  def foo: String = "foo"
}

Produces error

[error] -- [E007] Type Mismatch Error: /Users/hughsimpson/Sandbox/Play/reproduce_bug/src/test/scala/reproduce_bug/MockabilityTest.scala:10:25
[error] 10 |    val ok: Unmockable = mock[Unmockable]
[error]    |                         ^^^^^^^^^^^^^^^^
[error]    |                         Found:    reproduce_bug.HasParam
[error]    |                         Required: reproduce_bug.HasParam[Double]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] one error found

Purpose

Upgrading the scala version with which the library is built fixes this bug downstream in the above minimal reproduction. Writing the code as a test within the project does not prompt the bug so I have not attempted to add it here.

The reproduction test passes with the following updated build.sbt (where 6.0.1-LOCAL is a locally published version of this branch):

scalaVersion := "3.4.0"
libraryDependencies ++= Seq(
  "org.scalamock" %% "scalamock" % "6.0.1-LOCAL" % Test,
  "org.specs2" %% "specs2-core" % "4.20.8" % Test
)
Test / scalacOptions += "-experimental"

No changes were required for the test file.

Background Context

The above failure appears to be a bug with scala 3.3.0 (possibly with TASTy? I don't know anything about the scalac internals). The earliest minor scala version that supports the -experimental scalacOption is 3.4.0; to avoid having to annotate everything with @experimental I've gone for bumping the scalaVersion to beyond the LTS release.

I don't know whether this bug would be present in a project that stuck to scala 3.3.0 specifically, since when attempting to use that specific version I get:

[error] error while loading ElementType,
[error] class file /modules/java.base/java/lang/annotation/ElementType.class is broken, reading aborted with class java.lang.RuntimeException
[error] bad constant pool index: 0 at pos: 1220

on the bug reproduction

hughsimpson avatar Aug 01 '24 10:08 hughsimpson