Skip to content

Some fixes for Scala 3 mock-method metadata#641

Merged
TimvdLippe merged 1 commit into
mockito:release/2.xfrom
jozic:2026-03-26-macro-common-improvements
Mar 27, 2026
Merged

Some fixes for Scala 3 mock-method metadata#641
TimvdLippe merged 1 commit into
mockito:release/2.xfrom
jozic:2026-03-26-macro-common-improvements

Conversation

@jozic

@jozic jozic commented Mar 27, 2026

Copy link
Copy Markdown
Contributor

I was testing these on real projects and figured out some issues for Scala 3 mock-method metadata implementation: overloaded methods conflict, storing primitives in value classes cache. Also added some docs explaining how mock method should be wrapped (if needed) in Scala 3

More details

  1. Overload dedup by symbol, not fullName.
    In Scala 3, all overloads of a method share the same Symbol.fullName. The old mutable.Set[String] dedup kept only the first overload encountered, silently dropping e.g. a vararg overload that shared a name with plain overloads. Changed to mutable.Set[Symbol] — each overload is a distinct symbol, so all survive.

  2. isPrimitiveType guard in returnsValueClassFor.
    Int <:< AnyVal is true at Scala's type level, so without an explicit check, returnsValueClassFor(TypeRepr.of[Int]) would return true and primitive-returning methods would be wrongly stored as value-class returns. The guard prevents false positives.

  3. Only true entries go into the returnsValueClass cache.
    The old code stored (method, false) for every method; the new code stores only true entries. getReturnsValueClass returning None is the same signal as false — the cache can be smaller and the intent is clearer.

  4. ReflectionUtils.returnsValueClass — remove broken fallback
    The old fallback classOf[AnyVal].isAssignableFrom(returnType) compiled to classOf[Object].isAssignableFrom(returnType) in Scala 3, returning true for every reference type. With only-true cache entries this fallback is also unnecessary — getOrElse(false) is correct and safe.

  5. MockCreator scaladoc + note in README — inline wrapper requirement explained

plus new tests to verify the fixes

@jozic

jozic commented Mar 27, 2026

Copy link
Copy Markdown
Contributor Author

@TimvdLippe please check this out. Thanks!

@jozic jozic marked this pull request as ready for review March 27, 2026 04:02

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice comments! Despite my minimal Scala knowledge, I was able to understand why these fixes were required.

@TimvdLippe TimvdLippe merged commit 16dbf46 into mockito:release/2.x Mar 27, 2026
5 checks passed
@jozic jozic deleted the 2026-03-26-macro-common-improvements branch March 27, 2026 10:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants