Skip to content

Inaccessible method can cause implicit conversion search to fail #11787

@travisbrown

Description

@travisbrown

This code doesn't compile:

object syntax {
  implicit class Ops1(x: String) { private[syntax] def bar: Int = 1 }
  implicit class Ops2(x: String) { def bar: Int = 2 }
}

object test {
  import syntax._
  val result = "foo".bar
}

The error message says there's an ambiguity:

[error] /home/travis/tmp/so/syntax/src/main/scala/test.scala:9:16: type mismatch;
[error]  found   : String("foo")
[error]  required: ?{def bar: ?}
[error] Note that implicit conversions are not applicable because they are ambiguous:
[error]  both method Ops1 in object syntax of type (x: String)syntax.Ops1
[error]  and method Ops2 in object syntax of type (x: String)syntax.Ops2
[error]  are possible conversion functions from String("foo") to ?{def bar: ?}
[error]   val result = "foo".bar
[error]                ^
[error] /home/travis/tmp/so/syntax/src/main/scala/test.scala:9:22: value bar is not a member of String
[error]   val result = "foo".bar
[error]                      ^
[error] two errors found

The following also fails (as expected):

object syntax {
  implicit class Ops1(x: String) { private[syntax] def bar: Int = 1 }
}

object test {
  import syntax._
  val result = "foo".bar
}

Because:

[error] /home/travis/tmp/so/syntax/src/main/scala/test.scala:9:22: method bar in class Ops1 cannot be accessed in syntax.Ops1
[error]   val result = "foo".bar
[error]                      ^
[error] one error found

So a method that isn't actually a candidate for the implicit conversion can still cause the search to fail because of ambiguity.

I've confirmed that the code above doesn't compile on 2.11.12, 2.12.10, and 2.13.1. The problem looks vaguely familiar to me but I couldn't find a previous report.

Metadata

Metadata

Assignees

Labels

fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)has PRimplicit

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions