From 3c2a293c96c6f1917341086c2cbd45558b90980f Mon Sep 17 00:00:00 2001 From: Jonathan Brachthäuser Date: Wed, 21 Dec 2016 17:08:59 +0100 Subject: Add error message for dangling this in path selections The following examples trigger the error message: val x: Foo.this = ??? // Also triggers the error: import foo.this // Additionally, also slays the compiler type X = Foo.this.type --- .../src/dotty/tools/dotc/parsing/Parsers.scala | 2 +- .../tools/dotc/reporting/diagnostic/messages.scala | 27 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) (limited to 'compiler/src/dotty') diff --git a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala index 18f0b42f5..5604cb9e4 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Parsers.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Parsers.scala @@ -540,7 +540,7 @@ object Parsers { def handleThis(qual: Ident) = { in.nextToken() val t = atPos(start) { This(qual) } - if (!thisOK && in.token != DOT) syntaxError("`.' expected") + if (!thisOK && in.token != DOT) syntaxError(DanglingThisInPath(), start) dotSelectors(t, finish) } def handleSuper(qual: Ident) = { diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 25db28e4a..9ba771be9 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -966,4 +966,31 @@ object messages { | ${"val foo: Int = 3"} |""" } + + case class DanglingThisInPath()(implicit ctx: Context) extends Message(36) { + val kind = "Syntax" + val msg = hl"""Expected an additional member selection after the keyword ${"this"}""" + + val importCode = + """import MyClass.this.member + |// ^^^^^^^ + """ + + val typeCode = + """type T = MyClass.this.Member + |// ^^^^^^^ + """ + + val explanation = + hl"""|Paths of imports and type selections must not end with the keyword ${"this"}. + | + |Maybe you forgot to select a member of ${"this"}? + | + |- Example for a valid import expression using a path + |${importCode} + | + |- Example for a valid type using a path + |${typeCode} + |""" + } } -- cgit v1.2.3