Skip to content

Commit f0c9ff8

Browse files
committed
Better positions on errorTermTree
1 parent 29f9d33 commit f0c9ff8

16 files changed

+60
-62
lines changed

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ object Parsers {
377377
false
378378
}
379379

380-
def errorTermTree: Literal = atSpan(in.offset) { Literal(Constant(null)) }
380+
def errorTermTree(start: Offset): Literal = atSpan(start, in.offset, in.offset) { Literal(Constant(null)) }
381381

382382
private var inFunReturnType = false
383383
private def fromWithinReturnType[T](body: => T): T = {
@@ -1931,7 +1931,7 @@ object Parsers {
19311931
PolyFunction(tparams, body)
19321932
else {
19331933
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
1934-
errorTermTree
1934+
errorTermTree(arrowOffset)
19351935
}
19361936
}
19371937
case _ =>
@@ -2298,8 +2298,9 @@ object Parsers {
22982298
in.nextToken()
22992299
simpleExpr(location)
23002300
else
2301+
val start = in.lastOffset
23012302
syntaxErrorOrIncomplete(IllegalStartSimpleExpr(tokenString(in.token)), expectedOffset)
2302-
errorTermTree
2303+
errorTermTree(start)
23032304
}
23042305
simpleExprRest(t, location, canApply)
23052306
}
@@ -2738,8 +2739,9 @@ object Parsers {
27382739
case _ =>
27392740
if (isLiteral) literal(inPattern = true)
27402741
else {
2742+
val start = in.lastOffset
27412743
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern(), expectedOffset)
2742-
errorTermTree
2744+
errorTermTree(start)
27432745
}
27442746
}
27452747

@@ -3314,8 +3316,9 @@ object Parsers {
33143316
}
33153317
val rhs2 =
33163318
if rhs.isEmpty && !isAllIds then
3319+
val start = in.lastOffset
33173320
syntaxError(ExpectedTokenButFound(EQUALS, in.token), Span(in.lastOffset))
3318-
errorTermTree
3321+
errorTermTree(start)
33193322
else
33203323
rhs
33213324
PatDef(mods, lhs, tpt, rhs2)
@@ -3483,11 +3486,12 @@ object Parsers {
34833486
case GIVEN =>
34843487
givenDef(start, mods, atSpan(in.skipToken()) { Mod.Given() })
34853488
case _ =>
3489+
val start = in.lastOffset
34863490
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition())
34873491
mods.annotations match {
34883492
case head :: Nil => head
34893493
case Nil => EmptyTree
3490-
case all => Block(all, errorTermTree)
3494+
case all => Block(all, errorTermTree(start))
34913495
}
34923496
}
34933497

