From 67336bb399deac766faed5e13a032f017c3353b3 Mon Sep 17 00:00:00 2001 From: Dylan MacKenzie Date: Wed, 6 Nov 2019 12:26:59 -0800 Subject: [PATCH] Extend const-loop and const-if to handle more cases This makes sure that our HIR visitor is visiting as many const-items as possible. --- src/test/ui/consts/const-if.rs | 22 ++++++++-- src/test/ui/consts/const-loop.rs | 70 ++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 21 deletions(-) diff --git a/src/test/ui/consts/const-if.rs b/src/test/ui/consts/const-if.rs index 9bb5bcc499e..440723a3fe1 100644 --- a/src/test/ui/consts/const-if.rs +++ b/src/test/ui/consts/const-if.rs @@ -1,5 +1,21 @@ -const _X: i32 = if true { 5 } else { 6 }; -//~^ ERROR constant contains unimplemented expression type -//~| ERROR constant contains unimplemented expression type +const _: i32 = if true { //~ ERROR if expression is not allowed in a const + 5 +} else { + 6 +}; + +const _: i32 = match 1 { //~ ERROR match expression is not allowed in a const + 2 => 3, + 4 => 5, + _ => 0, +}; + +const fn foo() -> i32 { + if true { 5 } else { 6 } //~ ERROR if expression is not allowed in a const fn +} + +const fn bar() -> i32 { + match 0 { 1 => 2, _ => 0 } //~ ERROR match expression is not allowed in a const fn +} fn main() {} diff --git a/src/test/ui/consts/const-loop.rs b/src/test/ui/consts/const-loop.rs index 954f269d30e..b94f3de4d47 100644 --- a/src/test/ui/consts/const-loop.rs +++ b/src/test/ui/consts/const-loop.rs @@ -1,13 +1,49 @@ +const _: () = loop {}; //~ ERROR loop is not allowed in a const + +static FOO: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a static + +const fn foo() { + loop {} //~ ERROR loop is not allowed in a const fn +} + +pub trait Foo { + const BAR: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a const +} + +impl Foo for () { + const BAR: i32 = loop { break 4; }; //~ ERROR loop is not allowed in a const +} + +fn non_const_outside() { + const fn const_inside() { + loop {} //~ 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 {} + //~^ ERROR `while` is not allowed in a `const` + //~| ERROR constant contains unimplemented expression type + //~| ERROR constant contains unimplemented expression type + 4 + }]; +} + const _: i32 = { let mut x = 0; - while x < 4 { - //~^ ERROR constant contains unimplemented expression type - //~| ERROR constant contains unimplemented expression type + while x < 4 { //~ ERROR while loop is not allowed in a const x += 1; } - while x < 8 { + while x < 8 { //~ ERROR while loop is not allowed in a const x += 1; } @@ -17,16 +53,11 @@ const _: i32 = { const _: i32 = { let mut x = 0; - for i in 0..4 { - //~^ ERROR constant contains unimplemented expression type - //~| ERROR constant contains unimplemented expression type - //~| ERROR references in constants may only refer to immutable values - //~| ERROR calls in constants are limited to constant functions, tuple - // structs and tuple variants + for i in 0..4 { //~ ERROR for loop is not allowed in a const x += i; } - for i in 0..4 { + for i in 0..4 { //~ ERROR for loop is not allowed in a const x += i; } @@ -36,18 +67,16 @@ const _: i32 = { const _: i32 = { let mut x = 0; - loop { + loop { //~ ERROR loop is not allowed in a const x += 1; - if x == 4 { - //~^ ERROR constant contains unimplemented expression type - //~| ERROR constant contains unimplemented expression type + if x == 4 { //~ ERROR if expression is not allowed in a const break; } } - loop { + loop { //~ ERROR loop is not allowed in a const x += 1; - if x == 8 { + if x == 8 { //~ ERROR if expression is not allowed in a const break; } } @@ -55,4 +84,9 @@ const _: i32 = { x }; -fn main() {} +const _: i32 = { + let mut x = 0; + while let None = Some(x) { } //~ ERROR while loop is not allowed in a const + while let None = Some(x) { } //~ ERROR while loop is not allowed in a const + x +};