Skip to content

Commit 3c2a293

Browse files
b-studiosfelixmulder
authored andcommitted
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 a909c2f commit 3c2a293

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(DanglingThisInPath(), 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
@@ -966,4 +966,31 @@ object messages {
966966
| ${"val foo: Int = 3"}
967967
|"""
968968
}
969+
970+
case class DanglingThisInPath()(implicit ctx: Context) extends Message(36) {
971+
val kind = "Syntax"
972+
val msg = hl"""Expected an additional member selection after the keyword ${"this"}"""
973+
974+
val importCode =
975+
"""import MyClass.this.member
976+
|// ^^^^^^^
977+
"""
978+
979+
val typeCode =
980+
"""type T = MyClass.this.Member
981+
|// ^^^^^^^
982+
"""
983+
984+
val explanation =
985+
hl"""|Paths of imports and type selections must not end with the keyword ${"this"}.
986+
|
987+
|Maybe you forgot to select a member of ${"this"}?
988+
|
989+
|- Example for a valid import expression using a path
990+
|${importCode}
991+
|
992+
|- Example for a valid type using a path
993+
|${typeCode}
994+
|"""
995+
}
969996
}

0 commit comments

Comments
 (0)