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

package-private methods ambiguate implicit conversions #12578

Closed
armanbilge opened this issue Apr 12, 2022 · 3 comments
Closed

package-private methods ambiguate implicit conversions #12578

armanbilge opened this issue Apr 12, 2022 · 3 comments

Comments

@armanbilge
Copy link

armanbilge commented Apr 12, 2022

Reproduction steps

Scala version: (Is the bug only in Scala 3.x? If so, report it at https://github.com/lampepfl/dotty/issues/new/choose instead.)

//> using scala "2.13.8"

package foo {
  package object syntax {
    implicit class BarOps1(a: Any) {
      def bar(x: Int): String = ???
    }

    implicit class BarOps2(a: Any) {
      private[syntax] def bar(x: Int): String = ???
    }
  }
}

import foo.syntax._

object Main {
  def main(args: Array[String]): Unit = {
    val a = new Object
    a.bar(42)
  }
}

Problem

[error] ./bug.scala:20:5: type mismatch;
[error]  found   : a.type (with underlying type Object)
[error]  required: ?{def bar(x$1: ? >: Int(42)): ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error]  both method BarOps1 in package syntax of type (a: Any): foo.syntax.package.BarOps1
[error]  and method BarOps2 in package syntax of type (a: Any): foo.syntax.package.BarOps2
[error]  are possible conversion functions from a.type to ?{def bar(x$1: ? >: Int(42)): ?}
[error]     a.bar(42)
[error]     ^
[error] ./bug.scala:20:5: value bar is not a member of Object
[error]     a.bar(42)
[error]     ^^^^^

It's surprising that the conversion is ambiguous, since BarOps2 should not have publicly visible methods. Note also that changing to private will solve the problem.

Credit to @BalmungSan for identifying the issue while working on typelevel/cats#4183.

@joroKr21
Copy link
Member

I guess related to #6794

@armanbilge
Copy link
Author

Ah, and is a dupe of #11787.

@joroKr21
Copy link
Member

The place in Scalac is here: https://github.com/scala/scala/blob/2.13.x/src/compiler/scala/tools/nsc/typechecker/Typers.scala#L1373-L1393

It might be possible to improve by passing the expected type (pt) to adaptToMemberWithArgs and using HasMethodMatching when it's a function.

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

No branches or pull requests

2 participants