Skip to content

Commit 1b4e4f5

Browse files
oderskysmarter
authored andcommitted
Handle implicits with default parameters
Fixes #576. run/pending/Meter now compiles; crashes at runtime
1 parent 3c8f04e commit 1b4e4f5

File tree

1 file changed

+25
-21
lines changed

1 file changed

+25
-21
lines changed

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,29 +1435,33 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
14351435
wtp.paramTypes.foreach(instantiateSelected(_, tvarsToInstantiate))
14361436
val constr = ctx.typerState.constraint
14371437
def addImplicitArgs = {
1438-
def implicitArgError(msg: => String): Tree = {
1439-
ctx.error(msg, tree.pos.endPos)
1440-
EmptyTree
1441-
}
1442-
val args = (wtp.paramNames, wtp.paramTypes).zipped map { (pname, formal) =>
1443-
def where = d"parameter $pname of $methodStr"
1444-
inferImplicit(formal, EmptyTree, tree.pos.endPos) match {
1445-
case SearchSuccess(arg, _, _) =>
1446-
arg
1447-
case ambi: AmbiguousImplicits =>
1448-
implicitArgError(s"ambiguous implicits: ${ambi.explanation} of $where")
1449-
case failure: SearchFailure =>
1450-
implicitArgError(d"no implicit argument of type $formal found for $where" + failure.postscript)
1451-
}
1438+
def argsOrError(pnames: List[TermName], ptypes: List[Type], acc: mutable.ListBuffer[Tree])
1439+
: AnyRef /* List[Tree] | () => String */ = (pnames, ptypes) match {
1440+
case (pname :: pnames1, ptype :: ptypes1) =>
1441+
def where = d"parameter $pname of $methodStr"
1442+
inferImplicit(ptype, EmptyTree, tree.pos.endPos) match {
1443+
case SearchSuccess(arg, _, _) =>
1444+
acc += arg
1445+
argsOrError(pnames1, ptypes1, acc)
1446+
case ambi: AmbiguousImplicits =>
1447+
() => s"ambiguous implicits: ${ambi.explanation} of $where"
1448+
case failure: SearchFailure =>
1449+
() => d"no implicit argument of type $ptype found for $where" + failure.postscript
1450+
}
1451+
case (Nil, Nil) =>
1452+
acc.toList
14521453
}
1453-
if (args.exists(_.isEmpty)) {
1454-
// If there are several arguments, some arguments might already
1455-
// have influenced the context, binfing variables, but later ones
1456-
// might fail. In that case the constraint needs to be reset.
1457-
ctx.typerState.constraint = constr
1458-
tree
1454+
argsOrError(wtp.paramNames, wtp.paramTypes, new mutable.ListBuffer[Tree]) match {
1455+
case args: List[Tree] => adapt(tpd.Apply(tree, args), pt)
1456+
case failure: (() => String) =>
1457+
// If there are several arguments, some arguments might already
1458+
// have influenced the context, binding variables, but later ones
1459+
// might fail. In that case the constraint needs to be reset.
1460+
ctx.typerState.constraint = constr
1461+
tryEither
1462+
{ implicit ctx => typed(untpd.Apply(untpd.TypedSplice(tree), Nil), pt) }
1463+
{ (_, _) => ctx.error(failure(), tree.pos.endPos); tree }
14591464
}
1460-
else adapt(tpd.Apply(tree, args), pt)
14611465
}
14621466
if ((pt eq WildcardType) || original.isEmpty) addImplicitArgs
14631467
else

0 commit comments

Comments
 (0)