diff options
| author | bors <bors@rust-lang.org> | 2018-05-22 12:57:06 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-05-22 12:57:06 +0000 |
| commit | 9f80ea32811db0bdbd05b1bef59f5db26b3e24a9 (patch) | |
| tree | 6a347360749ddc9462a714f5cc00ffdd0efd0cdb /src/test | |
| parent | ff8fa5cc69db5567b32ceca1ee4ac0dcfa3a81bc (diff) | |
| parent | 2483c812176c8ecca54207517a2ba5500805b205 (diff) | |
| download | rust-9f80ea32811db0bdbd05b1bef59f5db26b3e24a9.tar.gz rust-9f80ea32811db0bdbd05b1bef59f5db26b3e24a9.zip | |
Auto merge of #49172 - oli-obk:const_let, r=eddyb
Allow let bindings and destructuring in constants and const fn r? @eddyb cc https://github.com/rust-lang/rust/issues/48821
Diffstat (limited to 'src/test')
19 files changed, 278 insertions, 37 deletions
diff --git a/src/test/compile-fail/const-block-non-item-statement-2.rs b/src/test/compile-fail/const-block-non-item-statement-2.rs index 83166c9bd4b..f80d55cb342 100644 --- a/src/test/compile-fail/const-block-non-item-statement-2.rs +++ b/src/test/compile-fail/const-block-non-item-statement-2.rs @@ -9,18 +9,20 @@ // except according to those terms. const A: usize = { 1; 2 }; -//~^ ERROR: blocks in constants are limited to items and tail expressions +//~^ ERROR statements in constants are unstable const B: usize = { { } 2 }; -//~^ ERROR: blocks in constants are limited to items and tail expressions +//~^ ERROR statements in constants are unstable macro_rules! foo { - () => (()) //~ ERROR: blocks in constants are limited to items and tail expressions + () => (()) //~ ERROR statements in constants are unstable } const C: usize = { foo!(); 2 }; const D: usize = { let x = 4; 2 }; -//~^ ERROR: blocks in constants are limited to items and tail expressions -//~^^ ERROR: blocks in constants are limited to items and tail expressions +//~^ ERROR let bindings in constants are unstable +//~| ERROR statements in constants are unstable +//~| ERROR let bindings in constants are unstable +//~| ERROR statements in constants are unstable pub fn main() {} diff --git a/src/test/compile-fail/const-block-non-item-statement-3.rs b/src/test/compile-fail/const-block-non-item-statement-3.rs index 70703791101..cfa4b778dde 100644 --- a/src/test/compile-fail/const-block-non-item-statement-3.rs +++ b/src/test/compile-fail/const-block-non-item-statement-3.rs @@ -9,7 +9,9 @@ // except according to those terms. type Array = [u32; { let x = 2; 5 }]; -//~^ ERROR: blocks in constants are limited to items and tail expressions -//~^^ ERROR: blocks in constants are limited to items and tail expressions +//~^ ERROR let bindings in constants are unstable +//~| ERROR statements in constants are unstable +//~| ERROR let bindings in constants are unstable +//~| ERROR statements in constants are unstable pub fn main() {} diff --git a/src/test/compile-fail/const-block-non-item-statement.rs b/src/test/compile-fail/const-block-non-item-statement.rs index 802e660b904..f974a24c26f 100644 --- a/src/test/compile-fail/const-block-non-item-statement.rs +++ b/src/test/compile-fail/const-block-non-item-statement.rs @@ -10,8 +10,10 @@ enum Foo { Bar = { let x = 1; 3 } - //~^ ERROR: blocks in constants are limited to items and tail expressions - //~^^ ERROR: blocks in constants are limited to items and tail expressions + //~^ ERROR let bindings in constants are unstable + //~| ERROR statements in constants are unstable + //~| ERROR let bindings in constants are unstable + //~| ERROR statements in constants are unstable } pub fn main() {} diff --git a/src/test/compile-fail/const-fn-destructuring-arg.rs b/src/test/compile-fail/const-fn-destructuring-arg.rs index c3d5975fe01..fce1688716d 100644 --- a/src/test/compile-fail/const-fn-destructuring-arg.rs +++ b/src/test/compile-fail/const-fn-destructuring-arg.rs @@ -8,16 +8,20 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// test that certain things are disallowed in const fn signatures +// test that certain things are disallowed in constant functions #![feature(const_fn)] // no destructuring const fn i(( - a, //~ ERROR: E0022 - b //~ ERROR: E0022 + a, + //~^ ERROR arguments of constant functions can only be immutable by-value bindings + b + //~^ ERROR arguments of constant functions can only be immutable by-value bindings ): (u32, u32)) -> u32 { a + b + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR let bindings in constant functions are unstable } fn main() {} diff --git a/src/test/compile-fail/const-fn-not-safe-for-const.rs b/src/test/compile-fail/const-fn-not-safe-for-const.rs index 48877a60d25..d985bae1f24 100644 --- a/src/test/compile-fail/const-fn-not-safe-for-const.rs +++ b/src/test/compile-fail/const-fn-not-safe-for-const.rs @@ -38,9 +38,15 @@ const fn get_Y_addr() -> &'static u32 { } const fn get() -> u32 { - let x = 22; //~ ERROR E0016 - let y = 44; //~ ERROR E0016 + let x = 22; + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR statements in constant functions are unstable + let y = 44; + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR statements in constant functions are unstable x + y + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR let bindings in constant functions are unstable } fn main() { diff --git a/src/test/compile-fail/issue-18118.rs b/src/test/compile-fail/issue-18118.rs index 35e57dffb6c..7194c159c1e 100644 --- a/src/test/compile-fail/issue-18118.rs +++ b/src/test/compile-fail/issue-18118.rs @@ -10,9 +10,12 @@ pub fn main() { const z: &'static isize = { - //~^ ERROR blocks in constants are limited to items and tail expressions + //~^ ERROR let bindings in constants are unstable + //~| ERROR statements in constants are unstable let p = 3; - //~^ ERROR blocks in constants are limited to items and tail expressions + //~^ ERROR let bindings in constants are unstable + //~| ERROR statements in constants are unstable &p //~ ERROR `p` does not live long enough + //~^ ERROR let bindings in constants are unstable }; } diff --git a/src/test/compile-fail/issue-37550.rs b/src/test/compile-fail/issue-37550.rs index e1f7f64e01a..af1f6ef5ed4 100644 --- a/src/test/compile-fail/issue-37550.rs +++ b/src/test/compile-fail/issue-37550.rs @@ -11,8 +11,12 @@ #![feature(const_fn)] const fn x() { - let t = true; //~ ERROR blocks in constant functions are limited to items and tail expressions - let x = || t; //~ ERROR blocks in constant functions are limited to items and tail expressions + let t = true; + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR statements in constant functions are unstable + let x = || t; + //~^ ERROR let bindings in constant functions are unstable + //~| ERROR statements in constant functions are unstable } fn main() {} diff --git a/src/test/compile-fail/issue32829.rs b/src/test/compile-fail/issue32829.rs index 9a84322ad06..2b223bac8e6 100644 --- a/src/test/compile-fail/issue32829.rs +++ b/src/test/compile-fail/issue32829.rs @@ -14,7 +14,8 @@ const bad : u32 = { { - 5; //~ ERROR: blocks in constants are limited to items and tail expressions + 5; + //~^ ERROR statements in constants are unstable 0 } }; @@ -22,7 +23,7 @@ const bad : u32 = { const bad_two : u32 = { { invalid(); - //~^ ERROR: blocks in constants are limited to items and tail expressions + //~^ ERROR statements in constants are unstable //~^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants 0 } @@ -31,14 +32,15 @@ const bad_two : u32 = { const bad_three : u32 = { { valid(); - //~^ ERROR: blocks in constants are limited to items and tail expressions + //~^ ERROR statements in constants are unstable 0 } }; static bad_four : u32 = { { - 5; //~ ERROR: blocks in statics are limited to items and tail expressions + 5; + //~^ ERROR statements in statics are unstable 0 } }; @@ -46,8 +48,8 @@ static bad_four : u32 = { static bad_five : u32 = { { invalid(); - //~^ ERROR: blocks in statics are limited to items and tail expressions - //~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants + //~^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants + //~| ERROR statements in statics are unstable 0 } }; @@ -55,14 +57,15 @@ static bad_five : u32 = { static bad_six : u32 = { { valid(); - //~^ ERROR: blocks in statics are limited to items and tail expressions + //~^ ERROR statements in statics are unstable 0 } }; static mut bad_seven : u32 = { { - 5; //~ ERROR: blocks in statics are limited to items and tail expressions + 5; + //~^ ERROR statements in statics are unstable 0 } }; @@ -70,8 +73,8 @@ static mut bad_seven : u32 = { static mut bad_eight : u32 = { { invalid(); - //~^ ERROR: blocks in statics are limited to items and tail expressions - //~^^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants + //~^ ERROR statements in statics are unstable + //~| ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants 0 } }; @@ -79,7 +82,7 @@ static mut bad_eight : u32 = { static mut bad_nine : u32 = { { valid(); - //~^ ERROR: blocks in statics are limited to items and tail expressions + //~^ ERROR statements in statics are unstable 0 } }; diff --git a/src/test/run-pass/ctfe/const-block-non-item-statement-3.rs b/src/test/run-pass/ctfe/const-block-non-item-statement-3.rs new file mode 100644 index 00000000000..e233107169c --- /dev/null +++ b/src/test/run-pass/ctfe/const-block-non-item-statement-3.rs @@ -0,0 +1,15 @@ +// Copyright 2014 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(const_let)] + +type Array = [u32; { let x = 2; 5 }]; + +pub fn main() {} diff --git a/src/test/run-pass/ctfe/const-block-non-item-statement.rs b/src/test/run-pass/ctfe/const-block-non-item-statement.rs new file mode 100644 index 00000000000..b5a9bfb45a1 --- /dev/null +++ b/src/test/run-pass/ctfe/const-block-non-item-statement.rs @@ -0,0 +1,17 @@ +// Copyright 2014 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(const_let)] + +enum Foo { + Bar = { let x = 1; 3 } +} + +pub fn main() {} diff --git a/src/test/run-pass/ctfe/const-fn-destructuring-arg.rs b/src/test/run-pass/ctfe/const-fn-destructuring-arg.rs new file mode 100644 index 00000000000..8b832976aab --- /dev/null +++ b/src/test/run-pass/ctfe/const-fn-destructuring-arg.rs @@ -0,0 +1,23 @@ +// Copyright 2015 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. + +// test that certain things are disallowed in constant functions + +#![feature(const_fn, const_let)] + +// no destructuring +const fn i(( + a, + b + ): (u32, u32)) -> u32 { + a + b +} + +fn main() {} diff --git a/src/test/run-pass/ctfe/issue-37550.rs b/src/test/run-pass/ctfe/issue-37550.rs new file mode 100644 index 00000000000..27796a5feea --- /dev/null +++ b/src/test/run-pass/ctfe/issue-37550.rs @@ -0,0 +1,18 @@ +// Copyright 2017 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(const_fn, const_let)] + +const fn x() { + let t = true; + let x = || t; +} + +fn main() {} diff --git a/src/test/run-pass/ctfe/locals-in-const-fn.rs b/src/test/run-pass/ctfe/locals-in-const-fn.rs new file mode 100644 index 00000000000..8c153315c25 --- /dev/null +++ b/src/test/run-pass/ctfe/locals-in-const-fn.rs @@ -0,0 +1,45 @@ +// Copyright 2018 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. + +// https://github.com/rust-lang/rust/issues/48821 + +#![feature(const_fn, const_let)] + +const fn foo(i: usize) -> usize { + let x = i; + x +} + +static FOO: usize = foo(42); + +const fn bar(mut i: usize) -> usize { + i += 8; + let x = &i; + *x +} + +static BAR: usize = bar(42); + +const fn boo(mut i: usize) -> usize { + { + let mut x = i; + x += 10; + i = x; + } + i +} + +static BOO: usize = boo(42); + +fn main() { + assert!(FOO == 42); + assert!(BAR == 50); + assert!(BOO == 52); +} diff --git a/src/test/ui/const-eval/const_let.rs b/src/test/ui/const-eval/const_let.rs new file mode 100644 index 00000000000..602d4da24f3 --- /dev/null +++ b/src/test/ui/const-eval/const_let.rs @@ -0,0 +1,30 @@ +// Copyright 2018 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(const_let)] + +fn main() {} + +struct FakeNeedsDrop; + +impl Drop for FakeNeedsDrop { + fn drop(&mut self) {} +} + +// ok +const X: FakeNeedsDrop = { let x = FakeNeedsDrop; x }; + +// error +const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x }; +//~^ ERROR constant contains unimplemented expression type + +// error +const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); }; +//~^ ERROR constant contains unimplemented expression type diff --git a/src/test/ui/const-eval/const_let.stderr b/src/test/ui/const-eval/const_let.stderr new file mode 100644 index 00000000000..86e3482fda6 --- /dev/null +++ b/src/test/ui/const-eval/const_let.stderr @@ -0,0 +1,15 @@ +error[E0019]: constant contains unimplemented expression type + --> $DIR/const_let.rs:25:55 + | +LL | const Y: FakeNeedsDrop = { let mut x = FakeNeedsDrop; x = FakeNeedsDrop; x }; + | ^ + +error[E0019]: constant contains unimplemented expression type + --> $DIR/const_let.rs:29:35 + | +LL | const Z: () = { let mut x = None; x = Some(FakeNeedsDrop); }; + | ^ + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0019`. diff --git a/src/test/ui/const-fn-error.rs b/src/test/ui/const-fn-error.rs index 9e09f66776c..17dc9f94fe1 100644 --- a/src/test/ui/const-fn-error.rs +++ b/src/test/ui/const-fn-error.rs @@ -14,7 +14,8 @@ const X : usize = 2; const fn f(x: usize) -> usize { let mut sum = 0; - //~^ ERROR E0016 + //~^ let bindings in constant functions are unstable + //~| statements in constant functions are unstable for i in 0..x { //~^ ERROR E0015 //~| ERROR E0019 diff --git a/src/test/ui/const-fn-error.stderr b/src/test/ui/const-fn-error.stderr index 767f28ff7b1..29edc2756af 100644 --- a/src/test/ui/const-fn-error.stderr +++ b/src/test/ui/const-fn-error.stderr @@ -1,23 +1,33 @@ -error[E0016]: blocks in constant functions are limited to items and tail expressions +error[E0658]: let bindings in constant functions are unstable (see issue #48821) --> $DIR/const-fn-error.rs:16:19 | LL | let mut sum = 0; | ^ + | + = help: add #![feature(const_let)] to the crate attributes to enable + +error[E0658]: statements in constant functions are unstable (see issue #48821) + --> $DIR/const-fn-error.rs:16:19 + | +LL | let mut sum = 0; + | ^ + | + = help: add #![feature(const_let)] to the crate attributes to enable error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants - --> $DIR/const-fn-error.rs:18:14 + --> $DIR/const-fn-error.rs:19:14 | LL | for i in 0..x { | ^^^^ error[E0019]: constant function contains unimplemented expression type - --> $DIR/const-fn-error.rs:18:14 + --> $DIR/const-fn-error.rs:19:14 | LL | for i in 0..x { | ^^^^ error[E0080]: constant evaluation error - --> $DIR/const-fn-error.rs:18:14 + --> $DIR/const-fn-error.rs:19:14 | LL | for i in 0..x { | ^^^^ calling non-const fn `<I as std::iter::IntoIterator><std::ops::Range<usize>>::into_iter` @@ -26,12 +36,12 @@ LL | let a : [i32; f(X)]; | ---- inside call to `f` | note: for constant expression here - --> $DIR/const-fn-error.rs:29:13 + --> $DIR/const-fn-error.rs:30:13 | LL | let a : [i32; f(X)]; | ^^^^^^^^^^^ -error: aborting due to 4 previous errors +error: aborting due to 5 previous errors -Some errors occurred: E0015, E0016, E0019, E0080. +Some errors occurred: E0015, E0019, E0080, E0658. For more information about an error, try `rustc --explain E0015`. diff --git a/src/test/ui/feature-gate-const_let.rs b/src/test/ui/feature-gate-const_let.rs new file mode 100644 index 00000000000..05d02e62bc8 --- /dev/null +++ b/src/test/ui/feature-gate-const_let.rs @@ -0,0 +1,22 @@ +// Copyright 2018 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. + +// Test use of const let without feature gate. + +#![feature(const_fn)] + +const fn foo() -> usize { + let x = 42; + //~^ ERROR statements in constant functions are unstable + //~| ERROR: let bindings in constant functions are unstable + 42 +} + +fn main() {} diff --git a/src/test/ui/feature-gate-const_let.stderr b/src/test/ui/feature-gate-const_let.stderr new file mode 100644 index 00000000000..6a7f6255678 --- /dev/null +++ b/src/test/ui/feature-gate-const_let.stderr @@ -0,0 +1,19 @@ +error[E0658]: let bindings in constant functions are unstable (see issue #48821) + --> $DIR/feature-gate-const_let.rs:16:13 + | +LL | let x = 42; + | ^^ + | + = help: add #![feature(const_let)] to the crate attributes to enable + +error[E0658]: statements in constant functions are unstable (see issue #48821) + --> $DIR/feature-gate-const_let.rs:16:13 + | +LL | let x = 42; + | ^^ + | + = help: add #![feature(const_let)] to the crate attributes to enable + +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0658`. |
