Skip to content

Commit 3513c97

Browse files
committed
Add error message (i.e. do not ICE) when moving out of unsafe pointers.
Fix #20801.
1 parent 94c06a1 commit 3513c97

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/librustc_borrowck/borrowck/gather_loans/move_error.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
118118
match move_from.cat {
119119
mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
120120
mc::cat_deref(_, _, mc::Implicit(..)) |
121+
mc::cat_deref(_, _, mc::UnsafePtr(..)) |
121122
mc::cat_static_item => {
122123
bccx.span_err(move_from.span,
123124
&format!("cannot move out of {}",

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2015 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+
// We used to ICE when moving out of a `*mut T` or `*const T`.
12+
13+
struct T(u8);
14+
15+
static mut GLOBAL_MUT_T: T = T(0);
16+
17+
static GLOBAL_T: T = T(0);
18+
19+
fn imm_ref() -> &'static T {
20+
unsafe { &GLOBAL_T }
21+
}
22+
23+
fn mut_ref() -> &'static mut T {
24+
unsafe { &mut GLOBAL_MUT_T }
25+
}
26+
27+
fn mut_ptr() -> *mut T {
28+
unsafe { 0u8 as *mut T }
29+
}
30+
31+
fn const_ptr() -> *const T {
32+
unsafe { 0u8 as *const T }
33+
}
34+
35+
pub fn main() {
36+
let a = unsafe { *mut_ref() };
37+
//~^ ERROR cannot move out of dereference of borrowed content
38+
39+
let b = unsafe { *imm_ref() };
40+
//~^ ERROR cannot move out of dereference of borrowed content
41+
42+
let c = unsafe { *mut_ptr() };
43+
//~^ ERROR cannot move out of dereference of unsafe pointer
44+
45+
let d = unsafe { *const_ptr() };
46+
//~^ ERROR cannot move out of dereference of unsafe pointer
47+
}

0 commit comments

Comments
 (0)