Skip to content

Commit 021a503

Browse files
committed
Auto merge of #61136 - matthewjasper:cannot-move-errors, r=pnkfelix
Make cannot move errors more consistent with other borrowck errors * Note the type of the place being moved in all cases. * Note the place being moved from. * Simplify the search for overloaded place operators * Extend the note for move from overloaded deref apply to all types. * Add a note for moves from overloaded index. * Special case moves for closure captures. r? @pnkfelix
2 parents c22ce28 + 8ffa408 commit 021a503

File tree

107 files changed

+1116
-1516
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+1116
-1516
lines changed

src/librustc/mir/mod.rs

+10
Original file line numberDiff line numberDiff line change
@@ -913,6 +913,16 @@ impl<'tcx> LocalDecl<'tcx> {
913913
}
914914
}
915915

916+
/// Returns `true` if this is a reference to a variable bound in a `match`
917+
/// expression that is used to access said variable for the guard of the
918+
/// match arm.
919+
pub fn is_ref_for_guard(&self) -> bool {
920+
match self.is_user_variable {
921+
Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) => true,
922+
_ => false,
923+
}
924+
}
925+
916926
/// Returns `true` is the local is from a compiler desugaring, e.g.,
917927
/// `__next` from a `for` loop.
918928
#[inline]

src/librustc_mir/borrow_check/conflict_errors.rs

+10-14
Original file line numberDiff line numberDiff line change
@@ -234,22 +234,18 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
234234
);
235235
}
236236
}
237-
if let Place::Base(PlaceBase::Local(local)) = place {
237+
let span = if let Place::Base(PlaceBase::Local(local)) = place {
238238
let decl = &self.mir.local_decls[*local];
239-
err.span_label(
240-
decl.source_info.span,
241-
format!(
242-
"move occurs because {} has type `{}`, \
243-
which does not implement the `Copy` trait",
244-
note_msg, ty,
245-
));
239+
Some(decl.source_info.span)
246240
} else {
247-
err.note(&format!(
248-
"move occurs because {} has type `{}`, \
249-
which does not implement the `Copy` trait",
250-
note_msg, ty
251-
));
252-
}
241+
None
242+
};
243+
self.note_type_does_not_implement_copy(
244+
&mut err,
245+
&note_msg,
246+
ty,
247+
span,
248+
);
253249
}
254250

255251
if let Some((_, mut old_err)) = self.move_error_reported

src/librustc_mir/borrow_check/error_reporting.rs

+24-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ use rustc::hir;
22
use rustc::hir::def::Namespace;
33
use rustc::hir::def_id::DefId;
44
use rustc::mir::{
5-
AggregateKind, BindingForm, ClearCrossCrate, Constant, Field, Local,
6-
LocalKind, Location, Operand, Place, PlaceBase, ProjectionElem, Rvalue,
7-
Statement, StatementKind, Static, StaticKind, TerminatorKind,
5+
AggregateKind, Constant, Field, Local, LocalKind, Location, Operand,
6+
Place, PlaceBase, ProjectionElem, Rvalue, Statement, StatementKind, Static,
7+
StaticKind, TerminatorKind,
88
};
99
use rustc::ty::{self, DefIdTree, Ty};
1010
use rustc::ty::layout::VariantIdx;
@@ -180,9 +180,7 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
180180
&including_downcast,
181181
)?;
182182
} else if let Place::Base(PlaceBase::Local(local)) = proj.base {
183-
if let Some(ClearCrossCrate::Set(BindingForm::RefForGuard)) =
184-
self.mir.local_decls[local].is_user_variable
185-
{
183+
if self.mir.local_decls[local].is_ref_for_guard() {
186184
self.append_place_to_string(
187185
&proj.base,
188186
buf,
@@ -383,6 +381,26 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
383381
false
384382
}
385383
}
384+
385+
/// Add a note that a type does not implement `Copy`
386+
pub(super) fn note_type_does_not_implement_copy(
387+
&self,
388+
err: &mut DiagnosticBuilder<'a>,
389+
place_desc: &str,
390+
ty: Ty<'tcx>,
391+
span: Option<Span>,
392+
) {
393+
let message = format!(
394+
"move occurs because {} has type `{}`, which does not implement the `Copy` trait",
395+
place_desc,
396+
ty,
397+
);
398+
if let Some(span) = span {
399+
err.span_label(span, message);
400+
} else {
401+
err.note(&message);
402+
}
403+
}
386404
}
387405

388406
impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {

0 commit comments

Comments
 (0)