Skip to content

Commit 9dab737

Browse files
authored
Rollup merge of rust-lang#47543 - topecongiro:issue-42344, r=nikomatsakis
Disallow mutable borrow to non-mut statics Closes rust-lang#42344.
2 parents cde119d + 3d114c7 commit 9dab737

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,22 +1068,12 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10681068
};
10691069

10701070
match cause {
1071-
mc::AliasableStatic => {
1072-
// This happens when we have an `&mut` or assignment to a
1073-
// static. We should have already reported a mutability
1074-
// violation first, but may have continued compiling.
1075-
self.tcx.sess.delay_span_bug(
1076-
span,
1077-
&format!("aliasability violation for static `{}`", prefix)
1078-
);
1079-
return;
1080-
}
10811071
mc::AliasableStaticMut => {
10821072
// This path cannot occur. `static mut X` is not checked
10831073
// for aliasability violations.
10841074
span_bug!(span, "aliasability violation for static mut `{}`", prefix)
10851075
}
1086-
mc::AliasableBorrowed => {}
1076+
mc::AliasableStatic | mc::AliasableBorrowed => {}
10871077
};
10881078
let blame = cmt.immutability_blame();
10891079
let mut err = match blame {

src/test/compile-fail/issue-42344.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2018 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+
static TAB: [&mut [u8]; 0] = [];
12+
13+
pub unsafe fn test() {
14+
TAB[0].iter_mut(); //~ ERROR cannot borrow data mutably in a `&` reference [E0389]
15+
}
16+
17+
pub fn main() {}

src/test/compile-fail/issue-46604.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ fn write<T: AsRef<[u8]>>(buffer: T) { }
1717

1818
fn main() {
1919
write(&buf);
20-
buf[0]=2; //[mir]~ ERROR E0594
20+
buf[0]=2; //[ast]~ ERROR E0389
21+
//[mir]~^ ERROR E0594
2122
}

0 commit comments

Comments
 (0)