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

Don't try default args when there are ambiguous implicits #19647

Closed
wants to merge 1 commit into from

Conversation

mbovel
Copy link
Member

@mbovel mbovel commented Feb 8, 2024

Fixes #19414.


val propFail = propagatedFailure(args)
val firstAmbiguous = args.tpes.find(_.isInstanceOf[AmbiguousImplicits])
def firstError = args.tpes.find(_.isError)
Copy link
Member Author

@mbovel mbovel Feb 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now traversing args twice in the worst case, but I think the readability gain is worth it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: as before, I am only checking for AmbiguousImplicits. Should TooUnspecific fall in the same case? I am asking because both are considered ambiguous here:

https://github.com/lampepfl/dotty/blob/4c1d58f067f40c9cd52628f195c6306b5a483d4f/compiler/src/dotty/tools/dotc/typer/Implicits.scala#L442

@mbovel mbovel requested a review from smarter February 8, 2024 14:28
@mbovel mbovel marked this pull request as ready for review February 8, 2024 14:34
Before this commit, the logic in `class Typer` / `def adapt1` / `def addImplicitArgs` was to try default arguments if *any* argument had an implicit search error that was not an ambiguous implicits error.

This commit changes the logic to try default arguments only if *all* arguments' implicit search errors are not ambiguous implicits error.

The rational is that if there is any ambiguous implicits error, it's not worth trying default arguments because this cannot solve the ambiguity. Instead, we report the first ambiguous implicits error directly.
val propFail = propagatedFailure(args)
val firstAmbiguous = args.tpes.find(_.isInstanceOf[AmbiguousImplicits])
def firstError = args.tpes.find(_.isError)
val propFail = firstAmbiguous.orElse(firstError).getOrElse(NoType)
Copy link
Member

@smarter smarter Mar 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the previous logic preferred non-ambiguity errors over ambiguity errors, and this PR changes it to prefer ambiguity errors over non-ambiguity errors, but since ambiguity is no longer special after https://dotty.epfl.ch/docs/reference/changed-features/implicit-resolution.html, perhaps we could just report the first error found? /cc @odersky

@mbovel
Copy link
Member Author

mbovel commented Apr 25, 2024

Superseded by #20261.

@mbovel mbovel closed this Apr 25, 2024
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

Successfully merging this pull request may close these issues.

Wrong error message for ambiguous given instances
2 participants