@@ -3869,12 +3869,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
38693869
38703870 def addImplicitArgs (using Context ) =
38713871 def hasDefaultParams = methPart(tree).symbol.hasDefaultParams
3872- def findDefaultArgument (argIndex : Int ): Tree =
3873- def appPart (t : Tree ): Tree = t match
3874- case Block (_, expr) => appPart(expr)
3875- case Inlined (_, _, expr) => appPart(expr)
3876- case t => t
3877- defaultArgument(appPart(tree), n = argIndex, testOnly = false )
38783872 def implicitArgs (formals : List [Type ], argIndex : Int , pt : Type ): List [Tree ] = formals match
38793873 case Nil => Nil
38803874 case formal :: formals1 =>
@@ -3889,40 +3883,27 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
38893883 implicitArgs(formals2, argIndex + 1 , pt)
38903884
38913885 val arg = inferImplicitArg(formal, tree.span.endPos)
3892-
3893- lazy val defaultArg = findDefaultArgument(argIndex)
3894- .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
3895- def argHasDefault = hasDefaultParams && ! defaultArg.isEmpty
3896-
3897- def canProfitFromMoreConstraints =
3898- arg.tpe.isInstanceOf [AmbiguousImplicits ]
3899- // Ambiguity could be decided by more constraints
3900- || ! isFullyDefined(formal, ForceDegree .none) && ! argHasDefault
3901- // More context might constrain type variables which could make implicit scope larger.
3902- // But in this case we should search with additional arguments typed only if there
3903- // is no default argument.
3904-
3905- // Try to constrain the result using `pt1`, but back out if a BadTyperStateAssertion
3906- // is thrown. TODO Find out why the bad typer state arises and prevent it. The try-catch
3907- // is a temporary hack to keep projects compiling that would fail otherwise due to
3908- // searching more arguments to instantiate implicits (PR #23532). A failing project
3909- // is described in issue #23609.
3910- def tryConstrainResult (pt : Type ): Boolean =
3911- try constrainResult(tree.symbol, wtp, pt)
3912- catch case ex : TyperState .BadTyperStateAssertion => false
3913-
3914- arg.tpe match
3915- case failed : SearchFailureType if canProfitFromMoreConstraints =>
3916- val pt1 = pt.deepenProtoTrans
3917- if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && tryConstrainResult(pt1) then
3918- return implicitArgs(formals, argIndex, pt1)
3919- case _ =>
3920-
39213886 arg.tpe match
39223887 case failed : AmbiguousImplicits =>
3923- arg :: implicitArgs(formals1, argIndex + 1 , pt)
3888+ val pt1 = pt.deepenProtoTrans
3889+ if (pt1 `ne` pt) && (pt1 ne sharpenedPt) && constrainResult(tree.symbol, wtp, pt1)
3890+ then implicitArgs(formals, argIndex, pt1)
3891+ else arg :: implicitArgs(formals1, argIndex + 1 , pt1)
39243892 case failed : SearchFailureType =>
3925- if argHasDefault then
3893+ lazy val defaultArg =
3894+ def appPart (t : Tree ): Tree = t match
3895+ case Block (stats, expr) => appPart(expr)
3896+ case Inlined (_, _, expr) => appPart(expr)
3897+ case _ => t
3898+ defaultArgument(appPart(tree), argIndex, testOnly = false )
3899+ .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
3900+ if ! hasDefaultParams || defaultArg.isEmpty then
3901+ // no need to search further, the adapt fails in any case
3902+ // the reason why we continue inferring arguments in case of an AmbiguousImplicits
3903+ // is that we need to know whether there are further errors.
3904+ // If there are none, we have to propagate the ambiguity to the caller.
3905+ arg :: formals1.map(dummyArg)
3906+ else
39263907 // This is tricky. On the one hand, we need the defaultArg to
39273908 // correctly type subsequent formal parameters in the same using
39283909 // clause in case there are parameter dependencies. On the other hand,
@@ -3933,12 +3914,6 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
39333914 // `if propFail.exists` where we re-type the whole using clause with named
39343915 // arguments for all implicits that were found.
39353916 arg :: inferArgsAfter(defaultArg)
3936- else
3937- // no need to search further, the adapt fails in any case
3938- // the reason why we continue inferring arguments in case of an AmbiguousImplicits
3939- // is that we need to know whether there are further errors.
3940- // If there are none, we have to propagate the ambiguity to the caller.
3941- arg :: formals1.map(dummyArg)
39423917 case _ =>
39433918 arg :: inferArgsAfter(arg)
39443919 end implicitArgs
0 commit comments