Rollup merge of #57175 - oli-obk:const_let_stabilization, r=nikomatsakis
Stabilize `let` bindings and destructuring in constants and const fn
r? @Centril
This PR stabilizes the following features in constants and `const` functions:
* irrefutable destructuring patterns (e.g. `const fn foo((x, y): (u8, u8)) { ... }`)
* `let` bindings (e.g. `let x = 1;`)
* mutable `let` bindings (e.g. `let mut x = 1;`)
* assignment (e.g. `x = y`) and assignment operator (e.g. `x += y`) expressions, even where the assignment target is a projection (e.g. a struct field or index operation like `x[3] = 42`)
* expression statements (e.g. `3;`)
This PR does explicitly *not* stabilize:
* mutable references (i.e. `&mut T`)
* dereferencing mutable references
* refutable patterns (e.g. `Some(x)`)
* operations on `UnsafeCell` types (as that would need raw pointers and mutable references and such, not because it is explicitly forbidden. We can't explicitly forbid it as such values are OK as long as they aren't mutated.)
* We are not stabilizing `let` bindings in constants that use `&&` and `||` short circuiting operations. These are treated as `&` and `|` inside `const` and `static` items right now. If we stopped treating them as `&` and `|` after stabilizing `let` bindings, we'd break code like `let mut x = false; false && { x = true; false };`. So to use `let` bindings in constants you need to change `&&` and `||` to `&` and `|` respectively.
This commit is contained in:
commit
bd8f464877
94 changed files with 416 additions and 977 deletions
|
|
@ -1,68 +1,14 @@
|
|||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:5:17
|
||||
|
|
||||
LL | let p = 3;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:5:17
|
||||
|
|
||||
LL | let p = 3;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:8:9
|
||||
|
|
||||
LL | &p //~ ERROR `p` does not live long enough
|
||||
| ^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:2:5
|
||||
|
|
||||
LL | / const z: &'static isize = {
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | //~| ERROR statements in constants are unstable
|
||||
LL | | let p = 3;
|
||||
... |
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | };
|
||||
| |______^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:2:5
|
||||
|
|
||||
LL | / const z: &'static isize = {
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | //~| ERROR statements in constants are unstable
|
||||
LL | | let p = 3;
|
||||
... |
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | };
|
||||
| |______^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0597]: `p` does not live long enough
|
||||
--> $DIR/issue-18118.rs:8:9
|
||||
--> $DIR/issue-18118.rs:4:9
|
||||
|
|
||||
LL | &p //~ ERROR `p` does not live long enough
|
||||
| ^^
|
||||
| |
|
||||
| borrowed value does not live long enough
|
||||
| using this value as a constant requires that `p` is borrowed for `'static`
|
||||
LL | //~^ ERROR let bindings in constants are unstable
|
||||
LL | };
|
||||
| - `p` dropped here while still borrowed
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: aborting due to previous error
|
||||
|
||||
Some errors occurred: E0597, E0658.
|
||||
For more information about an error, try `rustc --explain E0597`.
|
||||
For more information about this error, try `rustc --explain E0597`.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,6 @@
|
|||
pub fn main() {
|
||||
const z: &'static isize = {
|
||||
//~^ ERROR let bindings in constants are unstable
|
||||
//~| ERROR statements in constants are unstable
|
||||
let p = 3;
|
||||
//~^ 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
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,67 +1,13 @@
|
|||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:5:17
|
||||
|
|
||||
LL | let p = 3;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:5:17
|
||||
|
|
||||
LL | let p = 3;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:8:9
|
||||
|
|
||||
LL | &p //~ ERROR `p` does not live long enough
|
||||
| ^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: let bindings in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:2:5
|
||||
|
|
||||
LL | / const z: &'static isize = {
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | //~| ERROR statements in constants are unstable
|
||||
LL | | let p = 3;
|
||||
... |
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | };
|
||||
| |______^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-18118.rs:2:5
|
||||
|
|
||||
LL | / const z: &'static isize = {
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | //~| ERROR statements in constants are unstable
|
||||
LL | | let p = 3;
|
||||
... |
|
||||
LL | | //~^ ERROR let bindings in constants are unstable
|
||||
LL | | };
|
||||
| |______^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0597]: `p` does not live long enough
|
||||
--> $DIR/issue-18118.rs:8:10
|
||||
--> $DIR/issue-18118.rs:4:10
|
||||
|
|
||||
LL | &p //~ ERROR `p` does not live long enough
|
||||
| ^ borrowed value does not live long enough
|
||||
LL | //~^ ERROR let bindings in constants are unstable
|
||||
LL | };
|
||||
| - borrowed value only lives until here
|
||||
|
|
||||
= note: borrowed value must be valid for the static lifetime...
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
error: aborting due to previous error
|
||||
|
||||
Some errors occurred: E0597, E0658.
|
||||
For more information about an error, try `rustc --explain E0597`.
|
||||
For more information about this error, try `rustc --explain E0597`.
|
||||
|
|
|
|||
|
|
@ -1,11 +1,8 @@
|
|||
// ignore-tidy-linelength
|
||||
|
||||
#![feature(const_fn)]
|
||||
|
||||
const bad : u32 = {
|
||||
{
|
||||
5;
|
||||
//~^ ERROR statements in constants are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -13,8 +10,7 @@ const bad : u32 = {
|
|||
const bad_two : u32 = {
|
||||
{
|
||||
invalid();
|
||||
//~^ ERROR statements in constants are unstable
|
||||
//~^^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
|
||||
//~^ ERROR: calls in constants are limited to constant functions, tuple structs and tuple variants
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -22,7 +18,6 @@ const bad_two : u32 = {
|
|||
const bad_three : u32 = {
|
||||
{
|
||||
valid();
|
||||
//~^ ERROR statements in constants are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -30,7 +25,6 @@ const bad_three : u32 = {
|
|||
static bad_four : u32 = {
|
||||
{
|
||||
5;
|
||||
//~^ ERROR statements in statics are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -39,7 +33,6 @@ static bad_five : u32 = {
|
|||
{
|
||||
invalid();
|
||||
//~^ ERROR: calls in statics are limited to constant functions, tuple structs and tuple variants
|
||||
//~| ERROR statements in statics are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -47,7 +40,6 @@ static bad_five : u32 = {
|
|||
static bad_six : u32 = {
|
||||
{
|
||||
valid();
|
||||
//~^ ERROR statements in statics are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -55,7 +47,6 @@ static bad_six : u32 = {
|
|||
static mut bad_seven : u32 = {
|
||||
{
|
||||
5;
|
||||
//~^ ERROR statements in statics are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -63,8 +54,7 @@ static mut bad_seven : u32 = {
|
|||
static mut bad_eight : u32 = {
|
||||
{
|
||||
invalid();
|
||||
//~^ ERROR statements in statics are unstable
|
||||
//~| 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
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
@ -72,7 +62,6 @@ static mut bad_eight : u32 = {
|
|||
static mut bad_nine : u32 = {
|
||||
{
|
||||
valid();
|
||||
//~^ ERROR statements in statics are unstable
|
||||
0
|
||||
}
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,94 +1,21 @@
|
|||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:7:9
|
||||
|
|
||||
LL | 5;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0015]: calls in constants are limited to constant functions, tuple structs and tuple variants
|
||||
--> $DIR/issue-32829-2.rs:15:9
|
||||
--> $DIR/issue-32829-2.rs:12:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:15:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in constants are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:24:9
|
||||
|
|
||||
LL | valid();
|
||||
| ^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:32:9
|
||||
|
|
||||
LL | 5;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
|
||||
--> $DIR/issue-32829-2.rs:40:9
|
||||
--> $DIR/issue-32829-2.rs:34:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:40:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:49:9
|
||||
|
|
||||
LL | valid();
|
||||
| ^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:57:9
|
||||
|
|
||||
LL | 5;
|
||||
| ^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error[E0015]: calls in statics are limited to constant functions, tuple structs and tuple variants
|
||||
--> $DIR/issue-32829-2.rs:65:9
|
||||
--> $DIR/issue-32829-2.rs:56:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:65:9
|
||||
|
|
||||
LL | invalid();
|
||||
| ^^^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
error[E0658]: statements in statics are unstable (see issue #48821)
|
||||
--> $DIR/issue-32829-2.rs:74:9
|
||||
|
|
||||
LL | valid();
|
||||
| ^^^^^^^
|
||||
|
|
||||
= help: add #![feature(const_let)] to the crate attributes to enable
|
||||
|
||||
error: aborting due to 12 previous errors
|
||||
|
||||
Some errors occurred: E0015, E0658.
|
||||
For more information about an error, try `rustc --explain E0015`.
|
||||
For more information about this error, try `rustc --explain E0015`.
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
const fn x() {
|
||||
let t = true; //~ ERROR local variables in const fn
|
||||
let x = || t;
|
||||
let t = true;
|
||||
let x = || t; //~ ERROR function pointers in const fn are unstable
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
error: local variables in const fn are unstable
|
||||
--> $DIR/issue-37550.rs:2:9
|
||||
error: function pointers in const fn are unstable
|
||||
--> $DIR/issue-37550.rs:3:9
|
||||
|
|
||||
LL | let t = true; //~ ERROR local variables in const fn
|
||||
LL | let x = || t; //~ ERROR function pointers in const fn are unstable
|
||||
| ^
|
||||
|
||||
error: aborting due to previous error
|
||||
|
|
|
|||
|
|
@ -6,5 +6,6 @@ use std::cell::RefCell;
|
|||
static boxed: Box<RefCell<isize>> = box RefCell::new(0);
|
||||
//~^ ERROR allocations are not allowed in statics
|
||||
//~| ERROR `std::cell::RefCell<isize>` cannot be shared between threads safely [E0277]
|
||||
//~| ERROR static contains unimplemented expression type
|
||||
|
||||
fn main() { }
|
||||
|
|
|
|||
|
|
@ -4,6 +4,12 @@ error[E0010]: allocations are not allowed in statics
|
|||
LL | static boxed: Box<RefCell<isize>> = box RefCell::new(0);
|
||||
| ^^^^^^^^^^^^^^^^^^^ allocation not allowed in statics
|
||||
|
||||
error[E0019]: static contains unimplemented expression type
|
||||
--> $DIR/issue-7364.rs:6:41
|
||||
|
|
||||
LL | static boxed: Box<RefCell<isize>> = box RefCell::new(0);
|
||||
| ^^^^^^^^^^^^^^^
|
||||
|
||||
error[E0277]: `std::cell::RefCell<isize>` cannot be shared between threads safely
|
||||
--> $DIR/issue-7364.rs:6:1
|
||||
|
|
||||
|
|
@ -15,7 +21,7 @@ LL | static boxed: Box<RefCell<isize>> = box RefCell::new(0);
|
|||
= note: required because it appears within the type `std::boxed::Box<std::cell::RefCell<isize>>`
|
||||
= note: shared static variables must have a type that implements `Sync`
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
error: aborting due to 3 previous errors
|
||||
|
||||
Some errors occurred: E0010, E0277.
|
||||
Some errors occurred: E0010, E0019, E0277.
|
||||
For more information about an error, try `rustc --explain E0010`.
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue