rustc_borrowck: Convert suggest_ampmut() 4-tuple to struct for readab… · rust-lang/rust@f6cb227 (original) (raw)

`@@ -1100,12 +1100,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

1100

1100

`}

`

1101

1101

`let decl_span = local_decl.source_info.span;

`

1102

1102

``

1103

``

`-

let label = match *local_decl.local_info() {

`

``

1103

`+

let amp_mut_sugg = match *local_decl.local_info() {

`

1104

1104

`LocalInfo::User(mir::BindingForm::ImplicitSelf(_)) => {

`

1105

1105

`let suggestion = suggest_ampmut_self(self.infcx.tcx, decl_span);

`

1106

1106

`let additional =

`

1107

1107

` local_trait.map(|span| (span, suggest_ampmut_self(self.infcx.tcx, span)));

`

1108

``

`-

Some((true, decl_span, suggestion, additional))

`

``

1108

`+

Some(AmpMutSugg { has_sugg: true, span: decl_span, suggestion, additional })

`

1109

1109

`}

`

1110

1110

``

1111

1111

`LocalInfo::User(mir::BindingForm::Var(mir::VarBindingForm {

`

`@@ -1165,7 +1165,12 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

1165

1165

` ..

`

1166

1166

`})) => {

`

1167

1167

`let sugg = suggest_ampmut_self(self.infcx.tcx, decl_span);

`

1168

``

`-

Some((true, decl_span, sugg, None))

`

``

1168

`+

Some(AmpMutSugg {

`

``

1169

`+

has_sugg: true,

`

``

1170

`+

span: decl_span,

`

``

1171

`+

suggestion: sugg,

`

``

1172

`+

additional: None,

`

``

1173

`+

})

`

1169

1174

`}

`

1170

1175

`` // explicit self (eg self: &'a Self)

``

1171

1176

` _ => suggest_ampmut(

`

`@@ -1186,15 +1191,24 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

1186

1191

` ..

`

1187

1192

`})) => {

`

1188

1193

`let pattern_span: Span = local_decl.source_info.span;

`

1189

``

`-

suggest_ref_mut(self.infcx.tcx, pattern_span)

`

1190

``

`-

.map(|span| (true, span, "mut ".to_owned(), None))

`

``

1194

`+

suggest_ref_mut(self.infcx.tcx, pattern_span).map(|span| AmpMutSugg {

`

``

1195

`+

has_sugg: true,

`

``

1196

`+

span,

`

``

1197

`+

suggestion: "mut ".to_owned(),

`

``

1198

`+

additional: None,

`

``

1199

`+

})

`

1191

1200

`}

`

1192

1201

``

1193

1202

` _ => unreachable!(),

`

1194

1203

`};

`

1195

1204

``

1196

``

`-

match label {

`

1197

``

`-

Some((true, err_help_span, suggested_code, additional)) => {

`

``

1205

`+

match amp_mut_sugg {

`

``

1206

`+

Some(AmpMutSugg {

`

``

1207

`+

has_sugg: true,

`

``

1208

`+

span: err_help_span,

`

``

1209

`+

suggestion: suggested_code,

`

``

1210

`+

additional,

`

``

1211

`+

}) => {

`

1198

1212

`let mut sugg = vec![(err_help_span, suggested_code)];

`

1199

1213

`if let Some(s) = additional {

`

1200

1214

` sugg.push(s);

`

`@@ -1216,7 +1230,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {

`

1216

1230

`);

`

1217

1231

`}

`

1218

1232

`}

`

1219

``

`-

Some((false, err_label_span, message, _)) => {

`

``

1233

`+

Some(AmpMutSugg {

`

``

1234

`+

has_sugg: false, span: err_label_span, suggestion: message, ..

`

``

1235

`+

}) => {

`

1220

1236

`let def_id = self.body.source.def_id();

`

1221

1237

`let hir_id = if let Some(local_def_id) = def_id.as_local()

`

1222

1238

` && 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 {

`

1421

1437

`}

`

1422

1438

`}

`

1423

1439

``

``

1440

`+

struct AmpMutSugg {

`

``

1441

`+

has_sugg: bool,

`

``

1442

`+

span: Span,

`

``

1443

`+

suggestion: String,

`

``

1444

`+

additional: Option<(Span, String)>,

`

``

1445

`+

}

`

``

1446

+

1424

1447

`` // When we want to suggest a user change a local variable to be a &mut, there

``

1425

1448

`// are three potential "obvious" things to highlight:

`

1426

1449

`//

`

`@@ -1442,7 +1465,7 @@ fn suggest_ampmut<'tcx>(

`

1442

1465

`decl_span: Span,

`

1443

1466

`opt_assignment_rhs_span: Option,

`

1444

1467

`opt_ty_info: Option,

`

1445

``

`-

) -> Option<(bool, Span, String, Option<(Span, String)>)> {

`

``

1468

`+

) -> Option {

`

1446

1469

`` // if there is a RHS and it starts with a & from it, then check if it is

``

1447

1470

`` // mutable, and if not, put suggest putting mut to make it mutable.

``

1448

1471

`// we don't have to worry about lifetime annotations here because they are

`

`@@ -1483,7 +1506,12 @@ fn suggest_ampmut<'tcx>(

`

1483

1506

``

1484

1507

`// FIXME(Ezrashaw): returning is bad because we still might want to

`

1485

1508

`// update the annotated type, see #106857.

`

1486

``

`-

return Some((true, span, "mut ".to_owned(), None));

`

``

1509

`+

return Some(AmpMutSugg {

`

``

1510

`+

has_sugg: true,

`

``

1511

`+

span,

`

``

1512

`+

suggestion: "mut ".to_owned(),

`

``

1513

`+

additional: None,

`

``

1514

`+

});

`

1487

1515

`}

`

1488

1516

`}

`

1489

1517

``

`@@ -1508,18 +1536,23 @@ fn suggest_ampmut<'tcx>(

`

1508

1536

` && let Some(ws_pos) = src.find(char::is_whitespace)

`

1509

1537

`{

`

1510

1538

`let span = span.with_lo(span.lo() + BytePos(ws_pos as u32)).shrink_to_lo();

`

1511

``

`-

Some((true, span, " mut".to_owned(), None))

`

``

1539

`+

Some(AmpMutSugg { has_sugg: true, span, suggestion: " mut".to_owned(), additional: None })

`

1512

1540

`` // if there is already a binding, we modify it to be mut

``

1513

1541

`} else if binding_exists {

`

1514

1542

`` // shrink the span to just after the & in &variable

``

1515

1543

`let span = span.with_lo(span.lo() + BytePos(1)).shrink_to_lo();

`

1516

``

`-

Some((true, span, "mut ".to_owned(), None))

`

``

1544

`+

Some(AmpMutSugg { has_sugg: true, span, suggestion: "mut ".to_owned(), additional: None })

`

1517

1545

`} else {

`

1518

1546

`// otherwise, suggest that the user annotates the binding; we provide the

`

1519

1547

`// type of the local.

`

1520

1548

`let ty = decl_ty.builtin_deref(true).unwrap();

`

1521

1549

``

1522

``

`-

Some((false, span, format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty), None))

`

``

1550

`+

Some(AmpMutSugg {

`

``

1551

`+

has_sugg: false,

`

``

1552

`+

span,

`

``

1553

`+

suggestion: format!("{}mut {}", if decl_ty.is_ref() { "&" } else { "*" }, ty),

`

``

1554

`+

additional: None,

`

``

1555

`+

})

`

1523

1556

`}

`

1524

1557

`}

`

1525

1558

``