Skip to content

Commit 863077c

Browse files
committed
Bring back ambiguity filter when we report an implicit not found error
This reverts one part of #20261. When we fail with both an ambiguity on one implicit argument and another error on another argument we prefer the other error. I added a comment why this is needed. Fixes #20344
1 parent 1276034 commit 863077c

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+8-1
Original file line numberDiff line numberDiff line change
@@ -4113,7 +4113,14 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41134113
* `SearchFailureType`.
41144114
*/
41154115
def issueErrors(fun: Tree, args: List[Tree]): Tree =
4116-
def firstFailure = args.tpes.find(_.isInstanceOf[SearchFailureType]).getOrElse(NoType)
4116+
// Prefer other errors over ambiguities. If nested in outer searches a missing
4117+
// implicit can be healed by simply dropping this alternative and tryng something
4118+
// else. But an ambiguity is sticky and propagates outwards. If we have both
4119+
// a missing implicit on one argument and an ambiguity on another the whole
4120+
// branch should be classified as a missing implicit.
4121+
val firstNonAmbiguous = args.tpes.find(tp => tp.isError && !tp.isInstanceOf[AmbiguousImplicits])
4122+
def firstError = args.tpes.find(_.isInstanceOf[SearchFailureType]).getOrElse(NoType)
4123+
def firstFailure = firstNonAmbiguous.getOrElse(firstError)
41174124
val errorType =
41184125
firstFailure match
41194126
case tp: AmbiguousImplicits =>

Diff for: tests/pos/i20344.scala

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
trait Monad[F[_]] extends Invariant[F]
2+
3+
trait Invariant[F[_]]
4+
object Invariant:
5+
implicit def catsInstancesForList: Monad[List] = ???
6+
implicit def catsInstancesForVector: Monad[Vector] = ???
7+
8+
trait Shrink[T]
9+
object Shrink extends ShrinkLowPriorityImplicits:
10+
trait Buildable[T,C]
11+
implicit def shrinkContainer[C[_],T](implicit v: C[T] => Traversable[T], s: Shrink[T], b: Buildable[T,C[T]]): Shrink[C[T]] = ???
12+
trait ShrinkLowPriorityImplicits:
13+
implicit def shrinkAny[T]: Shrink[T] = ???
14+
15+
trait Distribution[F[_], -P, X] extends (P => F[X])
16+
type GenBeta[A, B, X] = [F[_]] =>> Distribution[F, Beta.Params[A, B], X]
17+
type Beta[R] = [F[_]] =>> GenBeta[R, R, R][F]
18+
19+
object Beta:
20+
trait Params[+A, +B]
21+
trait BetaInstances:
22+
given schrodingerRandomBetaForDouble[F[_]: Monad]: Beta[Double][F] = ???
23+
24+
object all extends BetaInstances
25+
26+
@main def Test =
27+
import all.given
28+
summon[Shrink[Beta.Params[Double, Double]]]

0 commit comments

Comments
 (0)