com-lihaoyi / fastparse

Writing Fast Parsers Fast in Scala
https://com-lihaoyi.github.io/fastparse
MIT License
1.09k stars 164 forks source link

Syntax error of typo on `def` is not reported if it's not type annotated. #16

Closed taisukeoe closed 9 years ago

taisukeoe commented 9 years ago

I happen to find bugs about syntax error reporting of typo on def,val or var, which are not occurred if method or variable doesn't have type annotation. I assume they should be reported as syntax error even if they're not type annotated

Here are examples of method with typo on def. Behavior of variable with typo on val or var is the same.

scala> val p0 = scalaparse.Scala.CompilationUnit
p0: fastparse.P0 = CompilationUnit

//Reporting typo of `def`, if return type is annotated
scala> p0.parse("object A{def i:Int = 1}",0,true)
res24: fastparse.core.Result[Unit] = Success((), 23)

scala> p0.parse("object A{de i:Int = 1}",0,true)
res25: fastparse.core.Result[Unit] = Failure(CompilationUnit:0 / Body:0 / TopStatSeq:0 / TopStat:0 / Tmpl:0 / ObjDef:0 / DefTmpl:8 / TmplBody:8 / }:17 / "}":18 ..."= 1}", true)

//Not reporting typo of `def`, if return type is NOT annotated
scala> p0.parse("object A{def i = 1}",0,true)
res26: fastparse.core.Result[Unit] = Success((), 19)

scala> p0.parse("object A{de i = 1}",0,true)
res27: fastparse.core.Result[Unit] = Success((), 18)
lihaoyi commented 9 years ago

This is not a bug. Or if it is, it's not my bug. That is perfectly valid Scala:

scala> object de{ var i = 0 }
defined object de

scala> object A{ de i: Int = 1 }
<console>:1: error: ';' expected but '=' found.
       object A{ de i: Int = 1 }
                           ^

scala> object A{ de i = 1 }
warning: there was one feature warning; re-run with -feature for details
defined object A

scala> de.i
res1: Int = 0

scala> A
res2: A.type = A$@2b4ad817

scala> de.i
res3: Int = 1
taisukeoe commented 9 years ago

I got it. Thanks.