@@ -36,7 +36,6 @@ import util.{Property, SimpleIdentityMap, SrcPos}
3636import Applications .{tupleComponentTypes , wrapDefs , defaultArgument }
3737
3838import collection .mutable
39- import annotation .tailrec
4039import Implicits .*
4140import util .Stats .record
4241import config .Printers .{gadts , typr }
@@ -52,7 +51,8 @@ import config.Config
5251import config .MigrationVersion
5352import transform .CheckUnused .OriginalName
5453
55- import scala .annotation .constructorOnly
54+ import scala .annotation .{unchecked as _ , * }
55+ import scala .util .chaining .given
5656
5757object Typer {
5858
@@ -4188,6 +4188,12 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
41884188
41894189 def addImplicitArgs (using Context ) =
41904190 def hasDefaultParams = methPart(tree).symbol.hasDefaultParams
4191+ def findDefaultArgument (argIndex : Int ): Tree =
4192+ def appPart (t : Tree ): Tree = t match
4193+ case Block (_, expr) => appPart(expr)
4194+ case Inlined (_, _, expr) => appPart(expr)
4195+ case t => t
4196+ defaultArgument(appPart(tree), n = argIndex, testOnly = false )
41914197 def implicitArgs (formals : List [Type ], argIndex : Int , pt : Type ): List [Tree ] = formals match
41924198 case Nil => Nil
41934199 case formal :: formals1 =>
@@ -4209,13 +4215,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42094215 then implicitArgs(formals, argIndex, pt1)
42104216 else arg :: implicitArgs(formals1, argIndex + 1 , pt1)
42114217 case failed : SearchFailureType =>
4212- lazy val defaultArg =
4213- def appPart (t : Tree ): Tree = t match
4214- case Block (stats, expr) => appPart(expr)
4215- case Inlined (_, _, expr) => appPart(expr)
4216- case _ => t
4217- defaultArgument(appPart(tree), argIndex, testOnly = false )
4218- .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
4218+ lazy val defaultArg = findDefaultArgument(argIndex)
4219+ .showing(i " default argument: for $formal, $tree, $argIndex = $result" , typr)
42194220 if ! hasDefaultParams || defaultArg.isEmpty then
42204221 // no need to search further, the adapt fails in any case
42214222 // the reason why we continue inferring arguments in case of an AmbiguousImplicits
@@ -4237,44 +4238,44 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42374238 arg :: inferArgsAfter(arg)
42384239 end implicitArgs
42394240
4240- /** Reports errors for arguments of `appTree` that have a
4241- * `SearchFailureType`.
4242- */
4243- def issueErrors (fun : Tree , args : List [Tree ]): Tree =
4244- // Prefer other errors over ambiguities. If nested in outer searches a missing
4245- // implicit can be healed by simply dropping this alternative and trying something
4246- // else. But an ambiguity is sticky and propagates outwards. If we have both
4247- // a missing implicit on one argument and an ambiguity on another the whole
4248- // branch should be classified as a missing implicit.
4249- val firstNonAmbiguous = args.tpes.find(tp => tp.isError && ! tp.isInstanceOf [AmbiguousImplicits ])
4250- def firstError = args.tpes.find(_.isInstanceOf [SearchFailureType ]).getOrElse(NoType )
4251- def firstFailure = firstNonAmbiguous.getOrElse(firstError)
4252- val errorType =
4253- firstFailure match
4254- case tp : AmbiguousImplicits =>
4255- AmbiguousImplicits (tp.alt1, tp.alt2, tp.expectedType, tp.argument, nested = true )
4256- case tp =>
4257- tp
4258- val res = untpd.Apply (fun, args).withType(errorType)
4259-
4260- wtp.paramNames.lazyZip(wtp.paramInfos).lazyZip(args).foreach { (paramName, formal, arg) =>
4261- arg.tpe match
4262- case failure : SearchFailureType =>
4263- val methodStr = err.refStr(methPart(fun).tpe)
4264- val paramStr = implicitParamString(paramName, methodStr, fun)
4265- val paramSym = fun.symbol.paramSymss.flatten.find(_.name == paramName)
4266- val paramSymWithMethodCallTree = paramSym.map((_, res))
4267- report.error(
4268- missingArgMsg(arg, formal, paramStr, paramSymWithMethodCallTree),
4269- tree.srcPos.endPos
4270- )
4271- case _ =>
4272- }
4273-
4274- res
4241+ // Pick a failure type to propagate, if any.
4242+ // Prefer other errors over ambiguities. If nested in outer searches a missing
4243+ // implicit can be healed by simply dropping this alternative and trying something
4244+ // else. But an ambiguity is sticky and propagates outwards. If we have both
4245+ // a missing implicit on one argument and an ambiguity on another the whole
4246+ // branch should be classified as a missing implicit.
4247+ def propagatedFailure (args : List [Tree ]): Type = args match
4248+ case arg :: args => arg.tpe match
4249+ case ambi : AmbiguousImplicits => propagatedFailure(args) match
4250+ case NoType | (_ : AmbiguousImplicits ) => ambi
4251+ case failed => failed
4252+ case failed : SearchFailureType => failed
4253+ case _ => propagatedFailure(args)
4254+ case Nil => NoType
4255+
4256+ /** Reports errors for arguments of `appTree` that have a `SearchFailureType`.
4257+ */
4258+ def issueErrors (fun : Tree , args : List [Tree ], failureType : Type ): Tree =
4259+ val errorType = failureType match
4260+ case ai : AmbiguousImplicits => ai.asNested
4261+ case tp => tp
4262+ untpd.Apply (fun, args)
4263+ .withType(errorType)
4264+ .tap: res =>
4265+ wtp.paramNames.lazyZip(wtp.paramInfos).lazyZip(args).foreach: (paramName, formal, arg) =>
4266+ arg.tpe match
4267+ case failure : SearchFailureType =>
4268+ val methodStr = err.refStr(methPart(fun).tpe)
4269+ val paramStr = implicitParamString(paramName, methodStr, fun)
4270+ val paramSym = fun.symbol.paramSymss.flatten.find(_.name == paramName)
4271+ val paramSymWithMethodCallTree = paramSym.map((_, res))
4272+ val msg = missingArgMsg(arg, formal, paramStr, paramSymWithMethodCallTree)
4273+ report.error(msg, tree.srcPos.endPos)
4274+ case _ =>
42754275
42764276 val args = implicitArgs(wtp.paramInfos, 0 , pt)
4277- if (args.tpes.exists(_.isInstanceOf [SearchFailureType ])) {
4277+ val failureType = propagatedFailure(args)
4278+ if failureType.exists then
42784279 // If there are several arguments, some arguments might already
42794280 // have influenced the context, binding variables, but later ones
42804281 // might fail. In that case the constraint and instantiated variables
@@ -4283,32 +4284,40 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
42834284
42844285 // If method has default params, fall back to regular application
42854286 // where all inferred implicits are passed as named args.
4286- if hasDefaultParams then
4287+ if hasDefaultParams && ! failureType. isInstanceOf [ AmbiguousImplicits ] then
42874288 // Only keep the arguments that don't have an error type, or that
4288- // have an `AmbiguousImplicits` error type. The later ensures that a
4289+ // have an `AmbiguousImplicits` error type. The latter ensures that a
42894290 // default argument can't override an ambiguous implicit. See tests
42904291 // `given-ambiguous-default*` and `19414*`.
42914292 val namedArgs =
4292- wtp.paramNames.lazyZip(args)
4293- .filter((_, arg) => ! arg.tpe.isError || arg.tpe.isInstanceOf [AmbiguousImplicits ])
4294- .map((pname, arg) => untpd.NamedArg (pname, untpd.TypedSplice (arg)))
4295-
4296- val app = cpy.Apply (tree)(untpd.TypedSplice (tree), namedArgs)
4297- val needsUsing = wtp.isContextualMethod || wtp.match
4298- case MethodType (ContextBoundParamName (_) :: _) => sourceVersion.isAtLeast(`3.4`)
4299- case _ => false
4300- if needsUsing then app.setApplyKind(ApplyKind .Using )
4301- typr.println(i " try with default implicit args $app" )
4302- val retyped = typed(app, pt, locked)
4303-
4304- // If the retyped tree still has an error type and is an `Apply`
4305- // node, we can report the errors for each argument nicely.
4306- // Otherwise, we don't report anything here.
4307- retyped match
4308- case Apply (tree, args) if retyped.tpe.isError => issueErrors(tree, args)
4309- case _ => retyped
4310- else issueErrors(tree, args)
4311- }
4293+ wtp.paramNames.lazyZip(args).collect:
4294+ case (pname, arg) if ! arg.tpe.isError || arg.tpe.isInstanceOf [AmbiguousImplicits ] =>
4295+ untpd.NamedArg (pname, untpd.TypedSplice (arg))
4296+ .toList
4297+ val usingDefaultArgs =
4298+ wtp.paramNames.zipWithIndex
4299+ .exists((n, i) => ! namedArgs.exists(_.name == n) && ! findDefaultArgument(i).isEmpty)
4300+
4301+ if ! usingDefaultArgs then
4302+ issueErrors(tree, args, failureType)
4303+ else
4304+ val app = cpy.Apply (tree)(untpd.TypedSplice (tree), namedArgs)
4305+ // old-style implicit needs to be marked using so that implicits are searched
4306+ val needsUsing = wtp.isImplicitMethod || wtp.match
4307+ case MethodType (ContextBoundParamName (_) :: _) => sourceVersion.isAtLeast(`3.4`)
4308+ case _ => false
4309+ if needsUsing then app.setApplyKind(ApplyKind .Using )
4310+ typr.println(i " try with default implicit args $app" )
4311+ // If the retyped tree still has an error type and is an `Apply`
4312+ // node, we can report the errors for each argument nicely.
4313+ // Otherwise, we don't report anything here.
4314+ typed(app, pt, locked) match
4315+ case retyped @ Apply (tree, args) if retyped.tpe.isError =>
4316+ propagatedFailure(args) match
4317+ case sft : SearchFailureType => issueErrors(tree, args, sft)
4318+ case _ => issueErrors(tree, args, retyped.tpe)
4319+ case retyped => retyped
4320+ else issueErrors(tree, args, failureType)
43124321 else
43134322 inContext(origCtx):
43144323 // Reset context in case it was set to a supercall context before.
0 commit comments