Skip to content

Commit 3be597a

Browse files
committed
Allow assignment to static muts
1 parent 481b42b commit 3be597a

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

src/librustc_mir/borrow_check.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -640,10 +640,12 @@ impl<'c, 'b, 'a: 'b+'c, 'gcx, 'tcx: 'a> MirBorrowckCtxt<'c, 'b, 'a, 'gcx, 'tcx>
640640
Mutability::Mut => return,
641641
}
642642
}
643-
Lvalue::Static(_) => {
643+
Lvalue::Static(ref static_) => {
644644
// mutation of non-mut static is always illegal,
645645
// independent of dataflow.
646-
self.report_assignment_to_static(context, (lvalue, span));
646+
if !self.tcx.is_static_mut(static_.def_id) {
647+
self.report_assignment_to_static(context, (lvalue, span));
648+
}
647649
return;
648650
}
649651
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// revisions: ast mir
12+
//[mir]compile-flags: -Z emit-end-regions -Z borrowck-mir
13+
14+
// Test file taken from issue 45129 (https://github.com/rust-lang/rust/issues/45129)
15+
16+
struct Foo { x: [usize; 2] }
17+
18+
static mut SFOO: Foo = Foo { x: [23, 32] };
19+
20+
impl Foo {
21+
fn x(&mut self) -> &mut usize { &mut self.x[0] }
22+
}
23+
24+
fn main() {
25+
unsafe {
26+
let sfoo: *mut Foo = &mut SFOO;
27+
let x = (*sfoo).x();
28+
(*sfoo).x[1] += 1;
29+
*x += 1;
30+
}
31+
}

0 commit comments

Comments
 (0)