From 95e7c5a0236ce17c053d2b918cc828594e4b8bb8 Mon Sep 17 00:00:00 2001 From: Saurabh Rawat Date: Sat, 17 Mar 2018 14:00:31 +0530 Subject: [PATCH 1/3] Fixes #3683 REPL: Incorrect pretty-printing of singleton types --- compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala | 2 +- compiler/test/dotty/tools/repl/ReplCompilerTests.scala | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index 1c04a03605a1..81f851439cc1 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -142,7 +142,7 @@ class PlainPrinter(_ctx: Context) extends Printer { case tp: TypeParamRef => ParamRefNameString(tp) ~ lambdaHash(tp.binder) case tp: SingletonType => - toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")" + toTextRef(tp) ~ ".type" case AppliedType(tycon, args) => (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close case tp: RefinedType => diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index 855e781ff61a..5cc54edc0270 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -125,4 +125,9 @@ class ReplCompilerTests extends ReplTest { compile("def f(g: => Int): Int = g") assertTrue(storedOutput().startsWith("def f(g: => Int): Int")) } + + @Test def singletonType: Unit = fromInitialState { implicit state => + compile("""val a = "foo"; val x: a.type = a""") + assertTrue(storedOutput().contains("""val x: a.type = "foo"""")) + } } From 27780b4a2e2e96d8955a3cbc921d3a39a1d7bd20 Mon Sep 17 00:00:00 2001 From: Saurabh Rawat Date: Wed, 30 May 2018 17:30:20 +0530 Subject: [PATCH 2/3] attempt to case ConstantType --- compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala | 6 +++++- .../dotty/tools/dotc/reporting/ErrorMessagesTests.scala | 1 - 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala index b5046418a0a7..ab81c2383b26 100644 --- a/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala +++ b/compiler/src/dotty/tools/dotc/printing/PlainPrinter.scala @@ -141,8 +141,12 @@ class PlainPrinter(_ctx: Context) extends Printer { ParamRefNameString(tp) ~ ".type" case tp: TypeParamRef => ParamRefNameString(tp) ~ lambdaHash(tp.binder) + case tp: ConstantType => + toTextRef(tp) + case tp: TermRef => + toTextPrefix(tp.prefix) ~ selectionString(tp) ~ ".type" case tp: SingletonType => - toTextRef(tp) ~ ".type" + toTextLocal(tp.underlying) ~ "(" ~ toTextRef(tp) ~ ")" case AppliedType(tycon, args) => (toTextLocal(tycon) ~ "[" ~ Text(args map argText, ", ") ~ "]").close case tp: RefinedType => diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 101d5664973c..3767301e0ca0 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -367,7 +367,6 @@ class ErrorMessagesTests extends ErrorMessagesTest { assertMessageCount(1, messages) val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages - assertEquals("Scope.foo(1)", tree.show) assertEquals("((a: Int): Unit)(Scope.foo)", methodPart.show) } From f51ac6fb037a65c15e2b1b364025f536f3c8c895 Mon Sep 17 00:00:00 2001 From: Allan Renucci Date: Wed, 30 May 2018 16:01:24 +0200 Subject: [PATCH 3/3] Refactor "method does not take parameter" error message --- .../dotc/reporting/diagnostic/messages.scala | 20 +++++++++---------- .../src/dotty/tools/dotc/typer/Typer.scala | 2 +- .../dotc/reporting/ErrorMessagesTests.scala | 10 +++++----- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index ef685b0dc0f2..25b32ced4fed 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1371,27 +1371,25 @@ object messages { |""" } - case class MethodDoesNotTakeParameters(tree: tpd.Tree, methPartType: Types.Type)(err: typer.ErrorReporting.Errors)(implicit ctx: Context) + case class MethodDoesNotTakeParameters(tree: tpd.Tree, methodStr: String)(implicit ctx: Context) extends Message(MethodDoesNotTakeParametersId) { private val more = tree match { case Apply(_, _) => " more" case _ => "" } - val msg = hl"${err.refStr(methPartType)} does not take$more parameters" + val msg = hl"$methodStr does not take$more parameters" val kind = "Reference" - private val noParameters = if (methPartType.widenSingleton.isInstanceOf[ExprType]) - hl"""|As ${err.refStr(methPartType)} is defined without parenthesis, you may - |not use any at call-site, either. - |""" - else - "" + val explanation = { + val isNullary = tpd.methPart(tree).tpe.widenSingleton.isInstanceOf[ExprType] + val addendum = + if (isNullary) "\nNullary methods may not be called with parenthesis" + else "" - val explanation = - s"""|You have specified more parameter lists as defined in the method definition(s). - |$noParameters""".stripMargin + "You have specified more parameter lists as defined in the method definition(s)." + addendum + } } diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index f9c7fdaeef7b..baa65cf08dfd 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -2138,7 +2138,7 @@ class Typer extends Namer else tree case _ => tryInsertApplyOrImplicit(tree, pt, locked) { - errorTree(tree, MethodDoesNotTakeParameters(tree, methPart(tree).tpe)(err)) + errorTree(tree, MethodDoesNotTakeParameters(tree, methodStr)) } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 3767301e0ca0..6f7405c225f4 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -347,10 +347,10 @@ class ErrorMessagesTests extends ErrorMessagesTest { implicit val ctx: Context = ictx assertMessageCount(1, messages) - val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages + val MethodDoesNotTakeParameters(tree, methodStr) :: Nil = messages assertEquals("Scope.foo", tree.show) - assertEquals("=> Unit(Scope.foo)", methodPart.show) + assertEquals("method foo in object Scope", methodStr) } @Test def methodDoesNotTakeMorePrameters = @@ -365,10 +365,10 @@ class ErrorMessagesTests extends ErrorMessagesTest { .expect { (ictx, messages) => implicit val ctx: Context = ictx - assertMessageCount(1, messages) - val MethodDoesNotTakeParameters(tree, methodPart) :: Nil = messages + val MethodDoesNotTakeParameters(tree, methodStr) :: Nil = messages + assertEquals("Scope.foo(1)", tree.show) - assertEquals("((a: Int): Unit)(Scope.foo)", methodPart.show) + assertEquals("method foo in object Scope", methodStr) } @Test def ambiugousOverloadWithWildcard =