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

Difference in handling of context bounds and anonymous using arguments #21212

Closed
WojciechMazur opened this issue Jul 17, 2024 · 2 comments · Fixed by #21226
Closed

Difference in handling of context bounds and anonymous using arguments #21212

WojciechMazur opened this issue Jul 17, 2024 · 2 comments · Fixed by #21226
Assignees
Milestone

Comments

@WojciechMazur
Copy link
Contributor

During the minimization of the crash found in OpenCB for https://github.com/takapi327/ldbc I've observed that context bounds and anonymous using arguments are treated differently by the compiler in cases where they're mixed with named using arguments.
When using context bounds we now get an ambiguous implicit resolution, but it's not the case in when the anonymous using arguments. This behaviour seems strange since both ways of declaring implicit ends up as being synthetic implicit parameters introduced by the compiler.

Compiler version

3.6.0-RC1-bin-20240716-bbb45ca-NIGHTLY

Minimized code

trait Functor[F[_]]:
  def map[A, B](fa: F[A])(f: A => B): F[B] = ???
trait Monad[F[_]] extends Functor[F]
trait MonadError[F[_], E] extends Monad[F]:
  def raiseError[A](e: E): F[A]
trait Temporal[F[_]] extends MonadError[F, Throwable]

trait FunctorOps[F[_], A]:
  def map[B](f: A => B): F[B] = ???
implicit def toFunctorOps[F[_], A](target: F[A])(implicit tc: Functor[F]): FunctorOps[F, A] = ???

class ContextBounds[F[_]: Temporal](using err: MonadError[F, Throwable]):
  def useCase = err.raiseError(new RuntimeException())
  val bool: F[Boolean] = ???
  def fails = toFunctorOps(bool).map(_ => ())  // warns under -source:3.5, // error under -source:3.6

class UsingArguments[F[_]](using Temporal[F])(using err: MonadError[F, Throwable]):
  def useCase = err.raiseError(new RuntimeException())
  val bool: F[Boolean] = ???
  def works = toFunctorOps(bool).map(_ => ()) // warns under -source:3.5

Output

-source:3.5

[warn] ./src/main/scala/test.scala:15:33
[warn] Given search preference for Functor[F] between alternatives (ContextBounds.this.evidence$1 : Temporal[F]) and (ContextBounds.this.err : MonadError[F, Throwable]) will change
[warn] Current choice           : the first alternative
[warn] New choice from Scala 3.6: none - it's ambiguous
[warn]   def fails = toFunctorOps(bool).map(_ => ())  // warns under -source:3.5, // error under -source:3.6
[warn]                                 ^
[warn] ./src/main/scala/test.scala:20:33
[warn] Given search preference for Functor[F] between alternatives (UsingArguments.this.x$1 : Temporal[F]) and (UsingArguments.this.err : MonadError[F, Throwable]) will change
[warn] Current choice           : the first alternative
[warn] New choice from Scala 3.6: the second alternative
[warn]   def works = toFunctorOps(bool).map(_ => ()) // warns under -source:3.5
[warn]                                 ^

-source:3.6

[error] ./src/main/scala/test.scala:15:33
[error] Ambiguous given instances: both value evidence$1 in class ContextBounds and given instance err in class ContextBounds match type Functor[F] of parameter tc of method toFunctorOps
[error]   def fails = toFunctorOps(bool).map(_ => ())  // warns under -source:3.5, // error under -source:3.6
[error]                

Expectation

There might be a hole in implicit resolution logic that should be fixed.

@EugeneFlesselle
Copy link
Contributor

EugeneFlesselle commented Jul 18, 2024

Minimization:

trait A
trait B extends A

def test1(using a1: A)(using    b1: B) = summon[A] // ok: picks (most general) a1
def test2(using a2: A)(implicit b2: B) = summon[A] // error: ambiguous
def test3(implicit       a3: A, b3: B) = summon[A] // ok: picks (most specific) b3

@EugeneFlesselle
Copy link
Contributor

EugeneFlesselle commented Jul 18, 2024

Note that, as we can see in test3, the change in rules for given prioritization #19300 does not apply to implicit (as opposed to using) parameters, which is what context bounds expand to. In effect:

trait A[X]
trait B[X] extends A[X]

def test[X : A/* as a*/ : B/* as b*/] = summon[A[X]] // still picks b

does not pick the most general.

EugeneFlesselle added a commit to dotty-staging/dotty that referenced this issue Jul 18, 2024
EugeneFlesselle added a commit to dotty-staging/dotty that referenced this issue Jul 19, 2024
to apply the logic prioritizing givens over implicits as intended in scala#19300

Fix scala#21212
WojciechMazur pushed a commit to WojciechMazur/dotty that referenced this issue Jul 24, 2024
to apply the logic prioritizing givens over implicits as intended in scala#19300

Fix scala#21212
WojciechMazur pushed a commit to WojciechMazur/dotty that referenced this issue Jul 24, 2024
to apply the logic prioritizing givens over implicits as intended in scala#19300

Fix scala#21212
@WojciechMazur WojciechMazur added this to the 3.6.0 milestone Oct 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants