From 1d497f586a313aeb92039bd14d90db132c72d91e Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 28 Mar 2023 14:24:34 +0200 Subject: [PATCH] Recognize named arguments in isFunctionWithUnknownParamType --- compiler/src/dotty/tools/dotc/ast/TreeInfo.scala | 2 ++ compiler/src/dotty/tools/dotc/typer/Applications.scala | 2 +- tests/pos/i17155.scala | 7 +++++++ 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 tests/pos/i17155.scala diff --git a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala index 73f45bd7369b..c2147b6af2d3 100644 --- a/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala +++ b/compiler/src/dotty/tools/dotc/ast/TreeInfo.scala @@ -399,6 +399,8 @@ trait UntypedTreeInfo extends TreeInfo[Untyped] { self: Trees.Instance[Untyped] Some(tree) case Block(Nil, expr) => functionWithUnknownParamType(expr) + case NamedArg(_, expr) => + functionWithUnknownParamType(expr) case _ => None } diff --git a/compiler/src/dotty/tools/dotc/typer/Applications.scala b/compiler/src/dotty/tools/dotc/typer/Applications.scala index 345a8693063b..79d6501ccb2d 100644 --- a/compiler/src/dotty/tools/dotc/typer/Applications.scala +++ b/compiler/src/dotty/tools/dotc/typer/Applications.scala @@ -448,7 +448,7 @@ trait Applications extends Compatibility { def rec(t: Type): Type = { t.widen match{ case funType: MethodType => funType - case funType: PolyType => + case funType: PolyType => rec(instantiateWithTypeVars(funType)) case tp => tp } diff --git a/tests/pos/i17155.scala b/tests/pos/i17155.scala new file mode 100644 index 000000000000..7b58dbe4a13a --- /dev/null +++ b/tests/pos/i17155.scala @@ -0,0 +1,7 @@ +def foo[A, B](arr: Array[A], pf: PartialFunction[A, B]): Seq[B] = arr.toSeq.collect(pf) +def foo[A, B](list: List[A], pf: PartialFunction[A, B]): Seq[B] = list.collect(pf) // no errors if this is commented out + +val arr = Array(1, 2, 3) +val resOkay = foo(arr = arr, { case n if n % 2 != 0 => n.toString }) // compiles +val resNope = foo(arr = arr, pf = { case n if n % 2 != 0 => n.toString }) // Error 1 +val resNope2 = foo[Int, String](arr = arr, pf = { case n if n % 2 != 0 => n.toString }) // Error 2 \ No newline at end of file