1
Fork 0

rustc_borrowck: Convert suggest_ampmut() 4-tuple to struct for readability

This commit is contained in:
Martin Nordholts 2024-12-13 06:51:17 +01:00
parent 2d2c6f2a80
commit f6cb227f61

View file

@ -1100,12 +1100,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
} }
let decl_span = local_decl.source_info.span; let decl_span = local_decl.source_info.span;
let label = match *local_decl.local_info() { let amp_mut_sugg = match *local_decl.local_info() {
LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => { LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {
let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span); let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);
let additional = let additional =
local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span))); local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));
Some((true, decl_span, suggestion, additional)) Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })
} }
LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm { LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {
@ -1165,7 +1165,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
.. ..
})) => { })) => {
let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span); let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);
Some((true, decl_span, sugg, None)) Some(AmpMutSugg {
has_sugg: true,
span: decl_span,
suggestion: sugg,
additional: None,
})
} }
// explicit self (eg `self: &'a Self`) // explicit self (eg `self: &'a Self`)
_ => suggest_ampmut( _ => suggest_ampmut(
@ -1186,15 +1191,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
.. ..
})) => { })) => {
let pattern_span: Span = local_decl.source_info.span; let pattern_span: Span = local_decl.source_info.span;
suggest_ref_mut(self.infcx.tcx, pattern_span) suggest_ref_mut(self.infcx.tcx, pattern_span).map(|span| AmpMutSugg {
.map(|span| (true, span, "mut ".to_owned(), None)) has_sugg: true,
span,
suggestion: "mut ".to_owned(),
additional: None,
})
} }
_ => unreachable!(), _ => unreachable!(),
}; };
match label { match amp_mut_sugg {
Some((true, err_help_span, suggested_code, additional)) => { Some(AmpMutSugg {
has_sugg: true,
span: err_help_span,
suggestion: suggested_code,
additional,
}) => {
let mut sugg = vec![(err_help_span, suggested_code)]; let mut sugg = vec![(err_help_span, suggested_code)];
if let Some(s) = additional { if let Some(s) = additional {
sugg.push(s); sugg.push(s);
@ -1216,7 +1230,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
); );
} }
} }
Some((false, err_label_span, message, _)) => { Some(AmpMutSugg {
has_sugg: false, span: err_label_span, suggestion: message, ..
}) => {
let def_id = self.body.source.def_id(); let def_id = self.body.source.def_id();
let hir_id = if let Some(local_def_id) = def_id.as_local() let hir_id = if let Some(local_def_id) = def_id.as_local()
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id) && let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
@ -1421,6 +1437,13 @@ fn suggest_ampmut_self<'tcx>(tcx: TyCtxt<'tcx>, span: Span) -> String {
} }
} }
struct AmpMutSugg {
has_sugg: bool,
span: Span,
suggestion: String,
additional: Option<(Span, String)>,
}
// When we want to suggest a user change a local variable to be a `&mut`, there // When we want to suggest a user change a local variable to be a `&mut`, there
// are three potential "obvious" things to highlight: // are three potential "obvious" things to highlight:
// //
@ -1442,7 +1465,7 @@ fn suggest_ampmut<'tcx>(
decl_span: Span, decl_span: Span,
opt_assignment_rhs_span: Option<Span>, opt_assignment_rhs_span: Option<Span>,
opt_ty_info: Option<Span>, opt_ty_info: Option<Span>,
) -> Option<(bool, Span, String, Option<(Span, String)>)> { ) -> Option<AmpMutSugg> {
// if there is a RHS and it starts with a `&` from it, then check if it is // if there is a RHS and it starts with a `&` from it, then check if it is
// mutable, and if not, put suggest putting `mut ` to make it mutable. // mutable, and if not, put suggest putting `mut ` to make it mutable.
// we don't have to worry about lifetime annotations here because they are // we don't have to worry about lifetime annotations here because they are
@ -1483,7 +1506,12 @@ fn suggest_ampmut<'tcx>(
// FIXME(Ezrashaw): returning is bad because we still might want to // FIXME(Ezrashaw): returning is bad because we still might want to
// update the annotated type, see #106857. // update the annotated type, see #106857.
return Some((true, span, "mut ".to_owned(), None)); return Some(AmpMutSugg {
has_sugg: true,
span,
suggestion: "mut ".to_owned(),
additional: None,
});
} }
} }
@ -1508,18 +1536,23 @@ fn suggest_ampmut<'tcx>(
&& let Some(ws_pos) = src.find(char::is_whitespace) && let Some(ws_pos) = src.find(char::is_whitespace)
{ {
let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo(); let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo();
Some((true, span, " mut".to_owned(), None)) Some(AmpMutSugg { has_sugg: true, span, suggestion: " mut".to_owned(), additional: None })
// if there is already a binding, we modify it to be `mut` // if there is already a binding, we modify it to be `mut`
} else if binding_exists { } else if binding_exists {
// shrink the span to just after the `&` in `&variable` // shrink the span to just after the `&` in `&variable`
let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo(); let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();
Some((true, span, "mut ".to_owned(), None)) Some(AmpMutSugg { has_sugg: true, span, suggestion: "mut ".to_owned(), additional: None })
} else { } else {
// otherwise, suggest that the user annotates the binding; we provide the // otherwise, suggest that the user annotates the binding; we provide the
// type of the local. // type of the local.
let ty = decl_ty.builtin_deref(true).unwrap(); let ty = decl_ty.builtin_deref(true).unwrap();
Some((false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), None)) Some(AmpMutSugg {
has_sugg: false,
span,
suggestion: format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty),
additional: None,
})
} }
} }