1 use rustc::mir::{self, ClearCrossCrate, Local, LocalInfo, Location, ReadOnlyBodyAndCache};
2 use rustc::mir::{Mutability, Place, PlaceBase, PlaceRef, ProjectionElem};
3 use rustc::ty::{self, Ty, TyCtxt};
6 use rustc_index::vec::Idx;
7 use rustc_span::symbol::kw;
10 use crate::borrow_check::diagnostics::BorrowedContentSource;
11 use crate::borrow_check::MirBorrowckCtxt;
12 use crate::util::collect_writes::FindAssignments;
13 use rustc_errors::Applicability;
15 #[derive(Copy, Clone, Debug, Eq, PartialEq)]
16 pub(crate) enum AccessKind {
21 impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
22 pub(crate) fn report_mutability_error(
24 access_place: &Place<'tcx>,
26 the_place_err: PlaceRef<'cx, 'tcx>,
27 error_access: AccessKind,
31 "report_mutability_error(\
32 access_place={:?}, span={:?}, the_place_err={:?}, error_access={:?}, location={:?},\
34 access_place, span, the_place_err, error_access, location,
40 let mut opt_source = None;
41 let access_place_desc = self.describe_place(access_place.as_ref());
42 debug!("report_mutability_error: access_place_desc={:?}", access_place_desc);
45 PlaceRef { base: PlaceBase::Local(local), projection: [] } => {
46 item_msg = format!("`{}`", access_place_desc.unwrap());
47 if access_place.as_local().is_some() {
48 reason = ", as it is not declared as mutable".to_string();
50 let name = self.local_names[*local].expect("immutable unnamed local");
51 reason = format!(", as `{}` is not declared as mutable", name);
57 projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
59 debug_assert!(is_closure_or_generator(
60 Place::ty_from(&the_place_err.base, proj_base, *self.body, self.infcx.tcx).ty
63 item_msg = format!("`{}`", access_place_desc.unwrap());
64 if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
65 reason = ", as it is not declared as mutable".to_string();
67 let name = self.upvars[upvar_index.index()].name;
68 reason = format!(", as `{}` is not declared as mutable", name);
72 PlaceRef { base: &PlaceBase::Local(local), projection: [ProjectionElem::Deref] }
73 if self.body.local_decls[local].is_ref_for_guard() =>
75 item_msg = format!("`{}`", access_place_desc.unwrap());
76 reason = ", as it is immutable for the pattern guard".to_string();
78 PlaceRef { base: &PlaceBase::Local(local), projection: [ProjectionElem::Deref] }
79 if self.body.local_decls[local].is_ref_to_static() =>
81 if access_place.projection.len() == 1 {
82 item_msg = format!("immutable static item `{}`", access_place_desc.unwrap());
83 reason = String::new();
85 item_msg = format!("`{}`", access_place_desc.unwrap());
86 let local_info = &self.body.local_decls[local].local_info;
87 if let LocalInfo::StaticRef { def_id, .. } = *local_info {
88 let static_name = &self.infcx.tcx.item_name(def_id);
89 reason = format!(", as `{}` is an immutable static item", static_name);
91 bug!("is_ref_to_static return true, but not ref to static?");
95 PlaceRef { base: _, projection: [proj_base @ .., ProjectionElem::Deref] } => {
96 if the_place_err.base == &PlaceBase::Local(Local::new(1))
97 && proj_base.is_empty()
98 && !self.upvars.is_empty()
100 item_msg = format!("`{}`", access_place_desc.unwrap());
101 debug_assert!(self.body.local_decls[Local::new(1)].ty.is_region_ptr());
102 debug_assert!(is_closure_or_generator(
105 the_place_err.projection,
112 reason = if self.is_upvar_field_projection(access_place.as_ref()).is_some() {
113 ", as it is a captured variable in a `Fn` closure".to_string()
115 ", as `Fn` closures cannot mutate their captured variables".to_string()
118 let source = self.borrowed_content_source(PlaceRef {
119 base: the_place_err.base,
120 projection: proj_base,
122 let pointer_type = source.describe_for_immutable_place();
123 opt_source = Some(source);
124 if let Some(desc) = access_place_desc {
125 item_msg = format!("`{}`", desc);
126 reason = match error_access {
127 AccessKind::Mutate => format!(" which is behind {}", pointer_type),
128 AccessKind::MutableBorrow => {
129 format!(", as it is behind {}", pointer_type)
133 item_msg = format!("data in {}", pointer_type);
134 reason = String::new();
139 PlaceRef { base: _, projection: [.., ProjectionElem::Index(_)] }
140 | PlaceRef { base: _, projection: [.., ProjectionElem::ConstantIndex { .. }] }
141 | PlaceRef { base: _, projection: [.., ProjectionElem::Subslice { .. }] }
142 | PlaceRef { base: _, projection: [.., ProjectionElem::Downcast(..)] } => {
143 bug!("Unexpected immutable place.")
147 debug!("report_mutability_error: item_msg={:?}, reason={:?}", item_msg, reason);
149 // `act` and `acted_on` are strings that let us abstract over
150 // the verbs used in some diagnostic messages.
154 let span = match error_access {
155 AccessKind::Mutate => {
156 err = self.cannot_assign(span, &(item_msg + &reason));
158 acted_on = "written";
161 AccessKind::MutableBorrow => {
162 act = "borrow as mutable";
163 acted_on = "borrowed as mutable";
165 let borrow_spans = self.borrow_spans(span, location);
166 let borrow_span = borrow_spans.args_or_use();
167 err = self.cannot_borrow_path_as_mutable_because(borrow_span, &item_msg, &reason);
168 borrow_spans.var_span_label(
171 "mutable borrow occurs due to use of `{}` in closure",
172 // always Some() if the message is printed.
173 self.describe_place(access_place.as_ref()).unwrap_or_default(),
180 debug!("report_mutability_error: act={:?}, acted_on={:?}", act, acted_on);
182 match the_place_err {
183 // Suggest making an existing shared borrow in a struct definition a mutable borrow.
185 // This is applicable when we have a deref of a field access to a deref of a local -
186 // something like `*((*_1).0`. The local that we get will be a reference to the
187 // struct we've got a field access of (it must be a reference since there's a deref
188 // after the field access).
192 [proj_base @ .., ProjectionElem::Deref, ProjectionElem::Field(field, _), ProjectionElem::Deref],
194 err.span_label(span, format!("cannot {ACT}", ACT = act));
196 if let Some((span, message)) = annotate_struct_field(
198 Place::ty_from(base, proj_base, *self.body, self.infcx.tcx).ty,
203 "consider changing this to be mutable",
205 Applicability::MaybeIncorrect,
210 // Suggest removing a `&mut` from the use of a mutable reference.
211 PlaceRef { base: PlaceBase::Local(local), projection: [] }
217 if let LocalInfo::User(ClearCrossCrate::Set(
218 mir::BindingForm::ImplicitSelf(kind),
219 )) = local_decl.local_info
221 // Check if the user variable is a `&mut self` and we can therefore
222 // suggest removing the `&mut`.
224 // Deliberately fall into this case for all implicit self types,
225 // so that we don't fall in to the next case with them.
226 kind == mir::ImplicitSelfKind::MutRef
227 } else if Some(kw::SelfLower) == self.local_names[*local] {
228 // Otherwise, check if the name is the self kewyord - in which case
229 // we have an explicit self. Do the same thing in this case and check
230 // for a `self: &mut Self` to suggest removing the `&mut`.
231 if let ty::Ref(_, _, hir::Mutability::Mut) = local_decl.ty.kind {
243 err.span_label(span, format!("cannot {ACT}", ACT = act));
244 err.span_label(span, "try removing `&mut` here");
247 // We want to suggest users use `let mut` for local (user
248 // variable) mutations...
249 PlaceRef { base: PlaceBase::Local(local), projection: [] }
250 if self.body.local_decls[*local].can_be_made_mutable() =>
252 // ... but it doesn't make sense to suggest it on
253 // variables that are `ref x`, `ref mut x`, `&self`,
254 // or `&mut self` (such variables are simply not
256 let local_decl = &self.body.local_decls[*local];
257 assert_eq!(local_decl.mutability, Mutability::Not);
259 err.span_label(span, format!("cannot {ACT}", ACT = act));
261 local_decl.source_info.span,
262 "consider changing this to be mutable",
263 format!("mut {}", self.local_names[*local].unwrap()),
264 Applicability::MachineApplicable,
268 // Also suggest adding mut for upvars
271 projection: [proj_base @ .., ProjectionElem::Field(upvar_index, _)],
273 debug_assert!(is_closure_or_generator(
274 Place::ty_from(base, proj_base, *self.body, self.infcx.tcx).ty
277 err.span_label(span, format!("cannot {ACT}", ACT = act));
279 let upvar_hir_id = self.upvars[upvar_index.index()].var_hir_id;
280 if let Some(Node::Binding(pat)) = self.infcx.tcx.hir().find(upvar_hir_id) {
281 if let hir::PatKind::Binding(
282 hir::BindingAnnotation::Unannotated,
290 "consider changing this to be mutable",
291 format!("mut {}", upvar_ident.name),
292 Applicability::MachineApplicable,
298 // complete hack to approximate old AST-borrowck
299 // diagnostic: if the span starts with a mutable borrow of
300 // a local variable, then just suggest the user remove it.
301 PlaceRef { base: PlaceBase::Local(_), projection: [] }
303 if let Ok(snippet) = self.infcx.tcx.sess.source_map().span_to_snippet(span) {
304 snippet.starts_with("&mut ")
310 err.span_label(span, format!("cannot {ACT}", ACT = act));
311 err.span_label(span, "try removing `&mut` here");
314 PlaceRef { base: PlaceBase::Local(local), projection: [ProjectionElem::Deref] }
315 if self.body.local_decls[*local].is_ref_for_guard() =>
317 err.span_label(span, format!("cannot {ACT}", ACT = act));
319 "variables bound in patterns are immutable until the end of the pattern guard",
323 // We want to point out when a `&` can be readily replaced
326 // FIXME: can this case be generalized to work for an
327 // arbitrary base for the projection?
328 PlaceRef { base: PlaceBase::Local(local), projection: [ProjectionElem::Deref] }
329 if self.body.local_decls[*local].is_user_variable() =>
331 let local_decl = &self.body.local_decls[*local];
332 let suggestion = match local_decl.local_info {
333 LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::ImplicitSelf(_))) => {
334 Some(suggest_ampmut_self(self.infcx.tcx, local_decl))
337 LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
338 mir::VarBindingForm {
339 binding_mode: ty::BindingMode::BindByValue(_),
343 ))) => Some(suggest_ampmut(
351 LocalInfo::User(ClearCrossCrate::Set(mir::BindingForm::Var(
352 mir::VarBindingForm {
353 binding_mode: ty::BindingMode::BindByReference(_),
357 let pattern_span = local_decl.source_info.span;
358 suggest_ref_mut(self.infcx.tcx, pattern_span)
359 .map(|replacement| (pattern_span, replacement))
362 LocalInfo::User(ClearCrossCrate::Clear) => bug!("saw cleared local state"),
367 let (pointer_sigil, pointer_desc) = if local_decl.ty.is_region_ptr() {
370 ("*const", "pointer")
373 if let Some((err_help_span, suggested_code)) = suggestion {
376 &format!("consider changing this to be a mutable {}", pointer_desc),
378 Applicability::MachineApplicable,
382 match self.local_names[*local] {
383 Some(name) if !local_decl.from_compiler_desugaring() => {
387 "`{NAME}` is a `{SIGIL}` {DESC}, \
388 so the data it refers to cannot be {ACTED_ON}",
390 SIGIL = pointer_sigil,
400 "cannot {ACT} through `{SIGIL}` {DESC}",
402 SIGIL = pointer_sigil,
412 projection: [ProjectionElem::Deref],
413 // FIXME document what is this 1 magic number about
414 } if *base == PlaceBase::Local(Local::new(1)) && !self.upvars.is_empty() => {
415 err.span_label(span, format!("cannot {ACT}", ACT = act));
418 "consider changing this to accept closures that implement `FnMut`",
422 PlaceRef { base: _, projection: [.., ProjectionElem::Deref] } => {
423 err.span_label(span, format!("cannot {ACT}", ACT = act));
426 Some(BorrowedContentSource::OverloadedDeref(ty)) => {
428 "trait `DerefMut` is required to modify through a dereference, \
429 but it is not implemented for `{}`",
433 Some(BorrowedContentSource::OverloadedIndex(ty)) => {
435 "trait `IndexMut` is required to modify indexed content, \
436 but it is not implemented for `{}`",
445 err.span_label(span, format!("cannot {ACT}", ACT = act));
449 err.buffer(&mut self.errors_buffer);
453 fn suggest_ampmut_self<'tcx>(
455 local_decl: &mir::LocalDecl<'tcx>,
456 ) -> (Span, String) {
457 let sp = local_decl.source_info.span;
460 match tcx.sess.source_map().span_to_snippet(sp) {
462 let lt_pos = snippet.find('\'');
463 if let Some(lt_pos) = lt_pos {
464 format!("&{}mut self", &snippet[lt_pos..snippet.len() - 4])
466 "&mut self".to_string()
469 _ => "&mut self".to_string(),
474 // When we want to suggest a user change a local variable to be a `&mut`, there
475 // are three potential "obvious" things to highlight:
477 // let ident [: Type] [= RightHandSideExpression];
478 // ^^^^^ ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^
481 // We can always fallback on highlighting the first. But chances are good that
482 // the user experience will be better if we highlight one of the others if possible;
483 // for example, if the RHS is present and the Type is not, then the type is going to
484 // be inferred *from* the RHS, which means we should highlight that (and suggest
485 // that they borrow the RHS mutably).
487 // This implementation attempts to emulate AST-borrowck prioritization
488 // by trying (3.), then (2.) and finally falling back on (1.).
489 fn suggest_ampmut<'tcx>(
491 body: ReadOnlyBodyAndCache<'_, 'tcx>,
493 local_decl: &mir::LocalDecl<'tcx>,
494 opt_ty_info: Option<Span>,
495 ) -> (Span, String) {
496 let locations = body.find_assignments(local);
497 if !locations.is_empty() {
498 let assignment_rhs_span = body.source_info(locations[0]).span;
499 if let Ok(src) = tcx.sess.source_map().span_to_snippet(assignment_rhs_span) {
500 if let (true, Some(ws_pos)) =
501 (src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
503 let lt_name = &src[1..ws_pos];
504 let ty = &src[ws_pos..];
505 return (assignment_rhs_span, format!("&{} mut {}", lt_name, ty));
506 } else if src.starts_with('&') {
507 let borrowed_expr = &src[1..];
508 return (assignment_rhs_span, format!("&mut {}", borrowed_expr));
513 let highlight_span = match opt_ty_info {
514 // if this is a variable binding with an explicit type,
515 // try to highlight that for the suggestion.
516 Some(ty_span) => ty_span,
518 // otherwise, just highlight the span associated with
519 // the (MIR) LocalDecl.
520 None => local_decl.source_info.span,
523 if let Ok(src) = tcx.sess.source_map().span_to_snippet(highlight_span) {
524 if let (true, Some(ws_pos)) =
525 (src.starts_with("&'"), src.find(|c: char| -> bool { c.is_whitespace() }))
527 let lt_name = &src[1..ws_pos];
528 let ty = &src[ws_pos..];
529 return (highlight_span, format!("&{} mut{}", lt_name, ty));
533 let ty_mut = local_decl.ty.builtin_deref(true).unwrap();
534 assert_eq!(ty_mut.mutbl, hir::Mutability::Not);
537 if local_decl.ty.is_region_ptr() {
538 format!("&mut {}", ty_mut.ty)
540 format!("*mut {}", ty_mut.ty)
545 fn is_closure_or_generator(ty: Ty<'_>) -> bool {
546 ty.is_closure() || ty.is_generator()
549 /// Adds a suggestion to a struct definition given a field access to a local.
550 /// This function expects the local to be a reference to a struct in order to produce a suggestion.
553 /// LL | s: &'a String
554 /// | ---------- use `&'a mut String` here to make mutable
556 fn annotate_struct_field(
560 ) -> Option<(Span, String)> {
561 // Expect our local to be a reference to a struct of some kind.
562 if let ty::Ref(_, ty, _) = ty.kind {
563 if let ty::Adt(def, _) = ty.kind {
564 let field = def.all_fields().nth(field.index())?;
565 // Use the HIR types to construct the diagnostic message.
566 let hir_id = tcx.hir().as_local_hir_id(field.did)?;
567 let node = tcx.hir().find(hir_id)?;
568 // Now we're dealing with the actual struct that we're going to suggest a change to,
569 // we can expect a field that is an immutable reference to a type.
570 if let hir::Node::Field(field) = node {
571 if let hir::TyKind::Rptr(
573 hir::MutTy { mutbl: hir::Mutability::Not, ref ty },
576 // Get the snippets in two parts - the named lifetime (if there is one) and
577 // type being referenced, that way we can reconstruct the snippet without loss
579 let type_snippet = tcx.sess.source_map().span_to_snippet(ty.span).ok()?;
580 let lifetime_snippet = if !lifetime.is_elided() {
581 format!("{} ", tcx.sess.source_map().span_to_snippet(lifetime.span).ok()?)
588 format!("&{}mut {}", lifetime_snippet, &*type_snippet,),
598 /// If possible, suggest replacing `ref` with `ref mut`.
599 fn suggest_ref_mut(tcx: TyCtxt<'_>, binding_span: Span) -> Option<String> {
600 let hi_src = tcx.sess.source_map().span_to_snippet(binding_span).ok()?;
601 if hi_src.starts_with("ref") && hi_src["ref".len()..].starts_with(rustc_lexer::is_whitespace) {
602 let replacement = format!("ref mut{}", &hi_src["ref".len()..]);