Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand span errorTermTree to include skipped span. #14492

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ object Parsers {
false
}

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

private var inFunReturnType = false
private def fromWithinReturnType[T](body: => T): T = {
Expand Down Expand Up @@ -1931,7 +1931,7 @@ object Parsers {
PolyFunction(tparams, body)
else {
syntaxError("Implementation restriction: polymorphic function literals must have a value parameter", arrowOffset)
errorTermTree
errorTermTree(arrowOffset)
}
}
case _ =>
Expand Down Expand Up @@ -2298,8 +2298,9 @@ object Parsers {
in.nextToken()
simpleExpr(location)
else
val start = in.lastOffset
syntaxErrorOrIncomplete(IllegalStartSimpleExpr(tokenString(in.token)), expectedOffset)
errorTermTree
errorTermTree(start)
}
simpleExprRest(t, location, canApply)
}
Expand Down Expand Up @@ -2738,8 +2739,9 @@ object Parsers {
case _ =>
if (isLiteral) literal(inPattern = true)
else {
val start = in.lastOffset
syntaxErrorOrIncomplete(IllegalStartOfSimplePattern(), expectedOffset)
errorTermTree
errorTermTree(start)
}
}

Expand Down Expand Up @@ -3314,8 +3316,9 @@ object Parsers {
}
val rhs2 =
if rhs.isEmpty && !isAllIds then
val start = in.lastOffset
syntaxError(ExpectedTokenButFound(EQUALS, in.token), Span(in.lastOffset))
errorTermTree
errorTermTree(start)
else
rhs
PatDef(mods, lhs, tpt, rhs2)
Expand Down Expand Up @@ -3483,11 +3486,12 @@ object Parsers {
case GIVEN =>
givenDef(start, mods, atSpan(in.skipToken()) { Mod.Given() })
case _ =>
val start = in.lastOffset
syntaxErrorOrIncomplete(ExpectedStartOfTopLevelDefinition())
mods.annotations match {
case head :: Nil => head
case Nil => EmptyTree
case all => Block(all, errorTermTree)
case all => Block(all, errorTermTree(start))
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/parsing/xml/MarkupParsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ object MarkupParsers {
try handle.parseAttribute(Span(start, curOffset, mid), tmp)
catch {
case e: RuntimeException =>
errorAndResult("error parsing attribute value", parser.errorTermTree)
errorAndResult("error parsing attribute value", parser.errorTermTree(parser.in.offset))
}

case '{' =>
Expand Down Expand Up @@ -334,7 +334,7 @@ object MarkupParsers {
finally parser.in.resume(saved)

if (output == null)
parser.errorTermTree
parser.errorTermTree(parser.in.offset)
else
output
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ class DiagnosticsTest {
| Nil.map(x => x).filter(x$m1 =>$m2)$m3
|}""".withSource
.diagnostics(m1,
(m2 to m2, "expression expected but ')' found", Error, Some(IllegalStartSimpleExprID)),
(m1 to m1, """Found: Null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another confusing error gone.

|Required: Boolean""".stripMargin, Error, Some(TypeMismatchID))
(m2 to m2, "expression expected but ')' found", Error, Some(IllegalStartSimpleExprID))
)

@Test def diagnosticPureExpression: Unit =
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/arg-eof.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
object Test:
case class Widget(name: String, other: Int = 5)
Widget(name = "foo", // error // error
Widget(name = "foo", // error
4 changes: 2 additions & 2 deletions tests/neg/errpos.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object Test {
val b = type // error: expression expected (on "type")

1 match {
case // error: pattern expected // error: cannot compare with Null
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error leaks the implementation detail that parser errors produce a null.

case // error: pattern expected
case 2 => ""
}
}
}
6 changes: 0 additions & 6 deletions tests/neg/i12150.check
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,3 @@
| expression expected but end found
|
| longer explanation available when compiling with `-explain`
-- [E129] Potential Issue Warning: tests/neg/i12150.scala:1:11 ---------------------------------------------------------
1 |def f: Unit = // error
| ^
| A pure expression does nothing in statement position; you may be omitting necessary parentheses
|
| longer explanation available when compiling with `-explain`
20 changes: 10 additions & 10 deletions tests/neg/i1846.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ object Test {
val x = 42
val Y = "42"

x match { case { 42 } => () } // error // error
x match { case { 42.toString } => () } // error // error
x match { case { 42 }.toString => () } // error // error
x match { case { 42 } => () } // error
x match { case { 42.toString } => () } // error
x match { case { 42 }.toString => () } // error
x match { case "42".toInt => () } // error
x match { case { "42".toInt } => () } // error // error
x match { case { "42" }.toInt => () } // error // error
x match { case { "42".toInt } => () } // error // error
x match { case { "42".toInt } => () } // error
x match { case { "42" }.toInt => () } // error
x match { case { "42".toInt } => () } // error
x match { case Y => () } // error
x match { case { Y.toInt } => () } // error // error
x match { case { Y }.toInt => () } // error // error
x match { case { Y }.toString => () } // error // error
x match { case { Y.toString } => () } // error // error
x match { case { Y.toInt } => () } // error
x match { case { Y }.toInt => () } // error
x match { case { Y }.toString => () } // error
x match { case { Y.toString } => () } // error
}
}
12 changes: 6 additions & 6 deletions tests/neg/i3812.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ object Test {
val x = 42
val Y = "42"

x match { case { 42 } => () } // error // error
x match { case { "42".toInt } => () } // error // error
x match { case { "42" }.toInt => () } // error // error
x match { case { "42".toInt } => () } // error // error
x match { case { Y.toInt } => () } // error // error
x match { case { Y }.toInt => () } // error // error
x match { case { 42 } => () } // error
x match { case { "42".toInt } => () } // error
x match { case { "42" }.toInt => () } // error
x match { case { "42".toInt } => () } // error
x match { case { Y.toInt } => () } // error
x match { case { Y }.toInt => () } // error
}
}
4 changes: 2 additions & 2 deletions tests/neg/i5004.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object i0 {
1 match {
def this(): Int // error
def this()
def this() // error
}
}
}
4 changes: 2 additions & 2 deletions tests/neg/i7742.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
object A {
for // error // error
}
for // error
}
2 changes: 1 addition & 1 deletion tests/neg/parser-stability-25.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ class D extends (Int => 1) {
}

class Wrap(x: Int)
class E extends (Wrap)( // error
class E extends (Wrap)(
// error
2 changes: 1 addition & 1 deletion tests/neg/parser-stability-27.scala
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
class F extends (Int => 1)( // error
class F extends (Int => 1)(
// error
2 changes: 1 addition & 1 deletion tests/neg/parser-stability-5.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trait x0 {
x1 : { // error
var x2 // error
var x2
// error
24 changes: 13 additions & 11 deletions tests/neg/t5702-neg-bad-and-wild.check
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:10:22 ---------------------------------------------------
10 | case List(1, _*,) => // error: pattern expected // error
10 | case List(1, _*,) => // error: pattern expected
| ^
| pattern expected
|
| longer explanation available when compiling with `-explain`
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:12:23 ---------------------------------------------------
12 | case List(1, _*3,) => // error: pattern expected // error // error
12 | case List(1, _*3,) => // error: pattern expected // error
| ^
| pattern expected
|
| longer explanation available when compiling with `-explain`
-- [E040] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:13:23 ---------------------------------------------------
13 | case List(1, _*3:) => // error // error
| ^
| an identifier expected, but ')' found
-- [E032] Syntax Error: tests/neg/t5702-neg-bad-and-wild.scala:15:18 ---------------------------------------------------
15 | case List(x*, 1) => // error: pattern expected
| ^
Expand All @@ -34,17 +38,15 @@
| x is already defined as value x
|
| Note that overloaded methods must all be defined in the same group of toplevel definitions
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:10:21 -----------------------------------------------------------------
10 | case List(1, _*,) => // error: pattern expected // error
| ^
| Values of types Null and Int cannot be compared with == or !=
-- [E006] Not Found Error: tests/neg/t5702-neg-bad-and-wild.scala:12:20 ------------------------------------------------
12 | case List(1, _*3,) => // error: pattern expected // error // error
12 | case List(1, _*3,) => // error: pattern expected // error
| ^
| Not found: *
|
| longer explanation available when compiling with `-explain`
-- [E006] Not Found Error: tests/neg/t5702-neg-bad-and-wild.scala:13:20 ------------------------------------------------
13 | case List(1, _*3:) => // error // error
| ^
| Not found: *
|
| longer explanation available when compiling with `-explain`
-- Error: tests/neg/t5702-neg-bad-and-wild.scala:12:22 -----------------------------------------------------------------
12 | case List(1, _*3,) => // error: pattern expected // error // error
| ^
| Values of types Null and Int cannot be compared with == or !=
6 changes: 3 additions & 3 deletions tests/neg/t5702-neg-bad-and-wild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ object Test {
val is = List(1,2,3)

is match {
case List(1, _*,) => // error: pattern expected // error
case List(1, _*,) => // error: pattern expected

case List(1, _*3,) => // error: pattern expected // error // error
//case List(1, _*3:) => // poor recovery by parens
case List(1, _*3,) => // error: pattern expected // error
case List(1, _*3:) => // error // error
case List(1, x*) => // ok
case List(x*, 1) => // error: pattern expected
case (1, x*) => //ok
Expand Down
10 changes: 5 additions & 5 deletions tests/neg/trailingCommas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package foo

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

trait ArgumentExprs1 { validMethod(23, "bar", )(Ev0, Ev1) } // error // error
trait ArgumentExprs2 { validMethod(23, "bar")(Ev0, Ev1, ) } // error // error
trait ArgumentExprs3 { new ValidClass(23, "bar", )(Ev0, Ev1) } // error // error
trait ArgumentExprs4 { new ValidClass(23, "bar")(Ev0, Ev1, ) } // error // error
trait ArgumentExprs1 { validMethod(23, "bar", )(Ev0, Ev1) } // error
trait ArgumentExprs2 { validMethod(23, "bar")(Ev0, Ev1, ) } // error
trait ArgumentExprs3 { new ValidClass(23, "bar", )(Ev0, Ev1) } // error
trait ArgumentExprs4 { new ValidClass(23, "bar")(Ev0, Ev1, ) } // error

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

Expand Down Expand Up @@ -55,4 +55,4 @@ object `package` {

case class Foo(foo: Any)
case class Bar(foo: Any)
}
}