Skip to content

package-private methods ambiguate implicit conversions #12578

Closed
@armanbilge

Description

@armanbilge

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.

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