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

Wrong warning emitted during implicit resolution #20484

Closed
hamzaremmal opened this issue May 28, 2024 · 2 comments · Fixed by #20487
Closed

Wrong warning emitted during implicit resolution #20484

hamzaremmal opened this issue May 28, 2024 · 2 comments · Fixed by #20487
Labels
area:implicits related to implicits area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement

Comments

@hamzaremmal
Copy link
Member

Compiler version

current main branch (d3df8ca)

Minimized example

given Int = ???
given Char = ???
val a = summon[Int]

Output Error/Warning message

> scala test.scala
-- Warning: test.scala:3:19 ----------------------------------------------------
3 |val a = summon[Int]
  |                   ^
  |Given search preference for Int between alternatives (given_Int : Int) and (given_Char : Char) will change
  |Current choice           : the second alternative
  |New choice from Scala 3.6: the first alternative
1 warning found

Why this Error/Warning was not helpful

This warning should not be triggered at the first place. When printing the ast after typer, we can see that it resolved to the correct given. We can see that the compiler will emit the following code:

> scalac -Xprint:typer test.scala
-- Warning: test.scala:3:19 ----------------------------------------------------
3 |val a = summon[Int]
  |                   ^
  |Given search preference for Int between alternatives (given_Int : Int) and (given_Char : Char) will change
  |Current choice           : the second alternative
  |New choice from Scala 3.6: the first alternative
[[syntax trees at end of                     typer]] // test.scala
package <empty> {
  final lazy module val test$package: test$package = new test$package()
  final module class test$package() extends Object() {
    this: test$package.type =>
    final lazy given val given_Int: Int = ???
    final lazy given val given_Char: Char = ???
    val a: Int = given_Int
  }
}

We observe the correct behaviour if we set the source version to 3.4 or 3.6

> scalac -source 3.4 test.scala
[[syntax trees at end of                     typer]] // test.scala
package <empty> {
  final lazy module val test$package: test$package = new test$package()
  final module class test$package() extends Object() {
    this: test$package.type =>
    final lazy given val given_Int: Int = ???
    final lazy given val given_Char: Char = ???
    val a: Int = given_Int
  }
}
> scalac -source 3.6 test.scala
[[syntax trees at end of                     typer]] // test.scala
package <empty> {
  final lazy module val test$package: test$package = new test$package()
  final module class test$package() extends Object() {
    this: test$package.type =>
    final lazy given val given_Int: Int = ???
    final lazy given val given_Char: Char = ???
    val a: Int = given_Int
  }
}

Suggested improvement

The warning should not be emitted at the first place.

@hamzaremmal hamzaremmal added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc area:implicits related to implicits better-errors Issues concerned with improving confusing/unhelpful diagnostic messages labels May 28, 2024
@hamzaremmal hamzaremmal changed the title Wrong warning emited during implicit resolution Wrong warning emitted during implicit resolution May 28, 2024
@hamzaremmal
Copy link
Member Author

Swapping the definition of the givens doesn't emit the warning:

given Char = ???
given Int = ???
val a = summon[Int]

@hamzaremmal
Copy link
Member Author

Discussion here: #20480

odersky added a commit to dotty-staging/dotty that referenced this issue May 28, 2024
hamzaremmal added a commit that referenced this issue May 28, 2024
Warn about priority change in implicit search only if one of the
participating candidates appears in the final result.

It could be that we have an priority change between two ranked
candidates that both are superseded by the result of the implicit
search. In this case, no warning needs to be reported.

This PR is #20480 with different code for the last commit. I tried to
avoid entangling the priority handling too much in the returns types and
spent a side effect instead. I believe it's more efficient that way,
since priority warnings are very rare.

Fixes #20484
WojciechMazur pushed a commit to WojciechMazur/dotty that referenced this issue Jul 2, 2024
WojciechMazur pushed a commit to WojciechMazur/dotty that referenced this issue Jul 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:implicits related to implicits area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant