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

Open
travisbrown opened this issue Oct 30, 2019 · 1 comment
Labels
Milestone

Comments

@travisbrown
Copy link

@travisbrown travisbrown commented Oct 30, 2019

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

This comment has been minimized.

Copy link

@joroKr21 joroKr21 commented Nov 13, 2019

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...

@SethTisue SethTisue added this to the Backlog milestone Nov 14, 2019
@SethTisue SethTisue added the implicit label Nov 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
3 participants
You can’t perform that action at this time.