2018-06-27 22:06:54 +01:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
use core::unicode::property::Pattern_White_Space;
|
2018-06-27 22:06:54 +01:00
|
|
|
use rustc::mir::*;
|
2018-07-13 21:56:04 +01:00
|
|
|
use rustc::ty;
|
2018-06-27 22:06:54 +01:00
|
|
|
use rustc_errors::DiagnosticBuilder;
|
2018-08-07 17:06:21 +02:00
|
|
|
use rustc_data_structures::indexed_vec::Idx;
|
2018-06-27 22:06:54 +01:00
|
|
|
use syntax_pos::Span;
|
|
|
|
|
2018-07-13 21:56:04 +01:00
|
|
|
use borrow_check::MirBorrowckCtxt;
|
2018-08-07 19:50:19 +02:00
|
|
|
use borrow_check::prefixes::PrefixSet;
|
2018-07-13 21:56:04 +01:00
|
|
|
use dataflow::move_paths::{IllegalMoveOrigin, IllegalMoveOriginKind};
|
2018-06-27 22:06:54 +01:00
|
|
|
use dataflow::move_paths::{LookupResult, MoveError, MovePathIndex};
|
|
|
|
use util::borrowck_errors::{BorrowckErrors, Origin};
|
|
|
|
|
|
|
|
// 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-06-27 22:06:54 +01: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-06-27 22:06:54 +01: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.
|
2018-08-07 01:02:39 -07:00
|
|
|
// FIXME(ashtneoi): I think this is only for moves into temporaries, as
|
|
|
|
// when returning a value. Clarification needed.
|
2018-06-27 22:06:54 +01:00
|
|
|
OtherIllegalMove {
|
2018-08-07 17:06:21 +02:00
|
|
|
original_path: Place<'tcx>,
|
2018-06-27 22:06:54 +01:00
|
|
|
span: Span,
|
|
|
|
kind: IllegalMoveOriginKind<'tcx>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
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 },
|
|
|
|
} => {
|
|
|
|
let stmt_source_info = self.mir.source_info(location);
|
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(
|
|
|
|
Place::Local(local),
|
|
|
|
Rvalue::Use(Operand::Move(move_from)),
|
|
|
|
)) = 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
|
|
|
|
{
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
grouped_errors.push(GroupedMoveError::OtherIllegalMove {
|
|
|
|
span: stmt_source_info.span,
|
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) = {
|
2018-08-07 17:06:21 +02:00
|
|
|
let (span, original_path, kind): (Span, &Place<'tcx>, &IllegalMoveOriginKind) =
|
|
|
|
match error {
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromPlace {
|
2018-08-07 17:06:21 +02:00
|
|
|
span,
|
|
|
|
ref original_path,
|
|
|
|
ref kind,
|
|
|
|
..
|
|
|
|
} |
|
2018-08-07 01:02:39 -07:00
|
|
|
GroupedMoveError::MovesFromValue { span, ref original_path, ref kind, .. } |
|
2018-08-07 17:06:21 +02:00
|
|
|
GroupedMoveError::OtherIllegalMove { span, ref original_path, ref kind } => {
|
|
|
|
(span, original_path, kind)
|
|
|
|
},
|
|
|
|
};
|
2018-06-27 22:06:54 +01:00
|
|
|
let origin = Origin::Mir;
|
2018-08-07 17:06:21 +02:00
|
|
|
debug!("report: original_path={:?} span={:?}, kind={:?} \
|
|
|
|
original_path.is_upvar_field_projection={:?}", original_path, span, kind,
|
|
|
|
original_path.is_upvar_field_projection(self.mir, &self.tcx));
|
2018-06-27 22:06:54 +01:00
|
|
|
(
|
|
|
|
match kind {
|
|
|
|
IllegalMoveOriginKind::Static => {
|
|
|
|
self.tcx.cannot_move_out_of(span, "static item", origin)
|
|
|
|
}
|
2018-07-13 23:39:10 +01:00
|
|
|
IllegalMoveOriginKind::BorrowedContent { target_place: place } => {
|
2018-06-27 22:06:54 +01:00
|
|
|
// Inspect the type of the content behind the
|
|
|
|
// borrow to provide feedback about why this
|
|
|
|
// was a move rather than a copy.
|
2018-07-13 23:39:10 +01:00
|
|
|
let ty = place.ty(self.mir, self.tcx).to_ty(self.tcx);
|
2018-08-07 19:50:19 +02:00
|
|
|
let is_upvar_field_projection =
|
|
|
|
self.prefixes(&original_path, PrefixSet::All)
|
|
|
|
.any(|p| p.is_upvar_field_projection(self.mir, &self.tcx)
|
|
|
|
.is_some());
|
2018-06-27 22:06:54 +01:00
|
|
|
match ty.sty {
|
|
|
|
ty::TyArray(..) | ty::TySlice(..) => self
|
|
|
|
.tcx
|
|
|
|
.cannot_move_out_of_interior_noncopy(span, ty, None, origin),
|
2018-07-13 23:39:10 +01:00
|
|
|
ty::TyClosure(def_id, closure_substs)
|
2018-08-07 19:50:19 +02:00
|
|
|
if !self.mir.upvar_decls.is_empty() && is_upvar_field_projection
|
2018-08-07 17:06:21 +02:00
|
|
|
=> {
|
2018-07-13 23:39:10 +01:00
|
|
|
let closure_kind_ty =
|
|
|
|
closure_substs.closure_kind_ty(def_id, self.tcx);
|
|
|
|
let closure_kind = closure_kind_ty.to_opt_closure_kind();
|
|
|
|
let place_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"),
|
|
|
|
};
|
2018-08-07 15:37:32 +02:00
|
|
|
debug!("report: closure_kind_ty={:?} closure_kind={:?} \
|
|
|
|
place_description={:?}", closure_kind_ty, closure_kind,
|
|
|
|
place_description);
|
|
|
|
|
2018-08-07 17:06:21 +02:00
|
|
|
let mut diag = self.tcx.cannot_move_out_of(
|
|
|
|
span, place_description, origin);
|
|
|
|
|
2018-08-07 19:50:19 +02:00
|
|
|
for prefix in self.prefixes(&original_path, PrefixSet::All) {
|
|
|
|
if let Some(field) = prefix.is_upvar_field_projection(
|
|
|
|
self.mir, &self.tcx) {
|
|
|
|
let upvar_decl = &self.mir.upvar_decls[field.index()];
|
|
|
|
let upvar_hir_id =
|
|
|
|
upvar_decl.var_hir_id.assert_crate_local();
|
|
|
|
let upvar_node_id =
|
|
|
|
self.tcx.hir.hir_to_node_id(upvar_hir_id);
|
|
|
|
let upvar_span = self.tcx.hir.span(upvar_node_id);
|
|
|
|
diag.span_label(upvar_span, "captured outer variable");
|
|
|
|
break;
|
|
|
|
}
|
2018-08-07 17:06:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
diag
|
2018-07-13 23:39:10 +01:00
|
|
|
}
|
2018-06-27 22:06:54 +01:00
|
|
|
_ => self
|
|
|
|
.tcx
|
|
|
|
.cannot_move_out_of(span, "borrowed content", origin),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
|
|
|
|
self.tcx
|
|
|
|
.cannot_move_out_of_interior_of_drop(span, ty, origin)
|
|
|
|
}
|
|
|
|
IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => self
|
|
|
|
.tcx
|
|
|
|
.cannot_move_out_of_interior_noncopy(span, ty, Some(*is_index), origin),
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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-08-07 01:02:39 -07:00
|
|
|
let snippet = self.tcx.sess.codemap().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 {
|
2018-08-07 01:02:39 -07:00
|
|
|
Place::Projection(box PlaceProjection {
|
|
|
|
elem: ProjectionElem::Deref,
|
|
|
|
..
|
2018-08-07 23:44:35 -07:00
|
|
|
}) => true,
|
|
|
|
_ => false,
|
|
|
|
};
|
|
|
|
if try_remove_deref && snippet.starts_with('*') {
|
|
|
|
// This is false for (e.g.) index expressions `a[b]`,
|
|
|
|
// which roughly desugar to `*Index::index(&a, b)` or
|
|
|
|
// `*IndexMut::index_mut(&mut a, b)`.
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"consider removing this dereference operator",
|
|
|
|
snippet[1..].to_owned(),
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
err.span_suggestion(
|
|
|
|
span,
|
|
|
|
"consider using a reference instead",
|
|
|
|
format!("&{}", snippet),
|
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 23:44:35 -07:00
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
binds_to.sort();
|
|
|
|
binds_to.dedup();
|
|
|
|
self.add_move_error_labels(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);
|
|
|
|
self.add_move_error_labels(err, &binds_to);
|
|
|
|
}
|
|
|
|
// No binding. Nothing to suggest.
|
|
|
|
GroupedMoveError::OtherIllegalMove { .. } => (),
|
|
|
|
}
|
|
|
|
}
|
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],
|
|
|
|
) {
|
|
|
|
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 {
|
|
|
|
let pat_snippet = self
|
|
|
|
.tcx.sess.codemap()
|
|
|
|
.span_to_snippet(pat_span)
|
|
|
|
.unwrap();
|
|
|
|
if pat_snippet.starts_with('&') {
|
2018-08-07 16:16:37 -07:00
|
|
|
let pat_snippet = pat_snippet[1..].trim_left();
|
2018-08-07 01:02:39 -07:00
|
|
|
let suggestion;
|
|
|
|
if pat_snippet.starts_with("mut")
|
|
|
|
&& pat_snippet["mut".len()..].starts_with(Pattern_White_Space)
|
|
|
|
{
|
|
|
|
suggestion = pat_snippet["mut".len()..].trim_left();
|
2018-07-31 17:22:12 +02:00
|
|
|
} else {
|
2018-08-07 01:02:39 -07:00
|
|
|
suggestion = pat_snippet;
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
err.span_suggestion(
|
|
|
|
pat_span,
|
|
|
|
"consider removing this borrow operator",
|
|
|
|
suggestion.to_owned(),
|
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-07 01:02:39 -07:00
|
|
|
fn add_move_error_labels(
|
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'a>,
|
|
|
|
binds_to: &[Local],
|
|
|
|
) {
|
|
|
|
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 {
|
|
|
|
err.span_label(binding_span, format!("data moved here"));
|
|
|
|
} else {
|
|
|
|
err.span_label(binding_span, format!("... and here"));
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
2018-08-07 01:02:39 -07:00
|
|
|
|
|
|
|
err.span_note(
|
|
|
|
binding_span,
|
|
|
|
&format!(
|
2018-08-12 13:14:03 -07:00
|
|
|
"move occurs because `{}` has type `{}`, \
|
2018-08-07 01:02:39 -07:00
|
|
|
which does not implement the `Copy` trait",
|
|
|
|
bind_to.name.unwrap(),
|
|
|
|
bind_to.ty
|
|
|
|
),
|
|
|
|
);
|
2018-06-27 22:06:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|