2018-08-07 01:02:39 -07:00
|
|
|
use core::unicode::property::Pattern_White_Space;
|
2018-09-30 21:21:31 +02:00
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
use rustc::mir::*;
|
2019-05-05 11:16:56 +01:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt};
|
2018-09-15 03:18:29 +03:00
|
|
|
use rustc_errors::{DiagnosticBuilder,Applicability};
|
2018-06-27 22:06:54 +01:00
|
|
|
use syntax_pos::Span;
|
|
|
|
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::borrow_check::MirBorrowckCtxt;
|
|
|
|
use crate::borrow_check::prefixes::PrefixSet;
|
2019-05-05 12:02:17 +01:00
|
|
|
use crate::borrow_check::error_reporting::UseSpans;
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::dataflow::move_paths::{
|
2018-09-30 21:21:31 +02:00
|
|
|
IllegalMoveOrigin, IllegalMoveOriginKind, InitLocation,
|
|
|
|
LookupResult, MoveError, MovePathIndex,
|
|
|
|
};
|
2019-02-08 06:28:15 +09:00
|
|
|
use crate::util::borrowck_errors::{BorrowckErrors, Origin};
|
2018-06-27 22:06:54 +01:00
|
|
|
|
|
|
|
// Often when desugaring a pattern match we may have many individual moves in
|
|
|
|
// MIR that are all part of one operation from the user's point-of-view. For
|
|
|
|
// example:
|
|
|
|
//
|
|
|
|
// let (x, y) = foo()
|
|
|
|
//
|
|
|
|
// would move x from the 0 field of some temporary, and y from the 1 field. We
|
|
|
|
// group such errors together for cleaner error reporting.
|
|
|
|
//
|
|
|
|
// Errors are kept separate if they are from places with different parent move
|
|
|
|
// paths. For example, this generates two errors:
|
|
|
|
//
|
|
|
|
// let (&x, &y) = (&String::new(), &String::new());
|
|
|
|
#[derive(Debug)]
|
|
|
|
enum GroupedMoveError<'tcx> {
|
2018-08-07 01:02:39 -07:00
|
|
|
// Place expression can't be moved from,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., match x[0] { s => (), } where x: &[String]
|
2018-08-07 01:02:39 -07:00
|
|
|
MovesFromPlace {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
span: Span,
|
|
|
|
move_from: Place<'tcx>,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
binds_to: Vec<Local>,
|
|
|
|
},
|
2018-08-07 01:02:39 -07:00
|
|
|
// Part of a value expression can't be moved from,
|
2018-11-27 02:59:49 +00:00
|
|
|
// e.g., match &String::new() { &x => (), }
|
2018-08-07 01:02:39 -07:00
|
|
|
MovesFromValue {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
span: Span,
|
|
|
|
move_from: MovePathIndex,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
binds_to: Vec<Local>,
|
|
|
|
},
|
|
|
|
// Everything that isn't from pattern matching.
|
|
|
|
OtherIllegalMove {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2019-05-05 12:02:17 +01:00
|
|
|
use_spans: UseSpans,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
enum BorrowedContentSource<'tcx> {
|
2018-10-04 22:40:17 +02:00
|
|
|
DerefRawPointer,
|
2019-05-05 11:16:56 +01:00
|
|
|
DerefMutableRef,
|
|
|
|
DerefSharedRef,
|
|
|
|
OverloadedDeref(Ty<'tcx>),
|
|
|
|
OverloadedIndex(Ty<'tcx>),
|
2018-09-30 21:21:31 +02:00
|
|
|
}
|
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
impl BorrowedContentSource<'tcx> {
|
|
|
|
fn describe_for_unnamed_place(&self) -> String {
|
2018-09-30 21:21:31 +02:00
|
|
|
match *self {
|
2019-05-05 11:16:56 +01:00
|
|
|
BorrowedContentSource::DerefRawPointer => format!("a raw pointer"),
|
|
|
|
BorrowedContentSource::DerefSharedRef => format!("a shared reference"),
|
|
|
|
BorrowedContentSource::DerefMutableRef => {
|
|
|
|
format!("a mutable reference")
|
|
|
|
}
|
|
|
|
BorrowedContentSource::OverloadedDeref(ty) => {
|
|
|
|
if ty.is_rc() {
|
|
|
|
format!("an `Rc`")
|
|
|
|
} else if ty.is_arc() {
|
|
|
|
format!("an `Arc`")
|
|
|
|
} else {
|
|
|
|
format!("dereference of `{}`", ty)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn describe_for_named_place(&self) -> Option<&'static str> {
|
|
|
|
match *self {
|
|
|
|
BorrowedContentSource::DerefRawPointer => Some("raw pointer"),
|
|
|
|
BorrowedContentSource::DerefSharedRef => Some("shared reference"),
|
|
|
|
BorrowedContentSource::DerefMutableRef => Some("mutable reference"),
|
|
|
|
// Overloaded deref and index operators should be evaluated into a
|
|
|
|
// temporary. So we don't need a description here.
|
|
|
|
BorrowedContentSource::OverloadedDeref(_)
|
|
|
|
| BorrowedContentSource::OverloadedIndex(_) => None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn from_call(func: Ty<'tcx>, tcx: TyCtxt<'_, '_, 'tcx>) -> Option<Self> {
|
|
|
|
match func.sty {
|
|
|
|
ty::FnDef(def_id, substs) => {
|
|
|
|
let trait_id = tcx.trait_of_item(def_id)?;
|
|
|
|
|
|
|
|
let lang_items = tcx.lang_items();
|
|
|
|
if Some(trait_id) == lang_items.deref_trait()
|
|
|
|
|| Some(trait_id) == lang_items.deref_mut_trait()
|
|
|
|
{
|
|
|
|
Some(BorrowedContentSource::OverloadedDeref(substs.type_at(0)))
|
|
|
|
} else if Some(trait_id) == lang_items.index_trait()
|
|
|
|
|| Some(trait_id) == lang_items.index_mut_trait()
|
|
|
|
{
|
|
|
|
Some(BorrowedContentSource::OverloadedIndex(substs.type_at(0)))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => None,
|
2018-09-30 21:21:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-13 21:56:04 +01:00
|
|
|
impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
|
2018-08-07 17:06:21 +02:00
|
|
|
pub(crate) fn report_move_errors(&mut self, move_errors: Vec<(Place<'tcx>, MoveError<'tcx>)>) {
|
2018-06-27 22:06:54 +01:00
|
|
|
let grouped_errors = self.group_move_errors(move_errors);
|
|
|
|
for error in grouped_errors {
|
|
|
|
self.report(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 17:06:21 +02:00
|
|
|
fn group_move_errors(
|
|
|
|
&self,
|
|
|
|
errors: Vec<(Place<'tcx>, MoveError<'tcx>)>
|
|
|
|
) -> Vec<GroupedMoveError<'tcx>> {
|
2018-06-27 22:06:54 +01:00
|
|
|
let mut grouped_errors = Vec::new();
|
2018-08-07 17:06:21 +02:00
|
|
|
for (original_path, error) in errors {
|
|
|
|
self.append_to_grouped_errors(&mut grouped_errors, original_path, error);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
grouped_errors
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_to_grouped_errors(
|
2018-07-13 21:56:04 +01:00
|
|
|
&self,
|
2018-06-27 22:06:54 +01:00
|
|
|
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
error: MoveError<'tcx>,
|
|
|
|
) {
|
|
|
|
match error {
|
|
|
|
MoveError::UnionMove { .. } => {
|
|
|
|
unimplemented!("don't know how to report union move errors yet.")
|
|
|
|
}
|
|
|
|
MoveError::IllegalMove {
|
|
|
|
cannot_move_out_of: IllegalMoveOrigin { location, kind },
|
|
|
|
} => {
|
2018-07-22 21:38:31 +01:00
|
|
|
// Note: that the only time we assign a place isn't a temporary
|
|
|
|
// to a user variable is when initializing it.
|
|
|
|
// If that ever stops being the case, then the ever initialized
|
|
|
|
// flow could be used.
|
2018-06-27 22:06:54 +01:00
|
|
|
if let Some(StatementKind::Assign(
|
2019-02-22 05:24:03 +01:00
|
|
|
Place::Base(PlaceBase::Local(local)),
|
2018-09-24 11:32:31 +10:00
|
|
|
box Rvalue::Use(Operand::Move(move_from)),
|
2018-06-27 22:06:54 +01:00
|
|
|
)) = self.mir.basic_blocks()[location.block]
|
|
|
|
.statements
|
|
|
|
.get(location.statement_index)
|
|
|
|
.map(|stmt| &stmt.kind)
|
|
|
|
{
|
|
|
|
let local_decl = &self.mir.local_decls[*local];
|
2018-07-18 21:03:07 +01:00
|
|
|
// opt_match_place is the
|
|
|
|
// match_span is the span of the expression being matched on
|
|
|
|
// match *x.y { ... } match_place is Some(*x.y)
|
|
|
|
// ^^^^ match_span is the span of *x.y
|
|
|
|
//
|
|
|
|
// opt_match_place is None for let [mut] x = ... statements,
|
|
|
|
// whether or not the right-hand side is a place expression
|
2018-06-27 22:06:54 +01:00
|
|
|
if let Some(ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
|
|
|
opt_match_place: Some((ref opt_match_place, match_span)),
|
|
|
|
binding_mode: _,
|
|
|
|
opt_ty_info: _,
|
2018-08-07 01:02:39 -07:00
|
|
|
pat_span: _,
|
2018-06-27 22:06:54 +01:00
|
|
|
}))) = local_decl.is_user_variable
|
|
|
|
{
|
2019-05-05 12:02:17 +01:00
|
|
|
let stmt_source_info = self.mir.source_info(location);
|
2018-07-22 21:38:31 +01:00
|
|
|
self.append_binding_error(
|
|
|
|
grouped_errors,
|
|
|
|
kind,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-07-22 21:38:31 +01:00
|
|
|
move_from,
|
|
|
|
*local,
|
|
|
|
opt_match_place,
|
|
|
|
match_span,
|
|
|
|
stmt_source_info.span,
|
|
|
|
);
|
|
|
|
return;
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2019-05-05 12:02:17 +01:00
|
|
|
|
|
|
|
let move_spans = self.move_spans(&original_path, location);
|
2018-06-27 22:06:54 +01:00
|
|
|
grouped_errors.push(GroupedMoveError::OtherIllegalMove {
|
2019-05-05 12:02:17 +01:00
|
|
|
use_spans: move_spans,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn append_binding_error(
|
2018-07-13 21:56:04 +01:00
|
|
|
&self,
|
2018-06-27 22:06:54 +01:00
|
|
|
grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
move_from: &Place<'tcx>,
|
|
|
|
bind_to: Local,
|
|
|
|
match_place: &Option<Place<'tcx>>,
|
|
|
|
match_span: Span,
|
2018-07-22 21:38:31 +01:00
|
|
|
statement_span: Span,
|
2018-06-27 22:06:54 +01:00
|
|
|
) {
|
|
|
|
debug!(
|
2018-08-01 23:53:28 -07:00
|
|
|
"append_binding_error(match_place={:?}, match_span={:?})",
|
2018-06-27 22:06:54 +01:00
|
|
|
match_place, match_span
|
|
|
|
);
|
|
|
|
|
|
|
|
let from_simple_let = match_place.is_none();
|
|
|
|
let match_place = match_place.as_ref().unwrap_or(move_from);
|
|
|
|
|
|
|
|
match self.move_data.rev_lookup.find(match_place) {
|
|
|
|
// Error with the match place
|
|
|
|
LookupResult::Parent(_) => {
|
|
|
|
for ge in &mut *grouped_errors {
|
2018-08-07 01:02:39 -07:00
|
|
|
if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge {
|
2018-06-27 22:06:54 +01:00
|
|
|
if match_span == *span {
|
|
|
|
debug!("appending local({:?}) to list", bind_to);
|
|
|
|
if !binds_to.is_empty() {
|
|
|
|
binds_to.push(bind_to);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("found a new move error location");
|
|
|
|
|
|
|
|
// Don't need to point to x in let x = ... .
|
2018-07-22 21:38:31 +01:00
|
|
|
let (binds_to, span) = if from_simple_let {
|
|
|
|
(vec![], statement_span)
|
2018-06-27 22:06:54 +01:00
|
|
|
} else {
|
2018-07-22 21:38:31 +01:00
|
|
|
(vec![bind_to], match_span)
|
2018-06-27 22:06:54 +01:00
|
|
|
};
|
2018-08-07 01:02:39 -07:00
|
|
|
grouped_errors.push(GroupedMoveError::MovesFromPlace {
|
2018-07-22 21:38:31 +01:00
|
|
|
span,
|
2018-06-27 22:06:54 +01:00
|
|
|
move_from: match_place.clone(),
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind,
|
|
|
|
binds_to,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
// Error with the pattern
|
|
|
|
LookupResult::Exact(_) => {
|
|
|
|
let mpi = match self.move_data.rev_lookup.find(move_from) {
|
|
|
|
LookupResult::Parent(Some(mpi)) => mpi,
|
|
|
|
// move_from should be a projection from match_place.
|
|
|
|
_ => unreachable!("Probably not unreachable..."),
|
|
|
|
};
|
|
|
|
for ge in &mut *grouped_errors {
|
2018-08-07 01:02:39 -07:00
|
|
|
if let GroupedMoveError::MovesFromValue {
|
2018-06-27 22:06:54 +01:00
|
|
|
span,
|
|
|
|
move_from: other_mpi,
|
|
|
|
binds_to,
|
|
|
|
..
|
|
|
|
} = ge
|
|
|
|
{
|
|
|
|
if match_span == *span && mpi == *other_mpi {
|
|
|
|
debug!("appending local({:?}) to list", bind_to);
|
|
|
|
binds_to.push(bind_to);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
debug!("found a new move error location");
|
2018-08-07 01:02:39 -07:00
|
|
|
grouped_errors.push(GroupedMoveError::MovesFromValue {
|
2018-06-27 22:06:54 +01:00
|
|
|
span: match_span,
|
|
|
|
move_from: mpi,
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path,
|
2018-06-27 22:06:54 +01:00
|
|
|
kind,
|
|
|
|
binds_to: vec![bind_to],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-07-18 18:10:08 -03:00
|
|
|
fn report(&mut self, error: GroupedMoveError<'tcx>) {
|
2018-06-27 22:06:54 +01:00
|
|
|
let (mut err, err_span) = {
|
2019-02-08 06:28:15 +09:00
|
|
|
let (span, original_path, kind): (Span, &Place<'tcx>, &IllegalMoveOriginKind<'_>) =
|
2018-08-07 17:06:21 +02:00
|
|
|
match error {
|
2019-05-24 19:10:00 -07:00
|
|
|
GroupedMoveError::MovesFromPlace { span, ref original_path, ref kind, .. } |
|
2019-05-05 12:02:17 +01:00
|
|
|
GroupedMoveError::MovesFromValue { span, ref original_path, ref kind, .. } => {
|
2018-08-07 17:06:21 +02:00
|
|
|
(span, original_path, kind)
|
2019-05-05 12:02:17 +01:00
|
|
|
}
|
|
|
|
GroupedMoveError::OtherIllegalMove {
|
|
|
|
use_spans,
|
|
|
|
ref original_path,
|
|
|
|
ref kind
|
|
|
|
} => {
|
|
|
|
(use_spans.args_or_use(), original_path, kind)
|
2018-08-07 17:06:21 +02:00
|
|
|
},
|
|
|
|
};
|
|
|
|
debug!("report: original_path={:?} span={:?}, kind={:?} \
|
|
|
|
original_path.is_upvar_field_projection={:?}", original_path, span, kind,
|
2018-05-16 15:38:32 +03:00
|
|
|
self.is_upvar_field_projection(original_path));
|
2019-05-05 11:16:56 +01:00
|
|
|
(
|
|
|
|
match kind {
|
|
|
|
IllegalMoveOriginKind::Static => {
|
|
|
|
self.report_cannot_move_from_static(original_path, span)
|
|
|
|
}
|
|
|
|
IllegalMoveOriginKind::BorrowedContent { target_place } => {
|
|
|
|
self.report_cannot_move_from_borrowed_content(
|
|
|
|
original_path,
|
|
|
|
target_place,
|
2019-05-24 19:10:00 -07:00
|
|
|
span,
|
2019-05-05 11:16:56 +01:00
|
|
|
)
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2019-05-05 11:16:56 +01:00
|
|
|
IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
|
|
|
|
self.infcx.tcx
|
|
|
|
.cannot_move_out_of_interior_of_drop(span, ty, Origin::Mir)
|
|
|
|
}
|
|
|
|
IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } =>
|
|
|
|
self.infcx.tcx.cannot_move_out_of_interior_noncopy(
|
|
|
|
span, ty, Some(*is_index), Origin::Mir
|
|
|
|
),
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
)
|
2018-06-27 22:06:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
self.add_move_hints(error, &mut err, err_span);
|
2018-07-18 18:10:08 -03:00
|
|
|
err.buffer(&mut self.errors_buffer);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
fn report_cannot_move_from_static(
|
|
|
|
&mut self,
|
|
|
|
place: &Place<'tcx>,
|
|
|
|
span: Span
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
|
|
|
let mut base_static = place;
|
|
|
|
loop {
|
|
|
|
match base_static {
|
|
|
|
Place::Base(_) => break,
|
|
|
|
Place::Projection(box Projection { base, .. }) => base_static = base,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let description = if let Place::Base(_) = place {
|
|
|
|
format!("static item `{}`", self.describe_place(place).unwrap())
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"`{:?}` as `{:?}` is a static item",
|
|
|
|
self.describe_place(place).unwrap(),
|
|
|
|
self.describe_place(base_static).unwrap(),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
self.infcx.tcx.cannot_move_out_of(span, &description, Origin::Mir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn report_cannot_move_from_borrowed_content(
|
|
|
|
&mut self,
|
|
|
|
move_place: &Place<'tcx>,
|
|
|
|
deref_target_place: &Place<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
) -> DiagnosticBuilder<'a> {
|
|
|
|
let origin = Origin::Mir;
|
|
|
|
|
|
|
|
// Inspect the type of the content behind the
|
|
|
|
// borrow to provide feedback about why this
|
|
|
|
// was a move rather than a copy.
|
|
|
|
let ty = deref_target_place.ty(self.mir, self.infcx.tcx).ty;
|
|
|
|
let upvar_field = self.prefixes(&move_place, PrefixSet::All)
|
|
|
|
.find_map(|p| self.is_upvar_field_projection(p));
|
|
|
|
|
|
|
|
let deref_base = match deref_target_place {
|
|
|
|
Place::Projection(box Projection { base, elem: ProjectionElem::Deref }) => base,
|
|
|
|
_ => bug!("deref_target_place is not a deref projection"),
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Place::Base(PlaceBase::Local(local)) = *deref_base {
|
|
|
|
let decl = &self.mir.local_decls[local];
|
|
|
|
if decl.is_ref_for_guard() {
|
|
|
|
let mut err = self.infcx.tcx.cannot_move_out_of(
|
|
|
|
span,
|
|
|
|
&format!("`{}` in pattern guard", decl.name.unwrap()),
|
|
|
|
origin,
|
|
|
|
);
|
|
|
|
err.note(
|
|
|
|
"variables bound in patterns cannot be moved from \
|
|
|
|
until after the end of the pattern guard");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("report: ty={:?}", ty);
|
|
|
|
let mut err = match ty.sty {
|
|
|
|
ty::Array(..) | ty::Slice(..) =>
|
|
|
|
self.infcx.tcx.cannot_move_out_of_interior_noncopy(
|
|
|
|
span, ty, None, origin
|
|
|
|
),
|
|
|
|
ty::Closure(def_id, closure_substs)
|
|
|
|
if def_id == self.mir_def_id && upvar_field.is_some()
|
|
|
|
=> {
|
|
|
|
let closure_kind_ty = closure_substs.closure_kind_ty(def_id, self.infcx.tcx);
|
|
|
|
let closure_kind = closure_kind_ty.to_opt_closure_kind();
|
|
|
|
let capture_description = match closure_kind {
|
|
|
|
Some(ty::ClosureKind::Fn) => {
|
|
|
|
"captured variable in an `Fn` closure"
|
|
|
|
}
|
|
|
|
Some(ty::ClosureKind::FnMut) => {
|
|
|
|
"captured variable in an `FnMut` closure"
|
|
|
|
}
|
|
|
|
Some(ty::ClosureKind::FnOnce) => {
|
|
|
|
bug!("closure kind does not match first argument type")
|
|
|
|
}
|
|
|
|
None => bug!("closure kind not inferred by borrowck"),
|
|
|
|
};
|
|
|
|
|
|
|
|
let upvar = &self.upvars[upvar_field.unwrap().index()];
|
|
|
|
let upvar_hir_id = upvar.var_hir_id;
|
|
|
|
let upvar_name = upvar.name;
|
|
|
|
let upvar_span = self.infcx.tcx.hir().span_by_hir_id(upvar_hir_id);
|
|
|
|
|
|
|
|
let place_name = self.describe_place(move_place).unwrap();
|
|
|
|
|
|
|
|
let place_description = if self.is_upvar_field_projection(move_place).is_some() {
|
|
|
|
format!("`{}`, a {}", place_name, capture_description)
|
|
|
|
} else {
|
|
|
|
format!(
|
|
|
|
"`{}`, as `{}` is a {}",
|
|
|
|
place_name,
|
|
|
|
upvar_name,
|
|
|
|
capture_description,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
debug!(
|
|
|
|
"report: closure_kind_ty={:?} closure_kind={:?} place_description={:?}",
|
|
|
|
closure_kind_ty, closure_kind, place_description,
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut diag = self.infcx.tcx.cannot_move_out_of(span, &place_description, origin);
|
|
|
|
|
|
|
|
diag.span_label(upvar_span, "captured outer variable");
|
|
|
|
|
|
|
|
diag
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let source = self.borrowed_content_source(deref_base);
|
|
|
|
match (self.describe_place(move_place), source.describe_for_named_place()) {
|
|
|
|
(Some(place_desc), Some(source_desc)) => {
|
|
|
|
self.infcx.tcx.cannot_move_out_of(
|
|
|
|
span,
|
|
|
|
&format!("`{}` which is behind a {}", place_desc, source_desc),
|
|
|
|
origin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
(_, _) => {
|
|
|
|
self.infcx.tcx.cannot_move_out_of(
|
|
|
|
span,
|
|
|
|
&source.describe_for_unnamed_place(),
|
|
|
|
origin,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
let move_ty = format!(
|
|
|
|
"{:?}",
|
|
|
|
move_place.ty(self.mir, self.infcx.tcx).ty,
|
|
|
|
);
|
|
|
|
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
|
|
|
|
let is_option = move_ty.starts_with("std::option::Option");
|
|
|
|
let is_result = move_ty.starts_with("std::result::Result");
|
|
|
|
if is_option || is_result {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
&format!("consider borrowing the `{}`'s content", if is_option {
|
|
|
|
"Option"
|
|
|
|
} else {
|
|
|
|
"Result"
|
|
|
|
}),
|
|
|
|
format!("{}.as_ref()", snippet),
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
err
|
|
|
|
}
|
|
|
|
|
2018-06-27 22:06:54 +01:00
|
|
|
fn add_move_hints(
|
2018-07-13 21:56:04 +01:00
|
|
|
&self,
|
2018-06-27 22:06:54 +01:00
|
|
|
error: GroupedMoveError<'tcx>,
|
|
|
|
err: &mut DiagnosticBuilder<'a>,
|
|
|
|
span: Span,
|
|
|
|
) {
|
2018-09-11 16:38:35 +02:00
|
|
|
let snippet = self.infcx.tcx.sess.source_map().span_to_snippet(span).unwrap();
|
2018-06-27 22:06:54 +01:00
|
|
|
match error {
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromPlace {
|
2018-06-27 22:06:54 +01:00
|
|
|
mut binds_to,
|
|
|
|
move_from,
|
|
|
|
..
|
|
|
|
} => {
|
2018-08-07 23:44:35 -07:00
|
|
|
let try_remove_deref = match move_from {
|
2019-05-01 15:34:51 +01:00
|
|
|
Place::Projection(box Projection {
|
2018-08-07 01:02:39 -07:00
|
|
|
elem: ProjectionElem::Deref,
|
|
|
|
..
|
2018-08-07 23:44:35 -07:00
|
|
|
}) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if try_remove_deref && snippet.starts_with('*') {
|
2018-08-14 12:54:31 -07:00
|
|
|
// The snippet doesn't start with `*` in (e.g.) index
|
|
|
|
// expressions `a[b]`, which roughly desugar to
|
|
|
|
// `*Index::index(&a, b)` or
|
2018-08-07 23:44:35 -07:00
|
|
|
// `*IndexMut::index_mut(&mut a, b)`.
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2018-08-07 23:44:35 -07:00
|
|
|
span,
|
2018-08-12 13:27:14 -07:00
|
|
|
"consider removing the `*`",
|
2018-08-07 23:44:35 -07:00
|
|
|
snippet[1..].to_owned(),
|
2018-09-15 03:18:29 +03:00
|
|
|
Applicability::Unspecified,
|
2018-08-07 23:44:35 -07:00
|
|
|
);
|
|
|
|
} else {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2018-08-07 23:44:35 -07:00
|
|
|
span,
|
2018-08-12 13:55:34 -07:00
|
|
|
"consider borrowing here",
|
2018-08-07 23:44:35 -07:00
|
|
|
format!("&{}", snippet),
|
2018-09-15 03:18:29 +03:00
|
|
|
Applicability::Unspecified,
|
2018-08-07 23:44:35 -07:00
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 23:44:35 -07:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
if binds_to.is_empty() {
|
|
|
|
let place_ty = move_from.ty(self.mir, self.infcx.tcx).ty;
|
|
|
|
let place_desc = match self.describe_place(&move_from) {
|
|
|
|
Some(desc) => format!("`{}`", desc),
|
|
|
|
None => format!("value"),
|
|
|
|
};
|
|
|
|
|
|
|
|
self.note_type_does_not_implement_copy(
|
|
|
|
err,
|
|
|
|
&place_desc,
|
|
|
|
place_ty,
|
|
|
|
Some(span)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
binds_to.sort();
|
|
|
|
binds_to.dedup();
|
|
|
|
|
|
|
|
self.add_move_error_details(err, &binds_to);
|
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromValue { mut binds_to, .. } => {
|
2018-06-27 22:06:54 +01:00
|
|
|
binds_to.sort();
|
|
|
|
binds_to.dedup();
|
2018-08-07 01:02:39 -07:00
|
|
|
self.add_move_error_suggestions(err, &binds_to);
|
2018-08-14 12:54:31 -07:00
|
|
|
self.add_move_error_details(err, &binds_to);
|
2018-08-07 01:02:39 -07:00
|
|
|
}
|
|
|
|
// No binding. Nothing to suggest.
|
2019-05-05 12:02:17 +01:00
|
|
|
GroupedMoveError::OtherIllegalMove { ref original_path, use_spans, .. } => {
|
|
|
|
let span = use_spans.var_or_use();
|
2019-05-05 11:16:56 +01:00
|
|
|
let place_ty = original_path.ty(self.mir, self.infcx.tcx).ty;
|
|
|
|
let place_desc = match self.describe_place(original_path) {
|
|
|
|
Some(desc) => format!("`{}`", desc),
|
|
|
|
None => format!("value"),
|
|
|
|
};
|
|
|
|
self.note_type_does_not_implement_copy(
|
|
|
|
err,
|
|
|
|
&place_desc,
|
|
|
|
place_ty,
|
|
|
|
Some(span),
|
|
|
|
);
|
2019-05-05 12:02:17 +01:00
|
|
|
|
|
|
|
use_spans.args_span_label(err, format!("move out of {} occurs here", place_desc));
|
|
|
|
use_spans.var_span_label(
|
|
|
|
err,
|
|
|
|
format!("move occurs due to use{}", use_spans.describe()),
|
|
|
|
);
|
2019-05-05 11:16:56 +01:00
|
|
|
},
|
2018-08-07 01:02:39 -07:00
|
|
|
}
|
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
fn add_move_error_suggestions(
|
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'a>,
|
|
|
|
binds_to: &[Local],
|
|
|
|
) {
|
2018-08-13 22:01:54 -07:00
|
|
|
let mut suggestions: Vec<(Span, &str, String)> = Vec::new();
|
2018-08-07 01:02:39 -07:00
|
|
|
for local in binds_to {
|
|
|
|
let bind_to = &self.mir.local_decls[*local];
|
|
|
|
if let Some(
|
|
|
|
ClearCrossCrate::Set(BindingForm::Var(VarBindingForm {
|
|
|
|
pat_span,
|
|
|
|
..
|
|
|
|
}))
|
|
|
|
) = bind_to.is_user_variable {
|
2018-09-11 16:38:35 +02:00
|
|
|
let pat_snippet = self.infcx.tcx.sess.source_map()
|
2018-08-07 01:02:39 -07:00
|
|
|
.span_to_snippet(pat_span)
|
|
|
|
.unwrap();
|
|
|
|
if pat_snippet.starts_with('&') {
|
2018-12-05 06:42:56 -08:00
|
|
|
let pat_snippet = pat_snippet[1..].trim_start();
|
2018-08-07 01:02:39 -07:00
|
|
|
let suggestion;
|
2018-08-12 13:27:14 -07:00
|
|
|
let to_remove;
|
2018-08-07 01:02:39 -07:00
|
|
|
if pat_snippet.starts_with("mut")
|
|
|
|
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
|
|
|
|
{
|
2018-12-05 06:42:56 -08:00
|
|
|
suggestion = pat_snippet["mut".len()..].trim_start();
|
2018-08-12 13:27:14 -07:00
|
|
|
to_remove = "&mut";
|
2018-07-31 17:22:12 +02:00
|
|
|
} else {
|
2018-08-07 01:02:39 -07:00
|
|
|
suggestion = pat_snippet;
|
2018-08-12 13:27:14 -07:00
|
|
|
to_remove = "&";
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-13 03:17:53 -07:00
|
|
|
suggestions.push((
|
2018-08-07 01:02:39 -07:00
|
|
|
pat_span,
|
2018-08-13 22:01:54 -07:00
|
|
|
to_remove,
|
2018-08-07 01:02:39 -07:00
|
|
|
suggestion.to_owned(),
|
2018-08-13 03:17:53 -07:00
|
|
|
));
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-13 03:17:53 -07:00
|
|
|
suggestions.sort_unstable_by_key(|&(span, _, _)| span);
|
|
|
|
suggestions.dedup_by_key(|&mut (span, _, _)| span);
|
2018-08-13 22:01:54 -07:00
|
|
|
for (span, to_remove, suggestion) in suggestions {
|
2019-01-25 16:03:27 -05:00
|
|
|
err.span_suggestion(
|
2018-08-13 22:01:54 -07:00
|
|
|
span,
|
|
|
|
&format!("consider removing the `{}`", to_remove),
|
2018-09-15 03:18:29 +03:00
|
|
|
suggestion,
|
2018-09-17 03:16:08 +03:00
|
|
|
Applicability::MachineApplicable,
|
2018-08-13 22:01:54 -07:00
|
|
|
);
|
2018-08-13 03:17:53 -07:00
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
|
2018-08-14 12:54:31 -07:00
|
|
|
fn add_move_error_details(
|
2018-08-07 01:02:39 -07:00
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'a>,
|
|
|
|
binds_to: &[Local],
|
|
|
|
) {
|
2018-08-13 14:51:27 -07:00
|
|
|
let mut noncopy_var_spans = Vec::new();
|
2018-08-07 01:02:39 -07:00
|
|
|
for (j, local) in binds_to.into_iter().enumerate() {
|
|
|
|
let bind_to = &self.mir.local_decls[*local];
|
|
|
|
let binding_span = bind_to.source_info.span;
|
2018-06-27 22:06:54 +01:00
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
if j == 0 {
|
2018-10-16 15:10:59 +02:00
|
|
|
err.span_label(binding_span, "data moved here");
|
2018-08-07 01:02:39 -07:00
|
|
|
} else {
|
2018-10-16 15:10:59 +02:00
|
|
|
err.span_label(binding_span, "...and here");
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
|
2018-08-13 14:51:27 -07:00
|
|
|
if binds_to.len() == 1 {
|
2019-05-04 22:28:22 +01:00
|
|
|
self.note_type_does_not_implement_copy(
|
|
|
|
err,
|
|
|
|
&format!("`{}`", bind_to.name.unwrap()),
|
|
|
|
bind_to.ty,
|
|
|
|
Some(binding_span)
|
2018-08-13 14:51:27 -07:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
noncopy_var_spans.push(binding_span);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if binds_to.len() > 1 {
|
2018-08-07 01:02:39 -07:00
|
|
|
err.span_note(
|
2018-08-13 14:51:27 -07:00
|
|
|
noncopy_var_spans,
|
|
|
|
"move occurs because these variables have types that \
|
|
|
|
don't implement the `Copy` trait",
|
2018-08-07 01:02:39 -07:00
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-09-30 21:21:31 +02:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
fn borrowed_content_source(&self, deref_base: &Place<'tcx>) -> BorrowedContentSource<'tcx> {
|
|
|
|
let tcx = self.infcx.tcx;
|
2018-09-30 21:21:31 +02:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
// Look up the provided place and work out the move path index for it,
|
|
|
|
// we'll use this to check whether it was originally from an overloaded
|
|
|
|
// operator.
|
|
|
|
match self.move_data.rev_lookup.find(deref_base) {
|
|
|
|
LookupResult::Exact(mpi) | LookupResult::Parent(Some(mpi)) => {
|
|
|
|
debug!("borrowed_content_source: mpi={:?}", mpi);
|
|
|
|
|
|
|
|
for i in &self.move_data.init_path_map[mpi] {
|
|
|
|
let init = &self.move_data.inits[*i];
|
|
|
|
debug!("borrowed_content_source: init={:?}", init);
|
|
|
|
// We're only interested in statements that initialized a value, not the
|
|
|
|
// initializations from arguments.
|
|
|
|
let loc = match init.location {
|
|
|
|
InitLocation::Statement(stmt) => stmt,
|
|
|
|
_ => continue,
|
|
|
|
};
|
2018-09-30 21:21:31 +02:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
let bbd = &self.mir[loc.block];
|
|
|
|
let is_terminator = bbd.statements.len() == loc.statement_index;
|
|
|
|
debug!(
|
|
|
|
"borrowed_content_source: loc={:?} is_terminator={:?}",
|
|
|
|
loc,
|
|
|
|
is_terminator,
|
|
|
|
);
|
|
|
|
if !is_terminator {
|
|
|
|
continue;
|
|
|
|
} else if let Some(Terminator {
|
|
|
|
kind: TerminatorKind::Call {
|
|
|
|
ref func,
|
|
|
|
from_hir_call: false,
|
|
|
|
..
|
|
|
|
},
|
|
|
|
..
|
|
|
|
}) = bbd.terminator {
|
|
|
|
if let Some(source)
|
|
|
|
= BorrowedContentSource::from_call(func.ty(self.mir, tcx), tcx)
|
|
|
|
{
|
|
|
|
return source;
|
2018-09-30 21:21:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-05-05 11:16:56 +01:00
|
|
|
// Base is a `static` so won't be from an overloaded operator
|
|
|
|
_ => (),
|
|
|
|
};
|
2018-09-30 21:21:31 +02:00
|
|
|
|
2019-05-05 11:16:56 +01:00
|
|
|
// If we didn't find an overloaded deref or index, then assume it's a
|
|
|
|
// built in deref and check the type of the base.
|
|
|
|
let base_ty = deref_base.ty(self.mir, tcx).ty;
|
|
|
|
if base_ty.is_unsafe_ptr() {
|
|
|
|
BorrowedContentSource::DerefRawPointer
|
|
|
|
} else if base_ty.is_mutable_pointer() {
|
|
|
|
BorrowedContentSource::DerefMutableRef
|
|
|
|
} else {
|
|
|
|
BorrowedContentSource::DerefSharedRef
|
2018-10-04 22:40:17 +02:00
|
|
|
}
|
2018-09-30 21:21:31 +02:00
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|