rust/src/test/ui/consts/control-flow/loop.rs

98 lines
2.0 KiB
Rust
Raw Normal View History

// Ensure that loops are forbidden in a const context unless `#![feature(const_loop)]` is enabled.
2019-12-11 05:11:53 +00:00
// gate-test-const_loop
2020-05-21 19:49:38 +00:00
// revisions: stock loop_
2020-05-21 19:49:38 +00:00
#![cfg_attr(loop_, feature(const_loop))]
2020-05-21 19:49:38 +00:00
const _: () = loop {}; //[stock]~ ERROR `loop` is not allowed in a `const`
2020-05-21 19:49:38 +00:00
static FOO: i32 = loop { break 4; }; //[stock]~ ERROR `loop` is not allowed in a `static`
const fn foo() {
2020-05-21 19:49:38 +00:00
loop {} //[stock]~ ERROR `loop` is not allowed in a `const fn`
}
pub trait Foo {
2020-05-21 19:49:38 +00:00
const BAR: i32 = loop { break 4; }; //[stock]~ ERROR `loop` is not allowed in a `const`
}
impl Foo for () {
2020-05-21 19:49:38 +00:00
const BAR: i32 = loop { break 4; }; //[stock]~ ERROR `loop` is not allowed in a `const`
}
fn non_const_outside() {
const fn const_inside() {
2020-05-21 19:49:38 +00:00
loop {} //[stock]~ ERROR `loop` is not allowed in a `const fn`
}
}
const fn const_outside() {
fn non_const_inside() {
loop {}
}
}
fn main() {
let x = [0; {
while false {}
2020-05-21 19:49:38 +00:00
//[stock]~^ ERROR `while` is not allowed in a `const`
4
}];
}
const _: i32 = {
let mut x = 0;
2020-05-21 19:49:38 +00:00
while x < 4 { //[stock]~ ERROR `while` is not allowed in a `const`
x += 1;
}
2020-05-21 19:49:38 +00:00
while x < 8 { //[stock]~ ERROR `while` is not allowed in a `const`
x += 1;
}
x
};
const _: i32 = {
let mut x = 0;
2020-05-21 19:49:38 +00:00
for i in 0..4 { //[stock,loop_]~ ERROR `for` is not allowed in a `const`
x += i;
}
2020-05-21 19:49:38 +00:00
for i in 0..4 { //[stock,loop_]~ ERROR `for` is not allowed in a `const`
x += i;
}
x
};
const _: i32 = {
let mut x = 0;
2020-05-21 19:49:38 +00:00
loop { //[stock]~ ERROR `loop` is not allowed in a `const`
x += 1;
2020-05-21 19:49:38 +00:00
if x == 4 {
break;
}
}
2020-05-21 19:49:38 +00:00
loop { //[stock]~ ERROR `loop` is not allowed in a `const`
x += 1;
2020-05-21 19:49:38 +00:00
if x == 8 {
break;
}
}
x
};
const _: i32 = {
let mut x = 0;
2020-05-21 19:49:38 +00:00
while let None = Some(x) { } //[stock]~ ERROR `while` is not allowed in a `const`
while let None = Some(x) { } //[stock]~ ERROR `while` is not allowed in a `const`
x
};