Skip to content

Commit 84c7515

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 bf9888e commit 84c7515

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,6 +901,7 @@ object messages {
901901
val explanation = ""
902902
}
903903

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

0 commit comments

Comments
 (0)