-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Permit mutable references in all const contexts #78578
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
7 commits
Select commit
Hold shift + click to select a range
d118021
Permit mutable references in all const contexts
oli-obk b217fab
Rename tests to what their code actually does
oli-obk 3cd0b46
Fix a comment that only made sense in the context of a dataflow based…
oli-obk 00e62fa
Adjust wording of a diagnostic
oli-obk 14f39aa
Do not allow arbitrary mutable references in `static mut`, just keep …
oli-obk cd09871
Cover more cases in the test suite
oli-obk 819b008
Put dynamic check tests into their own file
oli-obk 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
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 mutable references are not allowed in statics | ||
//~^ ERROR mutable references are not allowed | ||
|
||
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
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: 0 additions & 15 deletions
15
src/test/ui/consts/const-mut-refs/const_mut_address_of.stderr
This file was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,57 @@ | ||
#![feature(const_mut_refs)] | ||
#![feature(const_fn)] | ||
#![feature(raw_ref_op)] | ||
#![feature(const_raw_ptr_deref)] | ||
|
||
const NULL: *mut i32 = std::ptr::null_mut(); | ||
const A: *const i32 = &4; | ||
|
||
// It could be made sound to allow it to compile, | ||
// but we do not want to allow this to compile, | ||
// as that would be an enormous footgun in oli-obk's opinion. | ||
const B: *mut i32 = &mut 4; //~ ERROR mutable references are not allowed | ||
|
||
// Ok, no actual mutable allocation exists | ||
const B2: Option<&mut i32> = None; | ||
|
||
// Not ok, can't prove that no mutable allocation ends up in final value | ||
const B3: Option<&mut i32> = Some(&mut 42); //~ ERROR temporary value dropped while borrowed | ||
|
||
const fn helper(x: &mut i32) -> Option<&mut i32> { Some(x) } | ||
const B4: Option<&mut i32> = helper(&mut 42); //~ ERROR temporary value dropped while borrowed | ||
|
||
// Ok, because no references to mutable data exist here, since the `{}` moves | ||
// its value and then takes a reference to that. | ||
const C: *const i32 = &{ | ||
let mut x = 42; | ||
x += 3; | ||
x | ||
}; | ||
|
||
use std::cell::UnsafeCell; | ||
struct NotAMutex<T>(UnsafeCell<T>); | ||
|
||
unsafe impl<T> Sync for NotAMutex<T> {} | ||
|
||
const FOO: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42)); | ||
//~^ ERROR temporary value dropped while borrowed | ||
|
||
static FOO2: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42)); | ||
//~^ ERROR temporary value dropped while borrowed | ||
|
||
static mut FOO3: NotAMutex<&mut i32> = NotAMutex(UnsafeCell::new(&mut 42)); | ||
//~^ ERROR temporary value dropped while borrowed | ||
|
||
// `BAR` works, because `&42` promotes immediately instead of relying on | ||
// the enclosing scope rule. | ||
const BAR: NotAMutex<&i32> = NotAMutex(UnsafeCell::new(&42)); | ||
|
||
fn main() { | ||
println!("{}", unsafe { *A }); | ||
unsafe { *B = 4 } // Bad news | ||
|
||
unsafe { | ||
**FOO.0.get() = 99; | ||
assert_eq!(**FOO.0.get(), 99); | ||
} | ||
} |
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.