Skip to content
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

Bad behaviour with break in while-loop condition #21751

Closed
arielb1 opened this issue Jan 29, 2015 · 9 comments
Closed

Bad behaviour with break in while-loop condition #21751

arielb1 opened this issue Jan 29, 2015 · 9 comments
Labels
I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.

Comments

@arielb1
Copy link
Contributor

arielb1 commented Jan 29, 2015

If you put a break in the condition of a while-loop, the compiler code seems to exhibit undefined behaviour.

For example, in play.rust-lang.org, this SIGILL-s when -O0 and prints 5 blank lines when -O2:

fn main() {
    for _ in 0..5 {
        while { break; } { println!("never"); }
        println!("?");
    }
}
@sfackler sfackler added I-wrong I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics. labels Jan 29, 2015
@pnkfelix
Copy link
Member

Seems like this may have been fixed?

Marking as needs-test.

@pnkfelix pnkfelix added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Apr 11, 2015
@untitaker
Copy link
Contributor

This SIGILLs for me at rustc 1.1.0-nightly (f9e53c7f2 2015-04-24) (built 2015-04-25).

@pnkfelix
Copy link
Member

indeed, removing the E-needstest label.

@pnkfelix pnkfelix removed the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Apr 28, 2015
@pnkfelix
Copy link
Member

Interestingly, adding explicit labels to both of the loops and then making the break go directly to either label exhibits entirely different behaviors -- in particular, breaking out of the for seems to work fine, and attempting to break out of the while from its own condition is statically rejected.

I wonder if this means that this is some sort of trans issue where it is simply guessing incorrectly for what the target of the break should be? (At least, I would think that the break in the original code is breaking out of the foo-loop...)

@untitaker
Copy link
Contributor

Removing the wrapping braces around break gives the correct behavior.

@apasel422
Copy link
Contributor

Still an issue.

@leoyvens
Copy link
Contributor

Triage: The code in the OP is working fine in the playpen for Debug and Release.

This seems fixed, however the scenario with labels described by @pnkfelix ICEs, I have opened #37353 to track that.

@nikomatsakis
Copy link
Contributor

The original example does not work fine -- the println("?") line prints "unreachable statement", though it does indeed execute:

rustc 1.15.1 (021bd294c 2017-02-08)
warning: unreachable statement, #[warn(unreachable_code)] on by default
 --> <anon>:3:28
  |
3 |         while { break; } { println!("never"); }
  |                            ^^^^^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate

warning: unreachable statement, #[warn(unreachable_code)] on by default
 --> <anon>:4:9
  |
4 |         println!("?");
  |         ^^^^^^^^^^^^^^
  |
  = note: this error originates in a macro outside of the current crate

?
?
?
?
?

Program ended.

@nikomatsakis
Copy link
Contributor

I'm going to close this issue in favor of #37576 which includes more examples and the official decision (that this code ought to be an error -- break with no label in while condition is counter-intuitive and should not be accepted).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
I-crash Issue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.
Projects
None yet
Development

No branches or pull requests

7 participants