Skip to content

Commit f26c0b4

Browse files
committed
Delay panic for aliasing violation for static items.
1 parent d5f2745 commit f26c0b4

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

Diff for: src/librustc_borrowck/borrowck/mod.rs

+13-8
Original file line numberDiff line numberDiff line change
@@ -1098,15 +1098,20 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10981098
};
10991099

11001100
match cause {
1101-
mc::AliasableStatic |
1101+
mc::AliasableStatic => {
1102+
// This happens when we have an `&mut` or assignment to a
1103+
// static. We should have already reported a mutability
1104+
// violation first, but may have continued compiling.
1105+
self.tcx.sess.delay_span_bug(
1106+
span,
1107+
&format!("aliasability violation for static `{}`", prefix)
1108+
);
1109+
return;
1110+
}
11021111
mc::AliasableStaticMut => {
1103-
// This path cannot occur. It happens when we have an
1104-
// `&mut` or assignment to a static. But in the case
1105-
// of `static X`, we get a mutability violation first,
1106-
// and never get here. In the case of `static mut X`,
1107-
// that is unsafe and hence the aliasability error is
1108-
// ignored.
1109-
span_bug!(span, "aliasability violation for static `{}`", prefix)
1112+
// This path cannot occur. `static mut X` is not checked
1113+
// for aliasability violations.
1114+
span_bug!(span, "aliasability violation for static mut `{}`", prefix)
11101115
}
11111116
mc::AliasableBorrowed => {}
11121117
};

Diff for: src/test/compile-fail/issue-46604.rs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright 2017 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 borrowck=mir
13+
14+
static buf: &mut [u8] = &mut [1u8,2,3,4,5,7]; //[ast]~ ERROR E0017
15+
//[mir]~^ ERROR E0017
16+
fn write<T: AsRef<[u8]>>(buffer: T) { }
17+
18+
fn main() {
19+
write(&buf);
20+
buf[0]=2; //[mir]~ ERROR E0594
21+
}

0 commit comments

Comments
 (0)