Skip to content

Ignore borrowck for static lvalues and allow assignment to static muts #46032

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 3 commits into from
Nov 18, 2017

Conversation

KiChjang
Copy link
Member

Fixes #45129.
Fixes #45641.

@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch 3 times, most recently from 54d3a29 to 64b7ac5 Compare November 16, 2017 11:02
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Just a few nits.

context, common_prefix, lvalue_span, bk,
&borrow, end_issued_loan_span)
// Ignore all borrows rooted in statics
if let Lvalue::Local(_) = *self.prefixes(lvalue_span.0, PrefixSet::All).last().unwrap() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this if let makes me nervous -- if we add more lvalues in the future, it might silently squash them. I'd rather see an exhaustive match.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alternatively, something like:

if let Lvalue::Static(_) = ... {
    // Skip checks on statics. See #123.
} else {
    ...
}

&borrow, end_issued_loan_span)
// Ignore all borrows rooted in statics
if let Lvalue::Local(_) = *self.prefixes(lvalue_span.0, PrefixSet::All).last().unwrap() {
self.each_borrow_involving_path(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also, I think this should be factored into a helper function personally

@nikomatsakis
Copy link
Contributor

r? @nikomatsakis

@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch 2 times, most recently from e4190a5 to 0c62bfd Compare November 16, 2017 11:26
@KiChjang
Copy link
Member Author

Done. r? @nikomatsakis

// revisions: ast mir
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir

// Test file taken from issue 45129 (https://github.com/rust-lang/rust/issues/45129)
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is UB. Please don't have it as a run-pass test, because verified compilation will correctly complain.

to make it non-UB, e.g. use an array and access different array indices:

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 x = SFOO.x();
        SFOO.x[1] += 1;
        *x += 1; 
    }
}

// Check permissions
self.check_access_permissions(lvalue_span, rw);

match *self.prefixes(lvalue_span.0, PrefixSet::All).last().unwrap() {
Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't borrows involving statics be handled in the same way as borrows involving raw pointers? I could open another issue for borrows through raw pointers, but they should be handled similarly.

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() {
    // this compiles with AST borrowck, should also compile with MIR borrowck
    unsafe {
        let sfoo: *mut Foo = &mut SFOO;
        let x = (*sfoo).x();
        (*sfoo).x[1] += 1;
        *x += 1; 
    }
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree they should be handled the same. Do you think that means however that we should unify the codepaths that make those two cases behave the same? If so, how do you think we should do it? (I think that sounds like a nice idea)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(In particular, if we decided to offer a lint at some point for obvious UB, I'd want it to apply to both.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess if we just factor out a helper function unsafe_access_path(&Lvalue) it can test for either a deref of a raw pointer or the use of a static mut.

Copy link
Contributor

@arielb1 arielb1 Nov 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikomatsakis

One I think good way of doing it is just not registering borrows that go through an unsafe access path.

Note that for unsafe pointers this is different from checking the lvalue, but it is what AST borrowck does, e.g. in:

fn main() {
    let mut x = 1 as *const ();
    let y = unsafe { &*x }; // borrow through an unsafe pointer
    let _ = &mut x; // then mutate everything - should be OK
    println!("{:p}", y);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was avoiding recommending that because I wanted to consider adding a lint later on. But we could do it that way. Either way, we basically need the helper function I talked about, I imagine. It's just a question of whether we try to avoid registering the borrows in the first place, or try to ignore them when they are registered. =)

@arielb1
Copy link
Contributor

arielb1 commented Nov 16, 2017

btw,

[00:02:41] tidy error: /checkout/src/test/run-pass/borrowck/borrowck-unsafe-static-mutable-borrows.rs:18: trailing whitespace

[00:02:41] tidy error: /checkout/src/test/run-pass/borrowck/borrowck-unsafe-static-mutable-borrows.rs:28: trailing whitespace

@kennytm kennytm added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label Nov 16, 2017
@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch from 0c62bfd to 086b92c Compare November 16, 2017 23:32
@KiChjang
Copy link
Member Author

Alright, I've made changes to the test file, and am now also not registering borrows that are from an unsafe lvalue.

Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! I just think one part of it is no longer needed.


match *lvalue {
Local(_) => false,
Static(_) => true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only static mut is an unsafe lvalue, not any static at all.

context, common_prefix, lvalue_span, bk,
&borrow, end_issued_loan_span)
}
fn check_borrows_readability_or_writability<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are suppressing the borrows, we shouldn't need any changes to this function.

let sfoo: *mut Foo = &mut SFOO;
let x = (*sfoo).x();
(*sfoo).x[1] += 1;
*x += 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! =)

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch 2 times, most recently from f22266b to 374047d Compare November 17, 2017 20:04
Copy link
Contributor

@nikomatsakis nikomatsakis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookin' good. One nit.

@@ -164,6 +203,34 @@ impl<'a, 'gcx, 'tcx> Borrows<'a, 'gcx, 'tcx> {
}
}
}

fn is_unsafe_lvalue(&self, lvalue: &mir::Lvalue<'tcx>) -> bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we have this same function twice? Copy-and-paste error?

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch from 374047d to c9d1db7 Compare November 17, 2017 22:16

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
@KiChjang KiChjang force-pushed the ignore-borrowck-statics branch from e11a29a to f8ba371 Compare November 17, 2017 22:22
@nikomatsakis
Copy link
Contributor

@bors r+

@bors
Copy link
Collaborator

bors commented Nov 18, 2017

📌 Commit f8ba371 has been approved by nikomatsakis

@kennytm kennytm added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Nov 18, 2017
@bors
Copy link
Collaborator

bors commented Nov 18, 2017

⌛ Testing commit f8ba371 with merge b1409af...

bors added a commit that referenced this pull request Nov 18, 2017
Ignore borrowck for static lvalues and allow assignment to static muts

Fixes #45129.
Fixes #45641.
@bors
Copy link
Collaborator

bors commented Nov 18, 2017

☀️ Test successful - status-appveyor, status-travis
Approved by: nikomatsakis
Pushing b1409af to master...

@bors bors merged commit f8ba371 into rust-lang:master Nov 18, 2017
@KiChjang KiChjang deleted the ignore-borrowck-statics branch November 18, 2017 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants