Allow let bindings in const fn and constants

This commit is contained in:
Oliver Schneider 2018-03-27 21:16:37 +02:00
parent 6835748725
commit 9872160836
No known key found for this signature in database
GPG key ID: 1D5CB4FC597C3004
9 changed files with 193 additions and 18 deletions

View 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() {}

View 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() {}

View 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() {}

View 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() {}

View 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);
}

View 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() {}

View 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`.