Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve ambigious implicits under new Scala 3.6 givens prioritization schema #415

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

WojciechMazur
Copy link
Contributor

@WojciechMazur WojciechMazur commented Jul 19, 2024

Pull Request Checklist

  • Have you read How to write the perfect pull request?
  • Have you read through the contributor guidelines?
  • Have you added tests for any changed functionality?
    Test already existed MacroSpec from the logs below. Tested locally using nightly version of Scala 3 compiler: 3.6.0-RC1-bin-20240718-bd84584-NIGHTLY

Fixes

Fixes usage of reactivemongo-bson for Scala 3.6+ users

Since Scala 3.5 or when using -source:3.5 we'd get a warning:

[warn] -- Warning: /Users/wmazur/projects/community-build3/repo/api/src/test/scala/MacroSpec.scala:1419:34 
[warn] 1419 |        val writer = Macros.writer[WithMap1]
[warn]      |                     ^^^^^^^^^^^^^^^^^^^^^^^
[warn]      |Given search preference for reactivemongo.api.bson.BSONWriter[Map[java.util.Locale, String]] between alternatives (reactivemongo.api.bson.BSONWriter.mapKeyWriter :
[warn]      |  [K, V]
[warn]      |    (using x$1: reactivemongo.api.bson.KeyWriter[K], x$2:
[warn]      |      reactivemongo.api.bson.BSONWriter[V]):
[warn]      |      reactivemongo.api.bson.BSONDocumentWriter[Map[K, V]]
[warn]      |) and (reactivemongo.api.bson.BSONWriter.collectionWriter :
[warn]      |  [T, Repr <: Iterable[T]]
[warn]      |    (using x$1: reactivemongo.api.bson.BSONWriter[T], x$2: Repr ¬ Option[T]):
[warn]      |      reactivemongo.api.bson.BSONWriter[Repr]
[warn]      |) will change
[warn]      |Current choice           : the first alternative
[warn]      |New choice from Scala 3.6: none - it's ambiguous

Since 3.6 it becomes a plain error about missing implicit

Purpose

This change resolves ambiguous implicit that are reported under the new givens prioritization betwen mapWriter and collectionWriter affecting Scala 3.6+ users.

It resolves compilation issues for the dependant projects mentioned here what the Scala 3 Open Community Build identified as failing due to the changes in compiler, especially https://github.com/lichess-org/lila and https://github.com/dapperware/dappermongo

Background Context

Since Scala 3.6 the order of givens prioritization have changed - compiler can choose the most general implicit instead of the most specific. For example when searching for Writer[Map[String, T]]

  • Scala 3.5 chooses mapWriter: BSONDocumentWriter[Map[String, V]] instead of collectionWriter[T, Repr <: Iterable[T]]: BSONWriter[Repr] as it's more specyfic
  • Scala 3.6 sees both of the candidates as equally good leading to ambigious implicit search result

To prevent this issue for Scala 3.6+ users more generalized writers should be put in the low-priority trait to workaround these changes. It would have no effect for Scala 3.5- users since the more specific implicit would choosen anyway
See Scala 3 PR that introduced this change for more info: scala/scala3#19300

I kindly request a timely release of a new version of ReactiveMongo-BSON after this change is merged to unblock dependant projects.

@cchantep
Copy link
Member

Thx. Will have a look

@cchantep
Copy link
Member

As the 3.5 and 3.6 are still in RC, and things can change meanwhile it released, I think it won't be merge for now.
Thanks.

@WojciechMazur
Copy link
Contributor Author

Actually it's not going to change now. Cutoff for 3.5.0 for almost 2 months ago (although it was followed by multipe RCs and fixes to critical issue), and 3.5.1 was cut off 2 weeks ago.
Scala 3 compiler releases are following a release procedure in which at the day of cutoff we create a new release branch from which the RC releases are produced and contains only backports for issues found in the RCs. From that point tooling and core projects can test it. At the same time the development continues in the main branch. While we're waiting for 3.5.0 final release (going to be released next week) current development cycle prepares 3.6.0

@cchantep cchantep merged commit 7923c0f into ReactiveMongo:master Nov 6, 2024
1 check passed
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