Skip to content

really flakey implicit resolution #5978

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

Closed
drdozer opened this issue Feb 23, 2019 · 2 comments
Closed

really flakey implicit resolution #5978

drdozer opened this issue Feb 23, 2019 · 2 comments

Comments

@drdozer
Copy link

drdozer commented Feb 23, 2019

Consider:

import scala.language.implicitConversions

opaque type Position[Buffer] = Int


trait TokenParser[Token, R]

object TextParser {
  implied TP for TokenParser[Char, Position[CharSequence]] {}

  implied FromCharToken
    given (T: TokenParser[Char, Position[CharSequence]]) for Conversion[Char, Position[CharSequence]] = ???

}


object Testcase {
  def main(args: Array[String]): Unit = {
    import TextParser._

    val tp_v: TokenParser[Char, Position[CharSequence]] = TextParser.TP // compiles
    val tp_i = infer[TokenParser[Char, Position[CharSequence]]] // does not compile
    val co_i = infer[Conversion[Char, Position[CharSequence]]] // does not compile
    val co_x : Position[CharSequence] = 'x'

    {
      implied XXX for Conversion[Char, Position[CharSequence]] = co_i
      val co_y : Position[CharSequence] = 'x' // compiles
    }

  }
}

This fails, as commented. However, if I add other seemingly unrelated statements, some or all of it may compile. I can find no rhyme or reason to it. If I make the implied declarations top-level, it works. In other situations, the statment co_x only works if the anonymous block following is present. If I comment that block out, then co_x no longer works. Again, I have no idea what I've done to the surrounding codebase to trigger this weird bug.

If I had to guess, I'd say there was 1. an issue with implied instance visibility, and 2. some issue with expressions being typed more completely in some situations than others. But honestly, I'm mystified. Some issues appear to empirically go away if I move everything to top-level declarations, but that is messy from a scoping point of view.

odersky added a commit to dotty-staging/dotty that referenced this issue Feb 23, 2019
@odersky
Copy link
Contributor

odersky commented Feb 23, 2019

You need to use an import implied. Normal imports don't bring implied instances into scope. I have fixed the test case in #5978.

@odersky odersky closed this as completed Feb 23, 2019
@drdozer
Copy link
Author

drdozer commented Feb 23, 2019

Oh wow! That could explain a lot of weirdness I've been experiencing, particularly if random statements were somehow pulling the implied instances that weren't explicitly imported into scope. I'll hunt for better test cases.

odersky added a commit that referenced this issue Feb 24, 2019
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