aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Odersky <odersky@gmail.com>2016-02-16 13:46:46 +0100
committerMartin Odersky <odersky@gmail.com>2016-02-16 13:46:46 +0100
commitc4be2191e0c17767911377d7bc835d1b40eb39af (patch)
tree38e7586809bc3520b31f5a64f8f4f5c53ddc9b27
parentafec2a7a597e6e7736e4d26620f8488e9587b7b0 (diff)
downloaddotty-c4be2191e0c17767911377d7bc835d1b40eb39af.tar.gz
dotty-c4be2191e0c17767911377d7bc835d1b40eb39af.tar.bz2
dotty-c4be2191e0c17767911377d7bc835d1b40eb39af.zip
Handle implicits with default parameters
If an implicit parameter has a default, then that default should be taken in case no implicit argument is found.
-rw-r--r--src/dotty/tools/dotc/typer/Typer.scala30
1 files changed, 25 insertions, 5 deletions
diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala
index 6a2ff30fa..b65e3cd42 100644
--- a/src/dotty/tools/dotc/typer/Typer.scala
+++ b/src/dotty/tools/dotc/typer/Typer.scala
@@ -1435,10 +1435,15 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
wtp.paramTypes.foreach(instantiateSelected(_, tvarsToInstantiate))
val constr = ctx.typerState.constraint
def addImplicitArgs = {
- def implicitArgError(msg: => String): Tree = {
- ctx.error(msg, tree.pos.endPos)
+ val errors = new mutable.ListBuffer[() => String]
+ def implicitArgError(msg: => String) = {
+ errors += (() => msg)
EmptyTree
}
+ def issueErrors() = {
+ for (err <- errors) ctx.error(err(), tree.pos.endPos)
+ tree
+ }
val args = (wtp.paramNames, wtp.paramTypes).zipped map { (pname, formal) =>
def where = d"parameter $pname of $methodStr"
inferImplicit(formal, EmptyTree, tree.pos.endPos) match {
@@ -1450,12 +1455,27 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
implicitArgError(d"no implicit argument of type $formal found for $where" + failure.postscript)
}
}
- if (args.exists(_.isEmpty)) {
+ if (errors.nonEmpty) {
// If there are several arguments, some arguments might already
- // have influenced the context, binfing variables, but later ones
+ // have influenced the context, binding variables, but later ones
// might fail. In that case the constraint needs to be reset.
ctx.typerState.constraint = constr
- tree
+
+ // If method has default params, fall back to regular application
+ // where all inferred implicits are passed as named args.
+ if (tree.symbol.hasDefaultParams) {
+ val namedArgs = (wtp.paramNames, args).zipped.flatMap { (pname, arg) =>
+ arg match {
+ case EmptyTree => Nil
+ case _ => untpd.NamedArg(pname, untpd.TypedSplice(arg)) :: Nil
+ }
+ }
+ tryEither { implicit ctx =>
+ typed(untpd.Apply(untpd.TypedSplice(tree), namedArgs), pt)
+ } { (_, _) =>
+ issueErrors()
+ }
+ } else issueErrors()
}
else adapt(tpd.Apply(tree, args), pt)
}