Skip to content

Commit 58dc16b

Browse files
committed
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
1 parent 098c50a commit 58dc16b

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ object Parsers {
540540
def handleThis(qual: Ident) = {
541541
in.nextToken()
542542
val t = atPos(start) { This(qual) }
543-
if (!thisOK && in.token != DOT) syntaxError("`.' expected")
543+
if (!thisOK && in.token != DOT) syntaxError(DangelingThisInPath(), start)
544544
dotSelectors(t, finish)
545545
}
546546
def handleSuper(qual: Ident) = {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,31 @@ object messages {
901901
val msg = hl"trying to define package with same name as `$existing`"
902902
val explanation = ""
903903
}
904+
905+
case class DangelingThisInPath()(implicit ctx: Context) extends Message(34) {
906+
val kind = "Syntax"
907+
val msg = hl"""Expected an additional member selection after the keyword ${"this"}"""
908+
909+
val importCode =
910+
"""import MyClass.this.member
911+
|// ^^^^^^^
912+
"""
913+
914+
val typeCode =
915+
"""type T = MyClass.this.Member
916+
|// ^^^^^^^
917+
"""
918+
919+
val explanation =
920+
hl"""|Paths of imports and type selections must not end with the keyword ${"this"}.
921+
|
922+
|Maybe you forgot to select a member of ${"this"}?
923+
|
924+
|- Example for a valid import expression using a path
925+
|${importCode}
926+
|
927+
|- Example for a valid type using a path
928+
|${typeCode}
929+
|"""
930+
}
904931
}

0 commit comments

Comments
 (0)