Auto merge of #77124 - spastorino:const-exprs-rfc-2920, r=oli-obk

Implement const expressions and patterns (RFC 2920)

cc `@ecstatic-morse` `@lcnr` `@oli-obk` `@petrochenkov`
This commit is contained in:
bors 2020-10-17 14:44:51 +00:00
commit 6af9846fcc
48 changed files with 301 additions and 36 deletions

View file

@ -0,0 +1,6 @@
fn main() {
let _ = const {
//~^ ERROR inline-const is experimental [E0658]
true
};
}

View file

@ -0,0 +1,12 @@
error[E0658]: inline-const is experimental
--> $DIR/feature-gate-inline_const.rs:2:13
|
LL | let _ = const {
| ^^^^^
|
= note: see issue #76001 <https://github.com/rust-lang/rust/issues/76001> for more information
= help: add `#![feature(inline_const)]` to the crate attributes to enable
error: aborting due to previous error
For more information about this error, try `rustc --explain E0658`.

View file

@ -0,0 +1,10 @@
// build-pass
#![allow(incomplete_features)]
#![feature(inline_const)]
use std::cell::Cell;
fn main() {
let _x = [const { Cell::new(0) }; 20];
}

View file

@ -0,0 +1,14 @@
// run-pass
#![allow(incomplete_features)]
#![feature(inline_const)]
fn foo() -> i32 {
const {
let x = 5 + 10;
x / 3
}
}
fn main() {
assert_eq!(5, foo());
}

View file

@ -0,0 +1,15 @@
// run-pass
#![allow(incomplete_features)]
#![feature(inline_const)]
const fn bar() -> i32 {
const {
2 + 3
}
}
fn main() {
let x: &'static i32 = &const{bar()};
assert_eq!(&5, x);
}

View file

@ -0,0 +1,21 @@
// run-pass
#![allow(incomplete_features)]
#![feature(inline_const)]
const MMIO_BIT1: u8 = 4;
const MMIO_BIT2: u8 = 5;
fn main() {
let s = match read_mmio() {
0 => "FOO",
const { 1 << MMIO_BIT1 } => "BAR",
const { 1 << MMIO_BIT2 } => "BAZ",
_ => unreachable!(),
};
assert_eq!("BAZ", s);
}
fn read_mmio() -> i32 {
1 << 5
}

View file

@ -13,4 +13,4 @@
fn f() { |[](* }
//~^ ERROR expected one of `,` or `:`, found `(`
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
//~| ERROR expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`

View file

@ -4,7 +4,7 @@ error: expected one of `,` or `:`, found `(`
LL | fn f() { |[](* }
| ^ expected one of `,` or `:`
error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `mut`, `ref`, `|`, identifier, or path, found `*`
error: expected one of `&`, `(`, `)`, `-`, `...`, `..=`, `..`, `[`, `_`, `box`, `const`, `mut`, `ref`, `|`, identifier, or path, found `*`
--> $DIR/issue-66357-unexpected-unreachable.rs:14:14
|
LL | fn f() { |[](* }