groovy

compiler complains for loop has empty body

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 1.5.1
  • Fix Version/s: 1.5.5, 1.6-beta-1
  • Component/s: None
  • Labels:
    None
  • Environment:
    jdk6u4
  • Number of attachments :
    2

Description

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

Issue Links

Activity

Hide
Guillaume Laforge added a comment -

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

Show
Guillaume Laforge added a comment - By the way, what's the use of a for loop which does nothing at all???
Hide
Daniel.Sun added a comment -

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)
Show
Daniel.Sun added a comment - 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)
Hide
Guillaume Laforge added a comment -

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

Show
Guillaume Laforge added a comment - Just use: for(...) {} Or even use a while loop. I think it makes the code more readable anyway.
Hide
Daniel.Sun added a comment -

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

Show
Daniel.Sun added a comment - 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
Hide
Russel Winder added a comment -

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.

Show
Russel Winder added a comment - 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.
Hide
Paul King added a comment -

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

Show
Paul King added a comment - Attached is a patch which fixes these two issues. Not applying until Jochen double checks.
Hide
Paul King added a comment -

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.

Show
Paul King added a comment - 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.
Hide
Paul King added a comment -

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.

Show
Paul King added a comment - 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.
Hide
Paul King added a comment - - 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.

Show
Paul King added a comment - - 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.
Hide
Paul King added a comment -

Added patch inluding empty while fix.

Show
Paul King added a comment - Added patch inluding empty while fix.
Hide
Paul King added a comment -

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?

Show
Paul King added a comment - 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?
Hide
Paul King added a comment -

Patch added to HEAD.

Show
Paul King added a comment - Patch added to HEAD.
Hide
Daniel.Sun added a comment -

Thanks Pual

Show
Daniel.Sun added a comment - Thanks Pual
Hide
Paul King added a comment -

merged to 1_5_x branch

Show
Paul King added a comment - merged to 1_5_x branch

People

Vote (0)
Watch (1)

Dates

  • Created:
    Updated:
    Resolved: