1 use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder, ErrorGuaranteed};
2 use rustc_middle::mir::*;
4 use rustc_mir_dataflow::move_paths::{
5 IllegalMoveOrigin, IllegalMoveOriginKind, LookupResult, MoveError, MovePathIndex,
7 use rustc_span::{sym, Span};
9 use crate::diagnostics::UseSpans;
10 use crate::prefixes::PrefixSet;
11 use crate::MirBorrowckCtxt;
13 // Often when desugaring a pattern match we may have many individual moves in
14 // MIR that are all part of one operation from the user's point-of-view. For
19 // would move x from the 0 field of some temporary, and y from the 1 field. We
20 // group such errors together for cleaner error reporting.
22 // Errors are kept separate if they are from places with different parent move
23 // paths. For example, this generates two errors:
25 // let (&x, &y) = (&String::new(), &String::new());
27 enum GroupedMoveError<'tcx> {
28 // Place expression can't be moved from,
29 // e.g., match x[0] { s => (), } where x: &[String]
31 original_path: Place<'tcx>,
33 move_from: Place<'tcx>,
34 kind: IllegalMoveOriginKind<'tcx>,
37 // Part of a value expression can't be moved from,
38 // e.g., match &String::new() { &x => (), }
40 original_path: Place<'tcx>,
42 move_from: MovePathIndex,
43 kind: IllegalMoveOriginKind<'tcx>,
46 // Everything that isn't from pattern matching.
48 original_path: Place<'tcx>,
49 use_spans: UseSpans<'tcx>,
50 kind: IllegalMoveOriginKind<'tcx>,
54 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
55 pub(crate) fn report_move_errors(&mut self, move_errors: Vec<(Place<'tcx>, MoveError<'tcx>)>) {
56 let grouped_errors = self.group_move_errors(move_errors);
57 for error in grouped_errors {
64 errors: Vec<(Place<'tcx>, MoveError<'tcx>)>,
65 ) -> Vec<GroupedMoveError<'tcx>> {
66 let mut grouped_errors = Vec::new();
67 for (original_path, error) in errors {
68 self.append_to_grouped_errors(&mut grouped_errors, original_path, error);
73 fn append_to_grouped_errors(
75 grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
76 original_path: Place<'tcx>,
77 error: MoveError<'tcx>,
80 MoveError::UnionMove { .. } => {
81 unimplemented!("don't know how to report union move errors yet.")
83 MoveError::IllegalMove { cannot_move_out_of: IllegalMoveOrigin { location, kind } } => {
84 // Note: that the only time we assign a place isn't a temporary
85 // to a user variable is when initializing it.
86 // If that ever stops being the case, then the ever initialized
87 // flow could be used.
88 if let Some(StatementKind::Assign(box (
90 Rvalue::Use(Operand::Move(move_from)),
91 ))) = self.body.basic_blocks()[location.block]
93 .get(location.statement_index)
94 .map(|stmt| &stmt.kind)
96 if let Some(local) = place.as_local() {
97 let local_decl = &self.body.local_decls[local];
98 // opt_match_place is the
99 // match_span is the span of the expression being matched on
100 // match *x.y { ... } match_place is Some(*x.y)
101 // ^^^^ match_span is the span of *x.y
103 // opt_match_place is None for let [mut] x = ... statements,
104 // whether or not the right-hand side is a place expression
105 if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
107 opt_match_place: Some((opt_match_place, match_span)),
112 )))) = local_decl.local_info
114 let stmt_source_info = self.body.source_info(location);
115 self.append_binding_error(
123 stmt_source_info.span,
130 let move_spans = self.move_spans(original_path.as_ref(), location);
131 grouped_errors.push(GroupedMoveError::OtherIllegalMove {
132 use_spans: move_spans,
140 fn append_binding_error(
142 grouped_errors: &mut Vec<GroupedMoveError<'tcx>>,
143 kind: IllegalMoveOriginKind<'tcx>,
144 original_path: Place<'tcx>,
145 move_from: Place<'tcx>,
147 match_place: Option<Place<'tcx>>,
149 statement_span: Span,
151 debug!("append_binding_error(match_place={:?}, match_span={:?})", match_place, match_span);
153 let from_simple_let = match_place.is_none();
154 let match_place = match_place.unwrap_or(move_from);
156 match self.move_data.rev_lookup.find(match_place.as_ref()) {
157 // Error with the match place
158 LookupResult::Parent(_) => {
159 for ge in &mut *grouped_errors {
160 if let GroupedMoveError::MovesFromPlace { span, binds_to, .. } = ge {
161 if match_span == *span {
162 debug!("appending local({:?}) to list", bind_to);
163 if !binds_to.is_empty() {
164 binds_to.push(bind_to);
170 debug!("found a new move error location");
172 // Don't need to point to x in let x = ... .
173 let (binds_to, span) = if from_simple_let {
174 (vec![], statement_span)
176 (vec![bind_to], match_span)
178 grouped_errors.push(GroupedMoveError::MovesFromPlace {
186 // Error with the pattern
187 LookupResult::Exact(_) => {
188 let LookupResult::Parent(Some(mpi)) = self.move_data.rev_lookup.find(move_from.as_ref()) else {
189 // move_from should be a projection from match_place.
190 unreachable!("Probably not unreachable...");
192 for ge in &mut *grouped_errors {
193 if let GroupedMoveError::MovesFromValue {
195 move_from: other_mpi,
200 if match_span == *span && mpi == *other_mpi {
201 debug!("appending local({:?}) to list", bind_to);
202 binds_to.push(bind_to);
207 debug!("found a new move error location");
208 grouped_errors.push(GroupedMoveError::MovesFromValue {
213 binds_to: vec![bind_to],
219 fn report(&mut self, error: GroupedMoveError<'tcx>) {
220 let (mut err, err_span) = {
221 let (span, use_spans, original_path, kind): (
223 Option<UseSpans<'tcx>>,
225 &IllegalMoveOriginKind<'_>,
227 GroupedMoveError::MovesFromPlace { span, original_path, ref kind, .. }
228 | GroupedMoveError::MovesFromValue { span, original_path, ref kind, .. } => {
229 (span, None, original_path, kind)
231 GroupedMoveError::OtherIllegalMove { use_spans, original_path, ref kind } => {
232 (use_spans.args_or_use(), Some(use_spans), original_path, kind)
236 "report: original_path={:?} span={:?}, kind={:?} \
237 original_path.is_upvar_field_projection={:?}",
241 self.is_upvar_field_projection(original_path.as_ref())
245 &IllegalMoveOriginKind::BorrowedContent { target_place } => self
246 .report_cannot_move_from_borrowed_content(
252 &IllegalMoveOriginKind::InteriorOfTypeWithDestructor { container_ty: ty } => {
253 self.cannot_move_out_of_interior_of_drop(span, ty)
255 &IllegalMoveOriginKind::InteriorOfSliceOrArray { ty, is_index } => {
256 self.cannot_move_out_of_interior_noncopy(span, ty, Some(is_index))
263 self.add_move_hints(error, &mut err, err_span);
264 self.buffer_error(err);
267 fn report_cannot_move_from_static(
271 ) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
272 let description = if place.projection.len() == 1 {
273 format!("static item {}", self.describe_any_place(place.as_ref()))
275 let base_static = PlaceRef { local: place.local, projection: &[ProjectionElem::Deref] };
278 "{} as {} is a static item",
279 self.describe_any_place(place.as_ref()),
280 self.describe_any_place(base_static),
284 self.cannot_move_out_of(span, &description)
287 fn report_cannot_move_from_borrowed_content(
289 move_place: Place<'tcx>,
290 deref_target_place: Place<'tcx>,
292 use_spans: Option<UseSpans<'tcx>>,
293 ) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
294 // Inspect the type of the content behind the
295 // borrow to provide feedback about why this
296 // was a move rather than a copy.
297 let ty = deref_target_place.ty(self.body, self.infcx.tcx).ty;
298 let upvar_field = self
299 .prefixes(move_place.as_ref(), PrefixSet::All)
300 .find_map(|p| self.is_upvar_field_projection(p));
302 let deref_base = match deref_target_place.projection.as_ref() {
303 [proj_base @ .., ProjectionElem::Deref] => {
304 PlaceRef { local: deref_target_place.local, projection: &proj_base }
306 _ => bug!("deref_target_place is not a deref projection"),
309 if let PlaceRef { local, projection: [] } = deref_base {
310 let decl = &self.body.local_decls[local];
311 if decl.is_ref_for_guard() {
312 let mut err = self.cannot_move_out_of(
314 &format!("`{}` in pattern guard", self.local_names[local].unwrap()),
317 "variables bound in patterns cannot be moved from \
318 until after the end of the pattern guard",
321 } else if decl.is_ref_to_static() {
322 return self.report_cannot_move_from_static(move_place, span);
326 debug!("report: ty={:?}", ty);
327 let mut err = match ty.kind() {
328 ty::Array(..) | ty::Slice(..) => {
329 self.cannot_move_out_of_interior_noncopy(span, ty, None)
331 ty::Closure(def_id, closure_substs)
332 if def_id.as_local() == Some(self.mir_def_id()) && upvar_field.is_some() =>
334 let closure_kind_ty = closure_substs.as_closure().kind_ty();
335 let closure_kind = match closure_kind_ty.to_opt_closure_kind() {
336 Some(kind @ (ty::ClosureKind::Fn | ty::ClosureKind::FnMut)) => kind,
337 Some(ty::ClosureKind::FnOnce) => {
338 bug!("closure kind does not match first argument type")
340 None => bug!("closure kind not inferred by borrowck"),
342 let capture_description =
343 format!("captured variable in an `{}` closure", closure_kind);
345 let upvar = &self.upvars[upvar_field.unwrap().index()];
346 let upvar_hir_id = upvar.place.get_root_variable();
347 let upvar_name = upvar.place.to_string(self.infcx.tcx);
348 let upvar_span = self.infcx.tcx.hir().span(upvar_hir_id);
350 let place_name = self.describe_any_place(move_place.as_ref());
352 let place_description =
353 if self.is_upvar_field_projection(move_place.as_ref()).is_some() {
354 format!("{}, a {}", place_name, capture_description)
356 format!("{}, as `{}` is a {}", place_name, upvar_name, capture_description)
360 "report: closure_kind_ty={:?} closure_kind={:?} place_description={:?}",
361 closure_kind_ty, closure_kind, place_description,
364 let mut diag = self.cannot_move_out_of(span, &place_description);
366 diag.span_label(upvar_span, "captured outer variable");
369 format!("captured by this `{}` closure", closure_kind),
375 let source = self.borrowed_content_source(deref_base);
376 match (self.describe_place(move_place.as_ref()), source.describe_for_named_place())
378 (Some(place_desc), Some(source_desc)) => self.cannot_move_out_of(
380 &format!("`{}` which is behind a {}", place_desc, source_desc),
382 (_, _) => self.cannot_move_out_of(
384 &source.describe_for_unnamed_place(self.infcx.tcx),
389 let ty = move_place.ty(self.body, self.infcx.tcx).ty;
390 let def_id = match *ty.kind() {
391 ty::Adt(self_def, _) => self_def.did,
393 | ty::FnDef(def_id, _)
394 | ty::Closure(def_id, _)
395 | ty::Generator(def_id, ..)
396 | ty::Opaque(def_id, _) => def_id,
399 let diag_name = self.infcx.tcx.get_diagnostic_name(def_id);
400 if matches!(diag_name, Some(sym::Option | sym::Result))
401 && use_spans.map_or(true, |v| !v.for_closure())
403 err.span_suggestion_verbose(
405 &format!("consider borrowing the `{}`'s content", diag_name.unwrap()),
406 ".as_ref()".to_string(),
407 Applicability::MaybeIncorrect,
409 } else if let Some(use_spans) = use_spans {
410 self.explain_captures(
411 &mut err, span, span, use_spans, move_place, None, "", "", "", false, true,
417 fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diagnostic, span: Span) {
419 GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
420 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
423 "consider borrowing here",
424 format!("&{}", snippet),
425 Applicability::Unspecified,
429 if binds_to.is_empty() {
430 let place_ty = move_from.ty(self.body, self.infcx.tcx).ty;
431 let place_desc = match self.describe_place(move_from.as_ref()) {
432 Some(desc) => format!("`{}`", desc),
433 None => "value".to_string(),
436 self.note_type_does_not_implement_copy(
447 self.add_move_error_details(err, &binds_to);
450 GroupedMoveError::MovesFromValue { mut binds_to, .. } => {
453 self.add_move_error_suggestions(err, &binds_to);
454 self.add_move_error_details(err, &binds_to);
456 // No binding. Nothing to suggest.
457 GroupedMoveError::OtherIllegalMove { ref original_path, use_spans, .. } => {
458 let span = use_spans.var_or_use();
459 let place_ty = original_path.ty(self.body, self.infcx.tcx).ty;
460 let place_desc = match self.describe_place(original_path.as_ref()) {
461 Some(desc) => format!("`{}`", desc),
462 None => "value".to_string(),
464 self.note_type_does_not_implement_copy(err, &place_desc, place_ty, Some(span), "");
466 use_spans.args_span_label(err, format!("move out of {} occurs here", place_desc));
471 fn add_move_error_suggestions(&self, err: &mut Diagnostic, binds_to: &[Local]) {
472 let mut suggestions: Vec<(Span, &str, String)> = Vec::new();
473 for local in binds_to {
474 let bind_to = &self.body.local_decls[*local];
475 if let Some(box LocalInfo::User(ClearCrossCrate::Set(BindingForm::Var(
476 VarBindingForm { pat_span, .. },
477 )))) = bind_to.local_info
479 if let Ok(pat_snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(pat_span)
481 if let Some(stripped) = pat_snippet.strip_prefix('&') {
482 let pat_snippet = stripped.trim_start();
483 let (suggestion, to_remove) = if pat_snippet.starts_with("mut")
484 && pat_snippet["mut".len()..].starts_with(rustc_lexer::is_whitespace)
486 (pat_snippet["mut".len()..].trim_start(), "&mut")
490 suggestions.push((pat_span, to_remove, suggestion.to_owned()));
495 suggestions.sort_unstable_by_key(|&(span, _, _)| span);
496 suggestions.dedup_by_key(|&mut (span, _, _)| span);
497 for (span, to_remove, suggestion) in suggestions {
500 &format!("consider removing the `{}`", to_remove),
502 Applicability::MachineApplicable,
507 fn add_move_error_details(&self, err: &mut Diagnostic, binds_to: &[Local]) {
508 for (j, local) in binds_to.iter().enumerate() {
509 let bind_to = &self.body.local_decls[*local];
510 let binding_span = bind_to.source_info.span;
513 err.span_label(binding_span, "data moved here");
515 err.span_label(binding_span, "...and here");
518 if binds_to.len() == 1 {
519 self.note_type_does_not_implement_copy(
521 &format!("`{}`", self.local_names[*local].unwrap()),
529 if binds_to.len() > 1 {
531 "move occurs because these variables have types that \
532 don't implement the `Copy` trait",