You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@groovy.apache.org by "ocs@ocs.cz" <oc...@ocs.cz> on 2024/05/13 14:16:34 UTC

weird error report

Hi there,

the error in the following code is pretty plain, but the way it is being reported is seriously misleading. Is that the intended behaviour, or a compiler bug?

===
2074 ocs /tmp> <q.groovy 
def all=['hi','there'].findAll { it.startsWith('h')) }
2075 ocs /tmp> /usr/local/groovy-4.0.18/bin/groovy q
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/q.groovy: 1: Unexpected input: '{' @ line 1, column 32.
   def all=['hi','there'].findAll { it.startsWith('h')) }
                                  ^
1 error
2076 ocs /tmp> 
===

Thanks and all the best,
OC

Re: weird error report

Posted by OCsite <oc...@ocs.cz>.
Jochen,

> On 15. 5. 2024, at 14:18, Jochen Theodorou <bl...@gmx.org> wrote:
> On 13.05.24 16:16, ocs@ocs.cz wrote:
> [...]
>> 2074 ocs /tmp> <q.groovy
>> def all=['hi','there'].findAll { it.startsWith('h')) }
>> 2075 ocs /tmp> /usr/local/groovy-4.0.18/bin/groovy q
>> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
>> /private/tmp/q.groovy: 1: Unexpected input: '{' @ line 1, column 32.
>>    def all=['hi','there'].findAll { it.startsWith('h')) }
>>                                   ^
>> 1 error
>> 2076 ocs /tmp>
>> ===
> 
> I assume the problem is the )) for the startsWith.

Quite. I think other errors might cause the same problem, but I haven't tested it further now. On the other hand, I have meantime recalled I've bumped into this before, see please also the forwarded message below, which remained unanswered since I dumbly posted into users, not dev.

> The parser backtracks because there is no opening ( for the closing one and then finds that { can't be a valid input here. 

I see. On the other hand, with a normal non-closure block the error is reported properly:

===
1039 ocs /tmp> /usr/local/groovy-4.0.21/bin/groovy q
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/q.groovy: 1: Unexpected input: '{ bar())' @ line 1, column 15.
   if (0) { bar()) }
                 ^
1 error
1040 ocs /tmp> 
===

> Could you please make an issue for this? We should look into how we can improve this

https://issues.apache.org/jira/browse/GROOVY-11377

Thanks and all the best,
OC

> Begin forwarded message:
> 
> From: "ocs@ocs.cz" <oc...@ocs.cz>
> Subject: misplaced else inside a closure causes wrong (or at least pretty weird) error placement with Parrot
> Date: 19 May 2020 at 22:36:06 CEST
> To: users@groovy.apache.org
> Reply-To: users@groovy.apache.org
> 
> Hi there,
> 
> perhaps this is a known or well-understand issue, but worth reporting IMHO (not sure whether worth a ticket; let me please know should I create one for this):
> 
> ===
> 281 ocs /tmp> <q.groovy 
> class q {
>  def foo(val) {
>    10.times {
>      println "Essentially anything..."
> 
> 
> 
>      println "... can be here, the longer this code, the worse"
>      else println "This is wrong, but see the error report!"
>    }
>  }
> }
> 282 ocs /tmp> /usr/local/groovy-2.4.17/bin/groovyc q.groovy
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> q.groovy: 9: unexpected token: else @ line 9, column 7.
>         else println "This is wrong, but see the error report!"
>         ^
> 1 error
> 283 ocs /tmp> /usr/local/groovy-3.0.3/bin/groovyc q.groovy 
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> q.groovy: 3: Unexpected input: '{'; Expecting RBRACE @ line 3, column 14.
>       10.times {
>                ^
> 1 error
> 284 ocs /tmp> 
> ===
> 
> All the best,
> OC

Re: weird error report

Posted by Jochen Theodorou <bl...@gmx.org>.
On 13.05.24 16:16, ocs@ocs.cz wrote:
[...]
> 2074 ocs /tmp> <q.groovy
> def all=['hi','there'].findAll { it.startsWith('h')) }
> 2075 ocs /tmp> /usr/local/groovy-4.0.18/bin/groovy q
> org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
> /private/tmp/q.groovy: 1: Unexpected input: '{' @ line 1, column 32.
>     def all=['hi','there'].findAll { it.startsWith('h')) }
>                                    ^
> 1 error
> 2076 ocs /tmp>
> ===

I assume the problem is the )) for the startsWith. The parser backtracks
because there is no opening ( for the closing one and then finds that {
can't be a valid input here.

Could you please make an issue for this? We should look into how we can
improve this

bye  Jochen

Re: [EXT] weird error report

Posted by OCsite <oc...@ocs.cz>.
Miles,

> On 13. 5. 2024, at 17:43, Milles, Eric (TR Technology) via dev <de...@groovy.apache.org> wrote:
> When the parser cannot work out the meaning, it tends to report "unexpected input".

I don't complain at all that the error is reported as an “unexpected input”; I do apologise I haven't explained it in detail in my original e-mail, seemed to me sort of obvious, mea culpa.

My complaint is that it reports the error very very far from the place it actually occurs. If the “unexpected input“ was reported at the extra “)”, or about anywhere at the same line, it would be completely all right and I'd be happy.

What stroke me as seriously misleading is that the error is reported at a perfectly valid “{” at the very beginning of the block. It can happen with much longer blocks just as well, in which case the potential for confusion is even considerably bigger:

===
1025 ocs /tmp> <q.groovy 
def all=['hi','there'].findAll {
  def foo=it+'bar'
  if (foo.length>7) return false
  // yadda yadda...
  // ... looks like essentially any number...
  // ... of rows can be here
  it.startsWith('h')) // *NOTE* bug is here! The compiler is really not helping at all finding and fixing it
}
1026 ocs /tmp> /usr/local/groovy-4.0.21/bin/groovy q
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
/private/tmp/q.groovy: 1: Unexpected input: '{' @ line 1, column 32.
   def all=['hi','there'].findAll {
                                  ^
1 error
1027 ocs /tmp> 
===

The problem seems to occur with closures only. If the findAll at the first line is replaced by something like for (s in ['hi','there']) {, the error would be reported properly at the right place, at the extra ')'.

> There is a ticket about parser recovery that may address some types of errors like the extra closing paren in your example.  But it has not gotten off the ground.
> 
> https://issues.apache.org/jira/browse/GROOVY-9192
> 
> You can also keep an eye on all the Parrot Parser bugs here:
> 
> https://issues.apache.org/jira/issues/?jql=project+%3D+GROOVY+AND+component+%3D+parser-antlr4

Thanks, I'll check in there when I get more time.

All the best,
OC


Re: [EXT] weird error report

Posted by "Milles, Eric (TR Technology) via dev" <de...@groovy.apache.org>.
When the parser cannot work out the meaning, it tends to report "unexpected input".  There is a ticket about parser recovery that may address some types of errors like the extra closing paren in your example.  But it has not gotten off the ground.

https://issues.apache.org/jira/browse/GROOVY-9192

You can also keep an eye on all the Parrot Parser bugs here:

https://issues.apache.org/jira/issues/?jql=project+%3D+GROOVY+AND+component+%3D+parser-antlr4