Allow let bindings in const fn and constants
This commit is contained in:
parent
6835748725
commit
9872160836
9 changed files with 193 additions and 18 deletions
15
src/test/run-pass/ctfe/const-block-non-item-statement-3.rs
Normal file
15
src/test/run-pass/ctfe/const-block-non-item-statement-3.rs
Normal file
|
|
@ -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() {}
|
||||
17
src/test/run-pass/ctfe/const-block-non-item-statement.rs
Normal file
17
src/test/run-pass/ctfe/const-block-non-item-statement.rs
Normal file
|
|
@ -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() {}
|
||||
23
src/test/run-pass/ctfe/const-fn-destructuring-arg.rs
Normal file
23
src/test/run-pass/ctfe/const-fn-destructuring-arg.rs
Normal file
|
|
@ -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 const fn signatures
|
||||
|
||||
#![feature(const_fn, const_let)]
|
||||
|
||||
// no destructuring
|
||||
const fn i((
|
||||
a,
|
||||
b
|
||||
): (u32, u32)) -> u32 {
|
||||
a + b
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
18
src/test/run-pass/ctfe/issue-37550.rs
Normal file
18
src/test/run-pass/ctfe/issue-37550.rs
Normal file
|
|
@ -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() {}
|
||||
45
src/test/run-pass/ctfe/locals-in-const-fn.rs
Normal file
45
src/test/run-pass/ctfe/locals-in-const-fn.rs
Normal file
|
|
@ -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);
|
||||
}
|
||||
20
src/test/ui/feature-gate-const_let.rs
Normal file
20
src/test/ui/feature-gate-const_let.rs
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
// 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 blocks in constant functions are limited to items and tail expressions
|
||||
42
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
9
src/test/ui/feature-gate-const_let.stderr
Normal file
9
src/test/ui/feature-gate-const_let.stderr
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
error[E0016]: blocks in constant functions are limited to items and tail expressions
|
||||
--> $DIR/feature-gate-const_let.rs:16:13
|
||||
|
|
||||
LL | let x = 42; //~ ERROR blocks in constant functions are limited to items and tail expressions
|
||||
| ^^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
||||
For more information about this error, try `rustc --explain E0016`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue