From fe8ace8da3e2dba750fe09587628b2c36d303267 Mon Sep 17 00:00:00 2001 From: David Wood Date: Thu, 4 Oct 2018 22:40:17 +0200 Subject: [PATCH] Move errors specify "dereference of raw pointer". Previously, move errors involving the dereference of a raw pointer would say "borrowed content". This commit changes it to say "dereference of raw pointer". --- src/librustc_mir/borrow_check/move_errors.rs | 15 +++++++++++++++ .../borrowck-move-from-unsafe-ptr.nll.stderr | 4 ++-- src/test/ui/issues/issue-20801.nll.stderr | 8 ++++---- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/src/librustc_mir/borrow_check/move_errors.rs b/src/librustc_mir/borrow_check/move_errors.rs index 693cfea3c95f0..ea62694f8be75 100644 --- a/src/librustc_mir/borrow_check/move_errors.rs +++ b/src/librustc_mir/borrow_check/move_errors.rs @@ -68,6 +68,7 @@ enum GroupedMoveError<'tcx> { enum BorrowedContentSource { Arc, Rc, + DerefRawPointer, Other, } @@ -76,6 +77,7 @@ impl Display for BorrowedContentSource { match *self { BorrowedContentSource::Arc => write!(f, "an `Arc`"), BorrowedContentSource::Rc => write!(f, "an `Rc`"), + BorrowedContentSource::DerefRawPointer => write!(f, "dereference of raw pointer"), BorrowedContentSource::Other => write!(f, "borrowed content"), } } @@ -279,6 +281,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { self.prefixes(&original_path, PrefixSet::All) .any(|p| p.is_upvar_field_projection(self.mir, &self.infcx.tcx) .is_some()); + debug!("report: ty={:?}", ty); match ty.sty { ty::Array(..) | ty::Slice(..) => self.infcx.tcx.cannot_move_out_of_interior_noncopy( @@ -582,6 +585,18 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> { } } + // If we didn't find an `Arc` or an `Rc`, then check specifically for + // a dereference of a place that has the type of a raw pointer. + // We can't use `place.ty(..).to_ty(..)` here as that strips away the raw pointer. + if let Place::Projection(box Projection { + base, + elem: ProjectionElem::Deref, + }) = place { + if base.ty(self.mir, self.infcx.tcx).to_ty(self.infcx.tcx).is_unsafe_ptr() { + return BorrowedContentSource::DerefRawPointer; + } + } + BorrowedContentSource::Other } } diff --git a/src/test/ui/borrowck/borrowck-move-from-unsafe-ptr.nll.stderr b/src/test/ui/borrowck/borrowck-move-from-unsafe-ptr.nll.stderr index f823a6f08d789..c3a2180b9f082 100644 --- a/src/test/ui/borrowck/borrowck-move-from-unsafe-ptr.nll.stderr +++ b/src/test/ui/borrowck/borrowck-move-from-unsafe-ptr.nll.stderr @@ -1,10 +1,10 @@ -error[E0507]: cannot move out of borrowed content +error[E0507]: cannot move out of dereference of raw pointer --> $DIR/borrowck-move-from-unsafe-ptr.rs:13:13 | LL | let y = *x; //~ ERROR cannot move out of dereference of raw pointer | ^^ | | - | cannot move out of borrowed content + | cannot move out of dereference of raw pointer | help: consider removing the `*`: `x` error: aborting due to previous error diff --git a/src/test/ui/issues/issue-20801.nll.stderr b/src/test/ui/issues/issue-20801.nll.stderr index 3a6784eed67dd..362778b26c861 100644 --- a/src/test/ui/issues/issue-20801.nll.stderr +++ b/src/test/ui/issues/issue-20801.nll.stderr @@ -16,22 +16,22 @@ LL | let b = unsafe { *imm_ref() }; | cannot move out of borrowed content | help: consider removing the `*`: `imm_ref()` -error[E0507]: cannot move out of borrowed content +error[E0507]: cannot move out of dereference of raw pointer --> $DIR/issue-20801.rs:42:22 | LL | let c = unsafe { *mut_ptr() }; | ^^^^^^^^^^ | | - | cannot move out of borrowed content + | cannot move out of dereference of raw pointer | help: consider removing the `*`: `mut_ptr()` -error[E0507]: cannot move out of borrowed content +error[E0507]: cannot move out of dereference of raw pointer --> $DIR/issue-20801.rs:45:22 | LL | let d = unsafe { *const_ptr() }; | ^^^^^^^^^^^^ | | - | cannot move out of borrowed content + | cannot move out of dereference of raw pointer | help: consider removing the `*`: `const_ptr()` error: aborting due to 4 previous errors