forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#66654 - ecstatic-morse:check-consts-ref, r=…
…eddyb,matthewjasper Handle const-checks for `&mut` outside of `HasMutInterior` Addresses [this comment](rust-lang#64470 (comment)). Const-checking relied on `HasMutInterior` to forbid `&mut` in a const context. This was strange because all we needed to do was look for an `Rvalue::Ref` with a certain `BorrowKind`, whereas the `Qualif` traits are specifically meant to get the qualifs for a *value*. This PR removes that logic from `HasMutInterior` and moves it into `check_consts::Validator`. As a result, we can now properly handle qualifications for `static`s, which had to be ignored previously since you can e.g. borrow a static `Cell` from another `static`. We also remove the `derived_from_illegal_borrow` logic, since it is no longer necessary; we give good errors for subsequent reborrows/borrows of illegal borrows.
- Loading branch information
Showing
9 changed files
with
145 additions
and
181 deletions.
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
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,11 +1,24 @@ | ||
const _X: i32 = { | ||
// Ensure that we point the user to the erroneous borrow but not to any subsequent borrows of that | ||
// initial one. | ||
|
||
const _: i32 = { | ||
let mut a = 5; | ||
let p = &mut a; //~ ERROR references in constants may only refer to immutable values | ||
let p = &mut a; //~ ERROR references in constants may only refer to immutable values | ||
|
||
let reborrow = {p}; //~ ERROR references in constants may only refer to immutable values | ||
let reborrow = {p}; | ||
let pp = &reborrow; | ||
let ppp = &pp; | ||
***ppp | ||
}; | ||
|
||
const _: std::cell::Cell<i32> = { | ||
let mut a = std::cell::Cell::new(5); | ||
let p = &a; //~ ERROR cannot borrow a constant which may contain interior mutability | ||
|
||
let reborrow = {p}; | ||
let pp = &reborrow; | ||
let ppp = &pp; | ||
a | ||
}; | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
error[E0017]: references in constants may only refer to immutable values | ||
--> $DIR/const-multi-ref.rs:3:13 | ||
--> $DIR/const-multi-ref.rs:6:13 | ||
| | ||
LL | let p = &mut a; | ||
| ^^^^^^ constants require immutable values | ||
|
||
error[E0017]: references in constants may only refer to immutable values | ||
--> $DIR/const-multi-ref.rs:5:21 | ||
error[E0492]: cannot borrow a constant which may contain interior mutability, create a static instead | ||
--> $DIR/const-multi-ref.rs:16:13 | ||
| | ||
LL | let reborrow = {p}; | ||
| ^ constants require immutable values | ||
LL | let p = &a; | ||
| ^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0017`. | ||
Some errors have detailed explanations: E0017, E0492. | ||
For more information about an error, try `rustc --explain E0017`. |
Oops, something went wrong.