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

Inaccessible method can cause implicit conversion search to fail #11787

Closed
travisbrown opened this issue Oct 30, 2019 · 1 comment · Fixed by scala/scala#9999
Closed

Inaccessible method can cause implicit conversion search to fail #11787

travisbrown opened this issue Oct 30, 2019 · 1 comment · Fixed by scala/scala#9999
Assignees
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR implicit
Milestone

Comments

@travisbrown
Copy link

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.

@joroKr21
Copy link
Member

Can we say that #6794 is the canonical issue? There are so many problems in this area - inaccessible types inferred, inaccessible members available as implicits or escaping their definition...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) has PR implicit
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants