Skip to content

MIR-borrowck: paths off of statics should not be borrow-checked #45129

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

Closed
pnkfelix opened this issue Oct 9, 2017 · 5 comments
Closed

MIR-borrowck: paths off of statics should not be borrow-checked #45129

pnkfelix opened this issue Oct 9, 2017 · 5 comments
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Milestone

Comments

@pnkfelix
Copy link
Member

pnkfelix commented Oct 9, 2017

Paths off of statics should not be borrow-checked, as they already require unsafe. But currently MIR-borrowck is flagging errors for them, while AST-borrowck lets them through without complaint.

(Spawned off of #44985 (review))

@pnkfelix pnkfelix added WG-compiler-nll A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 9, 2017
@TimNN TimNN added the C-bug Category: This is a bug. label Oct 10, 2017
@arielb1 arielb1 added this to the NLL prototype milestone Nov 15, 2017
@KiChjang
Copy link
Member

I'm taking a look at this.

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 16, 2017

Example test:

struct Foo { x: usize }

static mut SFOO : Foo = Foo{x: 23 }; 

impl Foo {
    fn x(&mut self) -> &mut usize { &mut self.x }
}

fn main() {
    unsafe {
        let x = SFOO.x();
        SFOO.x += 1; // ?
        *x += 1; 
    }
}

This does indeed pass (for better or worse...) in AST-based borrowck.

@nikomatsakis
Copy link
Contributor

I think we should at least warn about this scenario. We could do this by looking at the borrow path during the access checks to see if it was a borrow of a static.

@nikomatsakis
Copy link
Contributor

nikomatsakis commented Nov 16, 2017

After some discussion on gitter, decided that we should leave warning for later consideration, and just match AST borrowck in this case for now. This is not a bug fix to AST-borrowck, no need to deviate right now.

@pnkfelix
Copy link
Member Author

Note for those reading this bug in future: the example test written in above comment is actually UB, because it is creating two simultaneous &mut references that point to the same location.

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

Fixes #45129.
Fixes #45641.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants