Use UseSpans in cannot move errors

This commit is contained in:
Matthew Jasper 2019-05-05 12:02:17 +01:00
parent f7e86a5a49
commit 9336b3d3d0

View file

@ -7,6 +7,7 @@ use syntax_pos::Span;
use crate::borrow_check::MirBorrowckCtxt; use crate::borrow_check::MirBorrowckCtxt;
use crate::borrow_check::prefixes::PrefixSet; use crate::borrow_check::prefixes::PrefixSet;
use crate::borrow_check::error_reporting::UseSpans;
use crate::dataflow::move_paths::{ use crate::dataflow::move_paths::{
IllegalMoveOrigin, IllegalMoveOriginKind, InitLocation, IllegalMoveOrigin, IllegalMoveOriginKind, InitLocation,
LookupResult, MoveError, MovePathIndex, LookupResult, MoveError, MovePathIndex,
@ -49,7 +50,7 @@ enum GroupedMoveError<'tcx> {
// Everything that isn't from pattern matching. // Everything that isn't from pattern matching.
OtherIllegalMove { OtherIllegalMove {
original_path: Place<'tcx>, original_path: Place<'tcx>,
span: Span, use_spans: UseSpans,
kind: IllegalMoveOriginKind<'tcx>, kind: IllegalMoveOriginKind<'tcx>,
}, },
} }
@ -150,7 +151,6 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
MoveError::IllegalMove { MoveError::IllegalMove {
cannot_move_out_of: IllegalMoveOrigin { location, kind }, cannot_move_out_of: IllegalMoveOrigin { location, kind },
} => { } => {
let stmt_source_info = self.mir.source_info(location);
// Note: that the only time we assign a place isn't a temporary // Note: that the only time we assign a place isn't a temporary
// to a user variable is when initializing it. // to a user variable is when initializing it.
// If that ever stops being the case, then the ever initialized // If that ever stops being the case, then the ever initialized
@ -178,6 +178,7 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
pat_span: _, pat_span: _,
}))) = local_decl.is_user_variable }))) = local_decl.is_user_variable
{ {
let stmt_source_info = self.mir.source_info(location);
self.append_binding_error( self.append_binding_error(
grouped_errors, grouped_errors,
kind, kind,
@ -191,8 +192,10 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
return; return;
} }
} }
let move_spans = self.move_spans(&original_path, location);
grouped_errors.push(GroupedMoveError::OtherIllegalMove { grouped_errors.push(GroupedMoveError::OtherIllegalMove {
span: stmt_source_info.span, use_spans: move_spans,
original_path, original_path,
kind, kind,
}); });
@ -288,9 +291,15 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
let (span, original_path, kind): (Span, &Place<'tcx>, &IllegalMoveOriginKind<'_>) = let (span, original_path, kind): (Span, &Place<'tcx>, &IllegalMoveOriginKind<'_>) =
match error { match error {
GroupedMoveError::MovesFromPlace { span, ref original_path, ref kind, .. } | GroupedMoveError::MovesFromPlace { span, ref original_path, ref kind, .. } |
GroupedMoveError::MovesFromValue { span, ref original_path, ref kind, .. } | GroupedMoveError::MovesFromValue { span, ref original_path, ref kind, .. } => {
GroupedMoveError::OtherIllegalMove { span, ref original_path, ref kind } => {
(span, original_path, kind) (span, original_path, kind)
}
GroupedMoveError::OtherIllegalMove {
use_spans,
ref original_path,
ref kind
} => {
(use_spans.args_or_use(), original_path, kind)
}, },
}; };
debug!("report: original_path={:?} span={:?}, kind={:?} \ debug!("report: original_path={:?} span={:?}, kind={:?} \
@ -548,7 +557,8 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
self.add_move_error_details(err, &binds_to); self.add_move_error_details(err, &binds_to);
} }
// No binding. Nothing to suggest. // No binding. Nothing to suggest.
GroupedMoveError::OtherIllegalMove { ref original_path, span, .. } => { GroupedMoveError::OtherIllegalMove { ref original_path, use_spans, .. } => {
let span = use_spans.var_or_use();
let place_ty = original_path.ty(self.mir, self.infcx.tcx).ty; let place_ty = original_path.ty(self.mir, self.infcx.tcx).ty;
let place_desc = match self.describe_place(original_path) { let place_desc = match self.describe_place(original_path) {
Some(desc) => format!("`{}`", desc), Some(desc) => format!("`{}`", desc),
@ -560,6 +570,12 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
place_ty, place_ty,
Some(span), Some(span),
); );
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()),
);
}, },
} }
} }