about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2015-02-10 08:43:18 -0800
committerAlex Crichton <alex@alexcrichton.com>2015-02-10 08:43:18 -0800
commitdf55acf1d055027ce834fe8c228669c37903a77f (patch)
tree0ef01a83e5951cf393ea0cbfdfc06bd81c66ee9a
parentba7b79008b5166b2b1cf043b4533917103d3e06e (diff)
parent3513c97302b72b6dd51136ce1e259ca0e67458f3 (diff)
downloadrust-df55acf1d055027ce834fe8c228669c37903a77f.tar.gz
rust-df55acf1d055027ce834fe8c228669c37903a77f.zip
rollup merge of #22143: pnkfelix/fix-issue-20801
Add error message (i.e. do not ICE) when moving out of unsafe pointers.

Fix #20801.
-rw-r--r--src/librustc_borrowck/borrowck/gather_loans/move_error.rs1
-rw-r--r--src/test/compile-fail/issue-20801.rs47
2 files changed, 48 insertions, 0 deletions
diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs
index b00973c82bd..10ffb89e728 100644
--- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs
+++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs
@@ -118,6 +118,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
     match move_from.cat {
         mc::cat_deref(_, _, mc::BorrowedPtr(..)) |
         mc::cat_deref(_, _, mc::Implicit(..)) |
+        mc::cat_deref(_, _, mc::UnsafePtr(..)) |
         mc::cat_static_item => {
             bccx.span_err(move_from.span,
                           &format!("cannot move out of {}",
diff --git a/src/test/compile-fail/issue-20801.rs b/src/test/compile-fail/issue-20801.rs
new file mode 100644
index 00000000000..a2565e88467
--- /dev/null
+++ b/src/test/compile-fail/issue-20801.rs
@@ -0,0 +1,47 @@
+// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// We used to ICE when moving out of a `*mut T` or `*const T`.
+
+struct T(u8);
+
+static mut GLOBAL_MUT_T: T = T(0);
+
+static GLOBAL_T: T = T(0);
+
+fn imm_ref() -> &'static T {
+    unsafe { &GLOBAL_T }
+}
+
+fn mut_ref() -> &'static mut T {
+    unsafe { &mut GLOBAL_MUT_T }
+}
+
+fn mut_ptr() -> *mut T {
+    unsafe { 0u8 as *mut T }
+}
+
+fn const_ptr() -> *const T {
+    unsafe { 0u8 as *const T }
+}
+
+pub fn main() {
+    let a = unsafe { *mut_ref() };
+    //~^ ERROR cannot move out of dereference of borrowed content
+
+    let b = unsafe { *imm_ref() };
+    //~^ ERROR cannot move out of dereference of borrowed content
+
+    let c = unsafe { *mut_ptr() };
+    //~^ ERROR cannot move out of dereference of unsafe pointer
+
+    let d = unsafe { *const_ptr() };
+    //~^ ERROR cannot move out of dereference of unsafe pointer
+}