Skip to content

"Ambiguous implicit values" error caused by a mix of -Xsource:3, an explicit (not wildcard) import and a evicted/transitive(?) dependency #12350

@hmemcpy

Description

@hmemcpy

We've hit a strange issue today, and it took me a while to track down, but I was able to reproduce it. Here's an example of the issue on Scastie: https://scastie.scala-lang.org/hmemcpy/PLSmFypOQ0arLZmTb50dfQ/7

It boils down to the following combination: compiling with -Xsource:3, a non-wildcard import, and referencing a dependency directly and indirectly via another one. I was able to reproduce this with the ZIO library. Given the conditions mentioned, the following code snippet doesn't compile:

import zio.ZIO

val nextRand = zio.random.nextDoubleBetween(0, 1)
ZIO.collectAll(List.fill(3)(nextRand))

failing with the following error:

ambiguous implicit values:
 both method buildFromAny in trait BuildFromCompat of type [Element, Collection[+Element] <: Iterable[Element] with scala.collection.IterableOps[Any,Collection,Any]]zio.package.BuildFrom[Collection[Any],Element,Collection[Element]]
 and method buildFromIterableOps in trait BuildFromLowPriority2 of type [CC[X] <: Iterable[X] with scala.collection.IterableOps[X, CC, _], A0, A]scala.collection.BuildFrom[CC[A0],A,CC[A]]
 match expected type zio.BuildFrom[List[zio.ZIO[zio.random.Random,Nothing,Double]],Double,List[Double]] [56:23]

reproduction steps

using Scala 2.13.4 (and 2.13.5), and the following sbt definition:

lazy val zioVersion = "1.0.4-2"
lazy val zioConfigVersion = "1.0.0"

ThisBuild / scalaVersion := "2.13.4"

lazy val root = project
  .in(file("."))
  .settings(
    libraryDependencies ++= Seq(
      "dev.zio" %% "zio" % zioVersion,
      "dev.zio" %% "zio-config" % zioConfigVersion,
    ),
    scalacOptions ++= Seq(
       "-Xsource:3"
    ),
  )

Adding a file with the code snippet above causes it to fail with "ambiguous implicit values" error. Enabling -Ydebug revealed additional errors:

[ suppressed ] Cannot construct a collection of type immutable.this.List[scala.this.Double] with elements of type scala.this.Double based on a collection of type immutable.this.List[zio.this.ZIO[<root>.this.zio.random.Random,scala.this.Nothing,scala.this.Double]].
  ZIO.collectAll(List.fill(3)(nextRand))

[ suppressed ] not enough arguments for method collectAll: (implicit bf: zio.this.BuildFrom[immutable.this.List[zio.this.ZIO[<root>.this.zio.random.Random,scala.this.Nothing,scala.this.Double]],scala.this.Double,immutable.this.List[scala.this.Double]]): zio.this.ZIO[<root>.this.zio.random.Random,scala.this.Nothing,immutable.this.List[scala.this.Double]].
Unspecified value parameter bf.
  ZIO.collectAll(List.fill(3)(nextRand))

problem analysis

The ZIO.collectAll function requires an implicit BuildFrom, and provides a conversion in the zio.BuildFromCompat trait.

The dependency zio-config depends itself on ZIO v1.4.0-2. However, when both are referenced - it causes the compilation error (even though I'm not using anything from zio-config directly). Removing zio-config from libraryDependencies does not cause the issue (I initially thought this had to do with a mismatched ZIO version being evicted, but was able to confirm this not working even with "matching" versions).

workaround

When either removing -Xsource:3 OR removing zio-config OR importing zio._ instead of just zio.ZIO - the code compiles without problems!

I'm not sure who's the culprit here...

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