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
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions api/src/main/scala-3/BSONWriterInstances.scala
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,7 @@ private[bson] trait BSONWriterInstances extends BSONWriterInstancesLowPrio:
bsonUndefinedWriter,
bsonRegexWriter,
bsonJavaScriptWriter,
bsonJavaScriptWSWriter,
collectionWriter
bsonJavaScriptWSWriter
}

given tuple2Writer[A: BSONWriter, B: BSONWriter]: BSONWriter[(A, B)] =
Expand Down Expand Up @@ -141,5 +140,16 @@ end BSONWriterInstances
private[bson] sealed trait BSONWriterInstancesLowPrio {
_self: BSONWriterInstances =>

/*
* 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: https://github.com/scala/scala3/pull/19300
*/
export pkg.collectionWriter

given bsonValueIdentityWriter: BSONWriter[BSONValue] = BSONValueIdentity
}