-
Notifications
You must be signed in to change notification settings - Fork 13.4k
forbid mutable references in all constant contexts except for const-fns #72934
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Mutable references (`&mut`) can only be used in constant functions, not statics | ||
or constants. This limitation exists to prevent the creation of constants that | ||
have a mutable reference in their final value. If you had a constant of `&mut | ||
i32` type, you could modify the value through that reference, making the | ||
constant essentially mutable. While there could be a more fine-grained scheme | ||
in the future that allows mutable references if they are not "leaked" to the | ||
final value, a more conservative approach was chosen for now. `const fn` do not | ||
have this problem, as the borrow checker will prevent the `const fn` from | ||
returning new mutable references. | ||
|
||
Erroneous code example: | ||
|
||
```compile_fail,E0764 | ||
#![feature(const_fn)] | ||
#![feature(const_mut_refs)] | ||
|
||
fn main() { | ||
const OH_NO: &'static mut usize = &mut 1; // error! | ||
} | ||
``` | ||
|
||
Remember: you cannot use a function call inside a constant or static. However, | ||
you can totally use it in constant functions: | ||
|
||
``` | ||
#![feature(const_fn)] | ||
#![feature(const_mut_refs)] | ||
|
||
const fn foo(x: usize) -> usize { | ||
let mut y = 1; | ||
let z = &mut y; | ||
*z += x; | ||
y | ||
} | ||
|
||
fn main() { | ||
const FOO: usize = foo(10); // ok! | ||
} | ||
``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
// Checks that immutable static items can't have mutable slices | ||
|
||
static TEST: &'static mut [isize] = &mut []; | ||
//~^ ERROR references in statics may only refer to immutable values | ||
//~^ ERROR mutable references are not allowed in statics | ||
|
||
pub fn main() { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,9 @@ | ||
error[E0658]: references in statics may only refer to immutable values | ||
error[E0764]: mutable references are not allowed in statics | ||
--> $DIR/check-static-immutable-mut-slices.rs:3:37 | ||
| | ||
LL | static TEST: &'static mut [isize] = &mut []; | ||
| ^^^^^^^ statics require immutable values | ||
| | ||
= note: see issue #57349 <https://github.com/rust-lang/rust/issues/57349> for more information | ||
= help: add `#![feature(const_mut_refs)]` to the crate attributes to enable | ||
| ^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0658`. | ||
For more information about this error, try `rustc --explain E0764`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
error[E0764]: mutable references are not allowed in constants | ||
--> $DIR/const_mut_address_of.rs:24:5 | ||
| | ||
LL | foo().bar(); | ||
| ^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error[E0764]: mutable references are not allowed in constants | ||
--> $DIR/const_mut_address_of.rs:26:9 | ||
| | ||
LL | baz(&mut foo()); | ||
| ^^^^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0764`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
error[E0764]: mutable references are not allowed in constants | ||
--> $DIR/const_mut_refs.rs:31:17 | ||
| | ||
LL | let _: [(); foo().bar()] = [(); 1]; | ||
| ^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error[E0764]: mutable references are not allowed in constants | ||
--> $DIR/const_mut_refs.rs:33:21 | ||
| | ||
LL | let _: [(); baz(&mut foo())] = [(); 2]; | ||
| ^^^^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error[E0764]: mutable references are not allowed in constants | ||
--> $DIR/const_mut_refs.rs:35:22 | ||
| | ||
LL | let _: [(); bazz(&mut foo())] = [(); 3]; | ||
| ^^^^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0764`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,9 @@ | ||
// run-pass | ||
// We are keeping this test in case we decide to allow mutable references in statics again | ||
#![feature(const_mut_refs)] | ||
#![allow(const_err)] | ||
|
||
static OH_YES: &mut i32 = &mut 42; | ||
|
||
static OH_NO: &mut i32 = &mut 42; | ||
//~^ ERROR mutable references are not allowed in statics | ||
fn main() { | ||
// Make sure `OH_YES` can be read. | ||
assert_eq!(*OH_YES, 42); | ||
assert_eq!(*OH_NO, 42); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
error[E0764]: mutable references are not allowed in statics | ||
--> $DIR/read_from_static_mut_ref.rs:5:26 | ||
| | ||
LL | static OH_NO: &mut i32 = &mut 42; | ||
| ^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0764`. |
8 changes: 4 additions & 4 deletions
8
src/test/ui/consts/static_mut_containing_mut_ref2.mut_refs.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
error[E0080]: could not evaluate static initializer | ||
--> $DIR/static_mut_containing_mut_ref2.rs:7:45 | ||
error[E0764]: mutable references are not allowed in statics | ||
--> $DIR/static_mut_containing_mut_ref2.rs:7:46 | ||
| | ||
LL | pub static mut STDERR_BUFFER: () = unsafe { *(&mut STDERR_BUFFER_SPACE) = 42; }; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ modifying a static's initial value from another static's initializer | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ `&mut` is only allowed in `const fn` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0080`. | ||
For more information about this error, try `rustc --explain E0764`. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.