Skip to content

Commit 6a497d7

Browse files
committed
Improve message for colon fusion
When the user accidentally writes `val x_: Int` where the colon belongs to the identifier as an operator suffix, tell them so.
1 parent 53d4ff7 commit 6a497d7

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4008,7 +4008,14 @@ object Parsers {
40084008
val tpt = typedOpt()
40094009
val rhs =
40104010
if tpt.isEmpty || in.token == EQUALS then
4011-
accept(EQUALS)
4011+
if tpt.isEmpty && in.token != EQUALS then
4012+
lhs match
4013+
case Ident(name) :: Nil if name.endsWith(":") =>
4014+
val help = i"; identifier ends in colon, did you mean `${name.toSimpleName.dropRight(1)}`: in backticks?"
4015+
syntaxErrorOrIncomplete(ExpectedTokenButFound(EQUALS, in.token, suffix = help))
4016+
case _ => accept(EQUALS)
4017+
else
4018+
accept(EQUALS)
40124019
val rhsOffset = in.offset
40134020
subExpr() match
40144021
case rhs0 @ Ident(name) if placeholderParams.nonEmpty && name == placeholderParams.head.name

tests/neg/i18020.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
import _root_.scala.StringContext // ok
23

34
class Test :

tests/neg/i18020b.check

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
-- [E040] Syntax Error: tests/neg/i18020b.scala:2:17 -------------------------------------------------------------------
2+
2 |class i18020(a_: Int): // error
3+
| ^^^
4+
| ':' expected, but identifier found
5+
-- [E040] Syntax Error: tests/neg/i18020b.scala:3:12 -------------------------------------------------------------------
6+
3 | def f(b_: Int) = 42 // error
7+
| ^^^
8+
| ':' expected, but identifier found
9+
-- [E040] Syntax Error: tests/neg/i18020b.scala:4:10 -------------------------------------------------------------------
10+
4 | def g_: Int = 27 // error
11+
| ^^^
12+
| '=' expected, but identifier found
13+
-- [E040] Syntax Error: tests/neg/i18020b.scala:6:12 -------------------------------------------------------------------
14+
6 | val x_: Int = 1 // error
15+
| ^^^
16+
| '=' expected, but identifier found; identifier ends in colon, did you mean `x_`: in backticks?
17+
-- [E040] Syntax Error: tests/neg/i18020b.scala:7:12 -------------------------------------------------------------------
18+
7 | val y_: Int = 2 // error
19+
| ^^^
20+
| '=' expected, but identifier found; identifier ends in colon, did you mean `y_`: in backticks?
21+
-- [E006] Not Found Error: tests/neg/i18020b.scala:8:4 -----------------------------------------------------------------
22+
8 | x_ + y_ // error
23+
| ^^
24+
| Not found: x_ - did you mean x_:?
25+
|
26+
| longer explanation available when compiling with `-explain`

tests/neg/i18020b.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// problems with colon fusion, a harder challenge than cold fusion
2+
class i18020(a_: Int): // error
3+
def f(b_: Int) = 42 // error
4+
def g_: Int = 27 // error
5+
def k =
6+
val x_: Int = 1 // error
7+
val y_: Int = 2 // error
8+
x_ + y_ // error

0 commit comments

Comments
 (0)