-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Permit mutable references in all const contexts #78578
Merged
Merged
Changes from 6 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 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 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 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 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 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 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 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 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 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 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,68 @@ | ||
#![feature(const_mut_refs)] | ||
#![feature(const_fn)] | ||
#![feature(const_transmute)] | ||
#![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() -> Option<&'static mut i32> { unsafe { | ||
// Undefined behaviour, who doesn't love tests like this. | ||
// This code never gets executed, because the static checks fail before that. | ||
Some(&mut *(42 as *mut i32)) | ||
} } | ||
// Check that we do not look into function bodies. | ||
// We treat all functions as not returning a mutable reference, because there is no way to | ||
// do that without causing the borrow checker to complain (see the B5/helper2 test below). | ||
const B4: Option<&mut i32> = helper(); | ||
|
||
const fn helper2(x: &mut i32) -> Option<&mut i32> { Some(x) } | ||
const B5: Option<&mut i32> = helper2(&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 | ||
}; | ||
|
||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not too happy with
B4
... but also not sure what else to do here. We could start running checks on the return type of functions, but I'm not sure how to do that properly, especially once generics are involved.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is there no
//~ ERROR
here then?What if you do
Some(&mut *(&mut 42 as *mut i32))
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the static checks on other constants fail. This code will work and then error in validation if we were getting this far, but since everything else erros first, and B4 is not used, it isn't evaluated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would just give us a dangling pointer, but still not get to validation. I could put this test in a separate file, then we would get to validation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, the static checks in other consts fail.
Looks like this test needs its own file then.
Sure, I meant to test that in addition to B4, not instead of.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done