Skip to content

break allowed outside of loops #497

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

Closed
OddCoincidence opened this issue Feb 18, 2019 · 5 comments
Closed

break allowed outside of loops #497

OddCoincidence opened this issue Feb 18, 2019 · 5 comments

Comments

@OddCoincidence
Copy link
Contributor

The following should be a syntax error, but is allowed and causes a panic at runtime:

def illegal_break():
     break
@coolreader18
Copy link
Member

Can this be closed now?

@OddCoincidence
Copy link
Contributor Author

There are a few more edge cases not covered by the fix. Also a similar problem exists for yield and return being allowed outside of functions.

@Rustinante
Copy link
Contributor

Rustinante commented Feb 25, 2019

The current fix probably cannot handle breaks in nested loops, because having only a boolean indicator in_loop is not enough to keep track of the nesting. For example,

def f():
    a = 3
    while a:
        for i in range(10):
            print(a, i)
            if i == 2:
                break
        a -= 1
        if a == 1:
            break

The self.in_loop will be reset to false at the end of the for loop, but we will still be inside the while loop.

One solution might be to use an integer to keep track of the level of nesting.

@OddCoincidence
Copy link
Contributor Author

Yeah, this is the edge case I was alluding to above.

The in_loop flag should actually be enough, if instead of setting to false, it just saves and then restores the prior value.

@Rustinante
Copy link
Contributor

Rustinante commented Feb 25, 2019

@OddCoincidence please take a look at the PR #542 for the fix. Thank you~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants