@@ -4362,11 +4362,17 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4362
4362
4363
4363
val arg = inferImplicitArg(formal, tree.span.endPos)
4364
4364
4365
+ lazy val defaultArg = findDefaultArgument(argIndex)
4366
+ .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
4367
+ def argHasDefault = hasDefaultParams && ! defaultArg.isEmpty
4368
+
4365
4369
def canProfitFromMoreConstraints =
4366
4370
arg.tpe.isInstanceOf [AmbiguousImplicits ]
4367
- // ambiguity could be decided by more constraints
4368
- || ! isFullyDefined(formal, ForceDegree .none)
4369
- // more context might constrain type variables which could make implicit scope larger
4371
+ // Ambiguity could be decided by more constraints
4372
+ || ! isFullyDefined(formal, ForceDegree .none) && ! argHasDefault
4373
+ // More context might constrain type variables which could make implicit scope larger.
4374
+ // But in this case we should search with additional arguments typed only if there
4375
+ // is no default argument.
4370
4376
4371
4377
arg.tpe match
4372
4378
case failed : SearchFailureType if canProfitFromMoreConstraints =>
@@ -4379,15 +4385,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4379
4385
case failed : AmbiguousImplicits =>
4380
4386
arg :: implicitArgs(formals1, argIndex + 1 , pt)
4381
4387
case failed : SearchFailureType =>
4382
- lazy val defaultArg = findDefaultArgument(argIndex)
4383
- .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
4384
- if ! hasDefaultParams || defaultArg.isEmpty then
4385
- // no need to search further, the adapt fails in any case
4386
- // the reason why we continue inferring arguments in case of an AmbiguousImplicits
4387
- // is that we need to know whether there are further errors.
4388
- // If there are none, we have to propagate the ambiguity to the caller.
4389
- arg :: formals1.map(dummyArg)
4390
- else
4388
+ if argHasDefault then
4391
4389
// This is tricky. On the one hand, we need the defaultArg to
4392
4390
// correctly type subsequent formal parameters in the same using
4393
4391
// clause in case there are parameter dependencies. On the other hand,
@@ -4398,6 +4396,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
4398
4396
// `if propFail.exists` where we re-type the whole using clause with named
4399
4397
// arguments for all implicits that were found.
4400
4398
arg :: inferArgsAfter(defaultArg)
4399
+ else
4400
+ // no need to search further, the adapt fails in any case
4401
+ // the reason why we continue inferring arguments in case of an AmbiguousImplicits
4402
+ // is that we need to know whether there are further errors.
4403
+ // If there are none, we have to propagate the ambiguity to the caller.
4404
+ arg :: formals1.map(dummyArg)
4401
4405
case _ =>
4402
4406
arg :: inferArgsAfter(arg)
4403
4407
end implicitArgs
0 commit comments