Skip to content

Commit f309f91

Browse files
committed
Add method to note types don't implement Copy
1 parent 5cfa70f commit f309f91

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

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

+23-3
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;
@@ -381,6 +381,26 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
381381
false
382382
}
383383
}
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+
}
384404
}
385405

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

src/librustc_mir/borrow_check/move_errors.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -473,14 +473,11 @@ impl<'a, 'gcx, 'tcx> MirBorrowckCtxt<'a, 'gcx, 'tcx> {
473473
}
474474

475475
if binds_to.len() == 1 {
476-
err.span_note(
477-
binding_span,
478-
&format!(
479-
"move occurs because `{}` has type `{}`, \
480-
which does not implement the `Copy` trait",
481-
bind_to.name.unwrap(),
482-
bind_to.ty
483-
),
476+
self.note_type_does_not_implement_copy(
477+
err,
478+
&format!("`{}`", bind_to.name.unwrap()),
479+
bind_to.ty,
480+
Some(binding_span)
484481
);
485482
} else {
486483
noncopy_var_spans.push(binding_span);

0 commit comments

Comments
 (0)