-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
MIR borrowck: finalize check_access_permissions()
#46041
Conversation
src/librustc_mir/borrow_check.rs
Outdated
err.emit(); | ||
} | ||
}, | ||
Write(WriteKind::MutableBorrow(BorrowKind::Shared)) | |
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.
This should be a delay_span_bug
if this would cause an error - all 3 of these cases indicate an erroneous program.
src/librustc_mir/borrow_check.rs
Outdated
@@ -501,7 +513,9 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> | |||
lvalue_span: (&Lvalue<'tcx>, Span), | |||
kind: ShallowOrDeep, | |||
mode: MutateMode, | |||
is_local_mutation_allowed: LocalMutationIsAllowed, |
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.
This function checks for mutation of locals anyway, so it should always pass LocalMutationIsAllowed::Yes
to avoid double errors.
src/librustc_mir/borrow_check.rs
Outdated
let lvalue = lvalue_span.0; | ||
let ty = lvalue.ty(self.mir, self.tcx).to_ty(self.tcx); | ||
let moves_by_default = | ||
self.fake_infer_ctxt.type_moves_by_default(self.param_env, ty, DUMMY_SP); | ||
if moves_by_default { | ||
// move of lvalue: check if this is move of already borrowed path | ||
self.access_lvalue(context, lvalue_span, (Deep, Write(WriteKind::Move)), flow_state); | ||
self.access_lvalue(context, lvalue_span, (Deep, Write(WriteKind::Move)), LocalMutationIsAllowed::No, flow_state); |
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.
it is 100% OK to move out of an immutable local - this should be a yes.
src/librustc_mir/borrow_check.rs
Outdated
@@ -549,7 +564,7 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx> | |||
_ => unreachable!(), | |||
}; | |||
self.access_lvalue( | |||
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), flow_state); | |||
context, (lvalue, span), (Shallow(Some(af)), Read(ReadKind::Copy)), LocalMutationIsAllowed::No, flow_state); |
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.
dropandreplace
is an assignment of a type with a destructor, so this should be an error only if the value is already initialized.
This is a read, ignore all of this.
Commented. Need to leave for today. |
☔ The latest upstream changes (presumably #45825) made this pull request unmergeable. Please resolve the merge conflicts. |
@arielb1 Sorry I won't be available for the rest of the week and I didn't have the time to finalize my PR. I have some changes corresponding to your comments but not yet pushed. Some tests aren't passing. If this is blocking feel free to take the PR and finalize it yourself. Otherwise I'll finish it sunday or monday evening. |
src/librustc_mir/borrow_check.rs
Outdated
@@ -444,6 +446,12 @@ enum WriteKind { | |||
Move, | |||
} | |||
|
|||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | |||
enum LocalMutationIsAllowed { |
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.
Nit: can you add a comment explaining what this flag is about?
d3135c6
to
d4cc8b0
Compare
check_access_permissions()
check_access_permissions()
@nikomatsakis @arielb1 There is an ICE on the following test: https://github.com/rust-lang/rust/blob/master/src/test/run-pass/borrowck/borrowck-unsafe-static-mutable-borrows.rs struct Foo { x: [usize; 2] }
static mut SFOO: Foo = Foo { x: [23, 32] };
impl Foo {
fn x(&mut self) -> &mut usize { &mut self.x[0] }
}
fn main() {
unsafe {
let sfoo: *mut Foo = &mut SFOO;
let x = (*sfoo).x();
(*sfoo).x[1] += 1;
*x += 1;
}
} The How should I fix this? Is the |
|
src/librustc_mir/borrow_check.rs
Outdated
Write(WriteKind::Move) | | ||
Write(WriteKind::StorageDeadOrDrop) | | ||
Write(WriteKind::MutableBorrow(BorrowKind::Shared)) => { | ||
let item_msg = match self.describe_lvalue(lvalue) { |
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 think you're missing a check for whether the access is actually permitted, similarly to the previous lines. i.e. an
if let Err(lvalue_err) = self.is_mutable(lvalue, is_local_mutation_allowed) {
- There's no reason to use pretty-printing in
delay_span_bug
- it can hide information and it just makes the code uglier. Just format the lvalue directly (format!("Accessing {:?} with the kind
{:?}shouldn't be possible", lvalue, kind)
).
src/librustc_mir/borrow_check.rs
Outdated
Some(name) => format!("`{}`", name), | ||
None => "item".to_owned() | ||
}; | ||
span_bug!(span, "&unique borrow for {} should not fail", item_msg); |
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.
There's no reason to use pretty-printing in delay_span_bug
- it can hide information and it just makes the code uglier. Just format the lvalue directly (format!("Accessing {:?} with the kind
{:?} shouldn't be possible", lvalue, kind)
).
Oops wrong |
65b5a73
to
4bd53ae
Compare
@arielb1 I added |
☔ The latest upstream changes (presumably #46106) made this pull request unmergeable. Please resolve the merge conflicts. |
@bors r+ |
📌 Commit abd07b7 has been approved by |
☔ The latest upstream changes (presumably #46022) made this pull request unmergeable. Please resolve the merge conflicts. |
☔ The latest upstream changes (presumably #46312) made this pull request unmergeable. Please resolve the merge conflicts. |
@bors r+ |
📌 Commit 1cd9d74 has been approved by |
☀️ Test successful - status-appveyor, status-travis |
Fix #44837 (hopefully for good)
r? @arielb1