compiler/src/dotty/tools/dotc/parsing/xml/MarkupParsers.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ object MarkupParsers {
131131
try handle.parseAttribute(Span(start, curOffset, mid), tmp)
132132
catch {
133133
case e: RuntimeException =>
134-
errorAndResult("error parsing attribute value", parser.errorTermTree)
134+
errorAndResult("error parsing attribute value", parser.errorTermTree(parser.in.offset))
135135
}
136136

137137
case '{' =>
@@ -334,7 +334,7 @@ object MarkupParsers {
334334
finally parser.in.resume(saved)
335335

336336
if (output == null)
337-
parser.errorTermTree
337+
parser.errorTermTree(parser.in.offset)
338338
else
339339
output
340340
}

language-server/test/dotty/tools/languageserver/DiagnosticsTest.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,7 @@ class DiagnosticsTest {
2121
| Nil.map(x => x).filter(x$m1 =>$m2)$m3
2222
|}""".withSource
2323
.diagnostics(m1,
24-
(m2 to m2, "expression expected but ')' found", Error, Some(IllegalStartSimpleExprID)),
25-
(m1 to m1, """Found: Null
26-
|Required: Boolean""".stripMargin, Error, Some(TypeMismatchID))
24+
(m2 to m2, "expression expected but ')' found", Error, Some(IllegalStartSimpleExprID))
2725
)
2826

2927
@Test def diagnosticPureExpression: Unit =

tests/neg/arg-eof.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
object Test:
22
case class Widget(name: String, other: Int = 5)
3-
Widget(name = "foo", // error // error
3+
Widget(name = "foo", // error

tests/neg/errpos.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object Test {
1010
val b = type // error: expression expected (on "type")
1111

1212
1 match {
13-
case // error: pattern expected // error: cannot compare with Null
13+
case // error: pattern expected
1414
case 2 => ""
1515
}
16-
}
16+
}

tests/neg/i12150.check

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,3 @@
44
| expression expected but end found
55
|
66
| longer explanation available when compiling with `-explain`
7-
-- [E129] Potential Issue Warning: tests/neg/i12150.scala:1:11 ---------------------------------------------------------
8-
1 |def f: Unit = // error
9-
| ^
10-
| A pure expression does nothing in statement position; you may be omitting necessary parentheses
11-
|
12-
| longer explanation available when compiling with `-explain`

tests/neg/i1846.scala

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ object Test {
33
val x = 42
44
val Y = "42"
55

6-
x match { case { 42 } => () } // error // error
7-
x match { case { 42.toString } => () } // error // error
8-
x match { case { 42 }.toString => () } // error // error
6+
x match { case { 42 } => () } // error
7+
x match { case { 42.toString } => () } // error
8+
x match { case { 42 }.toString => () } // error
99
x match { case "42".toInt => () } // error
10-
x match { case { "42".toInt } => () } // error // error
11-
x match { case { "42" }.toInt => () } // error // error
12-
x match { case { "42".toInt } => () } // error // error
10+
x match { case { "42".toInt } => () } // error
11+
x match { case { "42" }.toInt => () } // error
12+
x match { case { "42".toInt } => () } // error
1313
x match { case Y => () } // error
14-
x match { case { Y.toInt } => () } // error // error
15-
x match { case { Y }.toInt => () } // error // error
16-
x match { case { Y }.toString => () } // error // error
17-
x match { case { Y.toString } => () } // error // error
14+
x match { case { Y.toInt } => () } // error
15+
x match { case { Y }.toInt => () } // error
16+
x match { case { Y }.toString => () } // error
17+
x match { case { Y.toString } => () } // error
1818
}
1919
}

tests/neg/i3812.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ object Test {
33
val x = 42
44
val Y = "42"
55

6-
x match { case { 42 } => () } // error // error
7-
x match { case { "42".toInt } => () } // error // error
8-
x match { case { "42" }.toInt => () } // error // error
9-
x match { case { "42".toInt } => () } // error // error
10-
x match { case { Y.toInt } => () } // error // error
11-
x match { case { Y }.toInt => () } // error // error
6+
x match { case { 42 } => () } // error
7+
x match { case { "42".toInt } => () } // error
8+
x match { case { "42" }.toInt => () } // error
9+
x match { case { "42".toInt } => () } // error
10+
x match { case { Y.toInt } => () } // error
11+
x match { case { Y }.toInt => () } // error
1212
}
1313
}

tests/neg/i5004.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object i0 {
22
1 match {
33
def this(): Int // error
4-
def this()
4+
def this() // error
5+
}
56
}
6-
}

tests/neg/i7742.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
object A {
2-
for // error // error
3-
}
2+
for // error
3+
}

tests/neg/parser-stability-25.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ class D extends (Int => 1) {
1111
}
1212

1313
class Wrap(x: Int)
14-
class E extends (Wrap)( // error
14+
class E extends (Wrap)(
1515
// error

tests/neg/parser-stability-27.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
class F extends (Int => 1)( // error
1+
class F extends (Int => 1)(
22
// error

tests/neg/parser-stability-5.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
trait x0 {
22
x1 : { // error
3-
var x2 // error
3+
var x2
44
// error

tests/neg/t5702-neg-bad-and-wild.check

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:10:22 ---------------------------------------------------
2-
10 | case List(1, _*,) => // error: pattern expected // error
2+
10 | case List(1, _*,) => // error: pattern expected
33
| ^
44
| pattern expected
55
|
66
| longer explanation available when compiling with `-explain`
77
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:12:23 ---------------------------------------------------
8-
12 | case List(1, _*3,) => // error: pattern expected // error // error
8+
12 | case List(1, _*3,) => // error: pattern expected // error
99
| ^
1010
| pattern expected
1111
|
1212
| longer explanation available when compiling with `-explain`
13+
-- [E040] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:13:23 ---------------------------------------------------
14+
13 | case List(1, _*3:) => // error // error
15+
| ^
16+
| an identifier expected, but ')' found
1317
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:15:18 ---------------------------------------------------
1418
15 | case List(x*, 1) => // error: pattern expected
1519
| ^
@@ -34,17 +38,15 @@
3438
| x is already defined as value x
3539
|
3640
| Note that overloaded methods must all be defined in the same group of toplevel definitions
37-
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:10:21 -----------------------------------------------------------------
38-
10 | case List(1, _*,) => // error: pattern expected // error
39-
| ^
40-
| Values of types Null and Int cannot be compared with == or !=
4141
-- [E006] Not Found Error: tests/neg/t5702-neg-bad-and-wild.scala:12:20 ------------------------------------------------
42-
12 | case List(1, _*3,) => // error: pattern expected // error // error
42+
12 | case List(1, _*3,) => // error: pattern expected // error
43+
| ^
44+
| Not found: *
45+
|
46+
| longer explanation available when compiling with `-explain`
47+
-- [E006] Not Found Error: tests/neg/t5702-neg-bad-and-wild.scala:13:20 ------------------------------------------------
48+
13 | case List(1, _*3:) => // error // error
4349
| ^
4450
| Not found: *
4551
|
4652
| longer explanation available when compiling with `-explain`
47-
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:12:22 -----------------------------------------------------------------
48-
12 | case List(1, _*3,) => // error: pattern expected // error // error
49-
| ^
50-
| Values of types Null and Int cannot be compared with == or !=

tests/neg/t5702-neg-bad-and-wild.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ object Test {
77
val is = List(1,2,3)
88

99
is match {
10-
case List(1, _*,) => // error: pattern expected // error
10+
case List(1, _*,) => // error: pattern expected
1111

12-
case List(1, _*3,) => // error: pattern expected // error // error
13-
//case List(1, _*3:) => // poor recovery by parens
12+
case List(1, _*3,) => // error: pattern expected // error
13+
case List(1, _*3:) => // error // error
1414
case List(1, x*) => // ok
1515
case List(x*, 1) => // error: pattern expected
1616
case (1, x*) => //ok

tests/neg/trailingCommas.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ package foo
22

33
// Multi-line only cases: make sure trailing commas are only supported when multi-line
44

5-
trait ArgumentExprs1 { validMethod(23, "bar", )(Ev0, Ev1) } // error // error
6-
trait ArgumentExprs2 { validMethod(23, "bar")(Ev0, Ev1, ) } // error // error
7-
trait ArgumentExprs3 { new ValidClass(23, "bar", )(Ev0, Ev1) } // error // error
8-
trait ArgumentExprs4 { new ValidClass(23, "bar")(Ev0, Ev1, ) } // error // error
5+
trait ArgumentExprs1 { validMethod(23, "bar", )(Ev0, Ev1) } // error
6+
trait ArgumentExprs2 { validMethod(23, "bar")(Ev0, Ev1, ) } // error
7+
trait ArgumentExprs3 { new ValidClass(23, "bar", )(Ev0, Ev1) } // error
8+
trait ArgumentExprs4 { new ValidClass(23, "bar")(Ev0, Ev1, ) } // error
99

1010
trait Params1 { def f(foo: Int, bar: String, )(implicit ev0: Ev0, ev1: Ev1, ) = 1 } // error // error
1111

@@ -55,4 +55,4 @@ object `package` {
5555

5656
case class Foo(foo: Any)
5757
case class Bar(foo: Any)
58-
}
58+
}

0 commit comments

Comments
 (0)