Issue Details (XML | Word | Printable)

Key: GROOVY-2523
Type: Bug Bug
Status: Closed Closed
Resolution: Fixed
Priority: Major Major
Assignee: Paul King
Reporter: Daniel.Sun
Votes: 0
Watchers: 1
Operations

If you were logged in you would be able to see more operations.
groovy

compiler complains for loop has empty body

Created: 20/Jan/08 02:25 AM   Updated: 17/Apr/08 07:26 AM   Resolved: 22/Feb/08 08:28 PM
Component/s: None
Affects Version/s: 1.5.1
Fix Version/s: 1.5.5, 1.6-beta-1

Time Tracking:
Not Specified

File Attachments: 1. Text File GROOVY-2523_Empty_for_and_while_loop_scenarios.patch (25 kB)
2. Text File GROOVY-2523_Empty_for_loop_scenarios.patch (2 kB)

Environment: jdk6u4
Issue Links:
Duplicate
 


 Description  « Hide

compiler complains for loop has empty body

for (int i = 0; i < 10; i++) ;

groovy> for (int i = 0; i < 10; i++) ;

Exception thrown: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script1: 1: unexpected token: ; @ line 1, column 30.
1 error



Guillaume Laforge added a comment - 20/Jan/08 03:12 AM

By the way, what's the use of a for loop which does nothing at all???


Daniel.Sun added a comment - 20/Jan/08 03:20 AM

For example,

def index(str, substr) {
    for (int i = 0; i < str.length(); i++) {
        int k
        for (k = 0; k < substr.length() && substr[k] == str[i + k]; k++) print ""; // Now I have to express 'do nothing' through print ""
        if (k == substr.length())
            return i;
    }

    return -1;
}

s1 = "abcdef"
s2 = "cde"

println index(s1, s2)

Guillaume Laforge added a comment - 20/Jan/08 03:23 AM

Just use: for(...) {}
Or even use a while loop.
I think it makes the code more readable anyway.


Daniel.Sun added a comment - 20/Jan/08 03:23 AM

By the way,

If I omit the initialization part in for loop like the following code:

int k = 0;
for (; k < 10; k++) {
    println k;
}

failed too
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed, Script3: 2: unexpected token: ; @ line 2, column 6.
1 error


Russel Winder added a comment - 21/Jan/08 02:17 AM

if we are going to have C/C++/Java style for loops then having ; as the body has to work. I am not sure it is acceptable to say "you must use {} as the body".

An interesting question is whether "for ( . . . ) \n" should be valid since Groovy has optional semicolons.


Paul King added a comment - 21/Jan/08 03:39 AM

Attached is a patch which fixes these two issues. Not applying until Jochen double checks.


Paul King added a comment - 21/Jan/08 03:43 AM

Jochen, can you provide feedback on this issue. Let me know if you are opposed to the change but if not, check if the patch looks OK. I am happy if you want to apply, otherwise assign to me and I will apply with appropriate tests.


Paul King added a comment - 21/Jan/08 04:21 AM

To answer Russel's question, currently (and with the attached patch) a for without a semicolon "takes the next line" as the statement, e.g.:

int k
for (k=0; k < 3; k++)
    println 'inner' + k
println 'outer' + k

yields:

inner0
inner1
inner2
outer3

and of course, adding the semicolon on to the for loop will yield:

inner3
outer3

which is a common garden variety bug which would be good to help eliminate. Without the patch we eliminate it by providing a horrible error message. With the patch we let it go but it is most likely to catch people who continue to use semicolons when doing Groovy, i.e. if you don't normally use semicollons and you do have one at the end of the for loop you are likely to intentionally rather than accidentally want it. I think with the patch is the preferred approach.


Paul King added a comment - 22/Jan/08 08:40 AM - edited

Also, just noted that while loops have the same limitation. I'll extend the patch to also allow empty while statements tomorrow unless I hear that this isn't desirable. I don't think it would normally be good style but I don't think it should be illegal.


Paul King added a comment - 23/Jan/08 12:25 AM

Added patch inluding empty while fix.


Paul King added a comment - 23/Jan/08 04:34 PM

Any feedback? Otherwise I will add the patch to HEAD on the weekend. Should I merge to 1_5_x or is the plan to do that all in one go for anything else that needs merging?


Paul King added a comment - 25/Jan/08 06:42 AM

Patch added to HEAD.


Daniel.Sun added a comment - 25/Jan/08 06:45 AM

Thanks Pual


Paul King added a comment - 22/Feb/08 08:28 PM

merged to 1_5_x branch