diff options
| author | bors <bors@rust-lang.org> | 2016-11-22 17:51:59 -0600 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2016-11-22 17:51:59 -0600 |
| commit | 1cabe2151299c63497abc3a20bd08c04c0cd32a3 (patch) | |
| tree | d5117e6a154299f1ca35561e42e53fc59bb81d52 /src/test/compile-fail | |
| parent | 3bf2be9ceea90b650105cd1f78ad5a098a0d158d (diff) | |
| parent | 9d42549df40464899dda11fc9509f511046fb4c6 (diff) | |
| download | rust-1cabe2151299c63497abc3a20bd08c04c0cd32a3.tar.gz rust-1cabe2151299c63497abc3a20bd08c04c0cd32a3.zip | |
Auto merge of #37487 - goffrie:break, r=nikomatsakis
Implement the `loop_break_value` feature.
This implements RFC 1624, tracking issue #37339.
- `FnCtxt` (in typeck) gets a stack of `LoopCtxt`s, which store the
currently deduced type of that loop, the desired type, and a list of
break expressions currently seen. `loop` loops get a fresh type
variable as their initial type (this logic is stolen from that for
arrays). `while` loops get `()`.
- `break {expr}` looks up the broken loop, and unifies the type of
`expr` with the type of the loop.
- `break` with no expr unifies the loop's type with `()`.
- When building MIR, loops no longer construct a `()` value at
termination of the loop; rather, the `break` expression assigns the
result of the loop.
- ~~I have also changed the loop scoping in MIR-building so that the test
of a while loop is not considered to be part of that loop. This makes
the rules consistent with #37360. The new loop scopes in typeck also
follow this rule. That means that `loop { while (break) {} }` now
terminates instead of looping forever. This is technically a breaking
change.~~
- ~~On that note, expressions like `while break {}` and `if break {}` no
longer parse because `{}` is interpreted as an expression argument to
`break`. But no code except compiler test cases should do that anyway
because it makes no sense.~~
- The RFC did not make it clear, but I chose to make `break ()` inside
of a `while` loop illegal, just in case we wanted to do anything with
that design space in the future.
This is my first time dealing with this part of rustc so I'm sure
there's plenty of problems to pick on here ^_^
Diffstat (limited to 'src/test/compile-fail')
| -rw-r--r-- | src/test/compile-fail/feature-gate-loop-break-value.rs | 15 | ||||
| -rw-r--r-- | src/test/compile-fail/loop-break-value.rs | 101 |
2 files changed, 116 insertions, 0 deletions
diff --git a/src/test/compile-fail/feature-gate-loop-break-value.rs b/src/test/compile-fail/feature-gate-loop-break-value.rs new file mode 100644 index 00000000000..1632c40d59f --- /dev/null +++ b/src/test/compile-fail/feature-gate-loop-break-value.rs @@ -0,0 +1,15 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + loop { + break 123; //~ ERROR `break` with a value is experimental + } +} diff --git a/src/test/compile-fail/loop-break-value.rs b/src/test/compile-fail/loop-break-value.rs new file mode 100644 index 00000000000..d4f29597486 --- /dev/null +++ b/src/test/compile-fail/loop-break-value.rs @@ -0,0 +1,101 @@ +// Copyright 2016 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(loop_break_value)] +#![feature(never_type)] + +fn main() { + let val: ! = loop { break break; }; + //~^ ERROR mismatched types + + loop { + if true { + break "asdf"; + } else { + break 123; //~ ERROR mismatched types + } + }; + + let _: i32 = loop { + break "asdf"; //~ ERROR mismatched types + }; + + let _: i32 = 'outer_loop: loop { + loop { + break 'outer_loop "nope"; //~ ERROR mismatched types + break "ok"; + }; + }; + + 'while_loop: while true { + break; + break (); //~ ERROR `break` with value from a `while` loop + loop { + break 'while_loop 123; + //~^ ERROR `break` with value from a `while` loop + //~| ERROR mismatched types + break 456; + break 789; + }; + } + + 'while_let_loop: while let Some(_) = Some(()) { + if break () { //~ ERROR `break` with value from a `while let` loop + break; + break None; + //~^ ERROR `break` with value from a `while let` loop + //~| ERROR mismatched types + } + loop { + break 'while_let_loop "nope"; + //~^ ERROR `break` with value from a `while let` loop + //~| ERROR mismatched types + break 33; + }; + } + + 'for_loop: for _ in &[1,2,3] { + break (); //~ ERROR `break` with value from a `for` loop + break [()]; + //~^ ERROR `break` with value from a `for` loop + //~| ERROR mismatched types + loop { + break Some(3); + break 'for_loop Some(17); + //~^ ERROR `break` with value from a `for` loop + //~| ERROR mismatched types + }; + } + + let _: i32 = 'a: loop { + let _: () = 'b: loop { + break ('c: loop { + break; + break 'c 123; //~ ERROR mismatched types + }); + break 'a 123; + }; + }; + + loop { + break (break, break); //~ ERROR mismatched types + }; + + loop { + break; + break 2; //~ ERROR mismatched types + }; + + loop { + break 2; + break; //~ ERROR mismatched types + break 4; + }; +} |
