From 0a6f9bbdaa2fd55e52cca6e66f8771605d3c924d Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 5 Apr 2022 09:59:09 +0200 Subject: [PATCH 1/2] Don't issue implicit errors if parameter has default value If implicit parameters have default values then we try to re-type the application with default values supplied as arguments. So far we fell back to the original errors if retyping failed, which can lead to confusing error messages. We now stick with the retyping in all cases. Fixes #14842 --- compiler/src/dotty/tools/dotc/typer/Typer.scala | 12 ++++-------- tests/neg/i14842.check | 7 +++++++ tests/neg/i14842.scala | 12 ++++++++++++ 3 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 tests/neg/i14842.check create mode 100644 tests/neg/i14842.scala diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index 2ae55032d7ae..a85b2574e59c 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3539,14 +3539,10 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer val namedArgs = wtp.paramNames.lazyZip(args).flatMap { (pname, arg) => if (arg.tpe.isError) Nil else untpd.NamedArg(pname, untpd.TypedSplice(arg)) :: Nil } - tryEither { - val app = cpy.Apply(tree)(untpd.TypedSplice(tree), namedArgs) - if (wtp.isContextualMethod) app.setApplyKind(ApplyKind.Using) - typr.println(i"try with default implicit args $app") - typed(app, pt, locked) - } { (_, _) => - issueErrors() - } + val app = cpy.Apply(tree)(untpd.TypedSplice(tree), namedArgs) + if (wtp.isContextualMethod) app.setApplyKind(ApplyKind.Using) + typr.println(i"try with default implicit args $app") + typed(app, pt, locked) else issueErrors() } else tree match { diff --git a/tests/neg/i14842.check b/tests/neg/i14842.check new file mode 100644 index 000000000000..45deb2a8c300 --- /dev/null +++ b/tests/neg/i14842.check @@ -0,0 +1,7 @@ +-- [E007] Type Mismatch Error: tests/neg/i14842.scala:11:35 ------------------------------------------------------------ +11 | val x: Either[Int, Any] = node.as[Any] // error + | ^^^^^^^^^^^^ + | Found: Either[String, Any] + | Required: Either[Int, Any] + | + | longer explanation available when compiling with `-explain` diff --git a/tests/neg/i14842.scala b/tests/neg/i14842.scala new file mode 100644 index 000000000000..201030ede371 --- /dev/null +++ b/tests/neg/i14842.scala @@ -0,0 +1,12 @@ +class Dummy +object Dummy: + val empty = new Dummy + +sealed trait Node: + def as[T](using d: Dummy = Dummy.empty): Either[String, T] = ??? + +object Sample extends App { + val node: Node = ??? + + val x: Either[Int, Any] = node.as[Any] // error +} \ No newline at end of file From 484c9c6fd09e7d38f3819c8f502a69c2a94219de Mon Sep 17 00:00:00 2001 From: odersky Date: Tue, 5 Apr 2022 11:48:50 +0200 Subject: [PATCH 2/2] Fix flaky test --- tests/neg/i14772.check | 9 --------- tests/neg/i14772.scala | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/tests/neg/i14772.check b/tests/neg/i14772.check index 255e5c897681..4fb5b7f7cd5f 100644 --- a/tests/neg/i14772.check +++ b/tests/neg/i14772.check @@ -4,12 +4,3 @@ | Overloaded or recursive method impl needs return type | | longer explanation available when compiling with `-explain` --- Error: tests/neg/i14772.scala:8:12 ---------------------------------------------------------------------------------- -8 | Expr(()) // error - | ^ - | no given instance of type quoted.ToExpr[Unit] was found for parameter x$2 of method apply in object Expr. - | I found: - | - | quoted.ToExpr.ClassToExpr[T] - | - | But given instance ClassToExpr in object ToExpr does not match type quoted.ToExpr[Unit]. diff --git a/tests/neg/i14772.scala b/tests/neg/i14772.scala index 29cafb8e6a48..a0cb3c5e857e 100644 --- a/tests/neg/i14772.scala +++ b/tests/neg/i14772.scala @@ -5,6 +5,6 @@ object A { def impl(a: Expr[Any])(using Quotes)/*: Expr[Any]*/ = { foo(a) // error - Expr(()) // error + ??? } } \ No newline at end of file