Skip to content

Commit 7db4825

Browse files
authored
Rollup merge of rust-lang#70389 - Centril:borrowck-no-underscores, r=mark-i-m
borrowck: prefer "value" over "`_`" in diagnostics Fixes rust-lang#67565. r? @pnkfelix @matthewjasper cc @mark-i-m
2 parents ca7dfb1 + 632c0af commit 7db4825

13 files changed

+119
-135
lines changed

src/librustc_mir/borrow_check/diagnostics/conflict_errors.rs

+31-58
Original file line numberDiff line numberDiff line change
@@ -256,25 +256,17 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
256256
"report_move_out_while_borrowed: location={:?} place={:?} span={:?} borrow={:?}",
257257
location, place, span, borrow
258258
);
259-
let value_msg = match self.describe_place(place.as_ref()) {
260-
Some(name) => format!("`{}`", name),
261-
None => "value".to_owned(),
262-
};
263-
let borrow_msg = match self.describe_place(borrow.borrowed_place.as_ref()) {
264-
Some(name) => format!("`{}`", name),
265-
None => "value".to_owned(),
266-
};
259+
let value_msg = self.describe_any_place(place.as_ref());
260+
let borrow_msg = self.describe_any_place(borrow.borrowed_place.as_ref());
267261

268262
let borrow_spans = self.retrieve_borrow_spans(borrow);
269263
let borrow_span = borrow_spans.args_or_use();
270264

271265
let move_spans = self.move_spans(place.as_ref(), location);
272266
let span = move_spans.args_or_use();
273267

274-
let mut err = self.cannot_move_when_borrowed(
275-
span,
276-
&self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
277-
);
268+
let mut err =
269+
self.cannot_move_when_borrowed(span, &self.describe_any_place(place.as_ref()));
278270
err.span_label(borrow_span, format!("borrow of {} occurs here", borrow_msg));
279271
err.span_label(span, format!("move out of {} occurs here", value_msg));
280272

@@ -314,16 +306,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
314306

315307
let mut err = self.cannot_use_when_mutably_borrowed(
316308
span,
317-
&self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
309+
&self.describe_any_place(place.as_ref()),
318310
borrow_span,
319-
&self.describe_place(borrow.borrowed_place.as_ref()).unwrap_or_else(|| "_".to_owned()),
311+
&self.describe_any_place(borrow.borrowed_place.as_ref()),
320312
);
321313

322314
borrow_spans.var_span_label(&mut err, {
323315
let place = &borrow.borrowed_place;
324-
let desc_place = self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned());
325-
326-
format!("borrow occurs due to use of `{}`{}", desc_place, borrow_spans.describe())
316+
let desc_place = self.describe_any_place(place.as_ref());
317+
format!("borrow occurs due to use of {}{}", desc_place, borrow_spans.describe())
327318
});
328319

329320
self.explain_why_borrow_contains_point(location, borrow, None)
@@ -433,7 +424,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
433424
borrow_spans.var_span_label(
434425
&mut err,
435426
format!(
436-
"borrow occurs due to use of `{}`{}",
427+
"borrow occurs due to use of {}{}",
437428
desc_place,
438429
borrow_spans.describe(),
439430
),
@@ -511,16 +502,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
511502
if issued_spans == borrow_spans {
512503
borrow_spans.var_span_label(
513504
&mut err,
514-
format!("borrows occur due to use of `{}`{}", desc_place, borrow_spans.describe()),
505+
format!("borrows occur due to use of {}{}", desc_place, borrow_spans.describe()),
515506
);
516507
} else {
517508
let borrow_place = &issued_borrow.borrowed_place;
518-
let borrow_place_desc =
519-
self.describe_place(borrow_place.as_ref()).unwrap_or_else(|| "_".to_owned());
509+
let borrow_place_desc = self.describe_any_place(borrow_place.as_ref());
520510
issued_spans.var_span_label(
521511
&mut err,
522512
format!(
523-
"first borrow occurs due to use of `{}`{}",
513+
"first borrow occurs due to use of {}{}",
524514
borrow_place_desc,
525515
issued_spans.describe(),
526516
),
@@ -529,7 +519,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
529519
borrow_spans.var_span_label(
530520
&mut err,
531521
format!(
532-
"second borrow occurs due to use of `{}`{}",
522+
"second borrow occurs due to use of {}{}",
533523
desc_place,
534524
borrow_spans.describe(),
535525
),
@@ -538,7 +528,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
538528

539529
if union_type_name != "" {
540530
err.note(&format!(
541-
"`{}` is a field of the union `{}`, so it overlaps the field `{}`",
531+
"{} is a field of the union `{}`, so it overlaps the field {}",
542532
msg_place, union_type_name, msg_borrow,
543533
));
544534
}
@@ -606,7 +596,6 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
606596
let ty = Place::ty_from(place_base, place_projection, *self.body, self.infcx.tcx).ty;
607597
ty.ty_adt_def().filter(|adt| adt.is_union()).map(|_| ty)
608598
};
609-
let describe_place = |place| self.describe_place(place).unwrap_or_else(|| "_".to_owned());
610599

611600
// Start with an empty tuple, so we can use the functions on `Option` to reduce some
612601
// code duplication (particularly around returning an empty description in the failure
@@ -645,30 +634,25 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
645634
.and_then(|(target_base, target_field)| {
646635
// With the place of a union and a field access into it, we traverse the second
647636
// borrowed place and look for a access to a different field of the same union.
648-
let Place { local, projection } = second_borrowed_place;
637+
let Place { local, ref projection } = *second_borrowed_place;
649638

650639
let mut cursor = &projection[..];
651640
while let [proj_base @ .., elem] = cursor {
652641
cursor = proj_base;
653642

654643
if let ProjectionElem::Field(field, _) = elem {
655-
if let Some(union_ty) = union_ty(*local, proj_base) {
644+
if let Some(union_ty) = union_ty(local, proj_base) {
656645
if field != target_field
657-
&& *local == target_base.local
646+
&& local == target_base.local
658647
&& proj_base == target_base.projection
659648
{
660-
// FIXME when we avoid clone reuse describe_place closure
661-
let describe_base_place = self
662-
.describe_place(PlaceRef {
663-
local: *local,
664-
projection: proj_base,
665-
})
666-
.unwrap_or_else(|| "_".to_owned());
667-
668649
return Some((
669-
describe_base_place,
670-
describe_place(first_borrowed_place.as_ref()),
671-
describe_place(second_borrowed_place.as_ref()),
650+
self.describe_any_place(PlaceRef {
651+
local,
652+
projection: proj_base,
653+
}),
654+
self.describe_any_place(first_borrowed_place.as_ref()),
655+
self.describe_any_place(second_borrowed_place.as_ref()),
672656
union_ty.to_string(),
673657
));
674658
}
@@ -681,7 +665,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
681665
// If we didn't find a field access into a union, or both places match, then
682666
// only return the description of the first place.
683667
(
684-
describe_place(first_borrowed_place.as_ref()),
668+
self.describe_any_place(first_borrowed_place.as_ref()),
685669
"".to_string(),
686670
"".to_string(),
687671
"".to_string(),
@@ -1404,12 +1388,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14041388
let loan_spans = self.retrieve_borrow_spans(loan);
14051389
let loan_span = loan_spans.args_or_use();
14061390

1391+
let descr_place = self.describe_any_place(place.as_ref());
14071392
if loan.kind == BorrowKind::Shallow {
14081393
if let Some(section) = self.classify_immutable_section(&loan.assigned_place) {
14091394
let mut err = self.cannot_mutate_in_immutable_section(
14101395
span,
14111396
loan_span,
1412-
&self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
1397+
&descr_place,
14131398
section,
14141399
"assign",
14151400
);
@@ -1424,11 +1409,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14241409
}
14251410
}
14261411

1427-
let mut err = self.cannot_assign_to_borrowed(
1428-
span,
1429-
loan_span,
1430-
&self.describe_place(place.as_ref()).unwrap_or_else(|| "_".to_owned()),
1431-
);
1412+
let mut err = self.cannot_assign_to_borrowed(span, loan_span, &descr_place);
14321413

14331414
loan_spans
14341415
.var_span_label(&mut err, format!("borrow occurs due to use{}", loan_spans.describe()));
@@ -1482,27 +1463,19 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14821463
})
14831464
| Some(LocalDecl { local_info: LocalInfo::StaticRef { .. }, .. })
14841465
| Some(LocalDecl { local_info: LocalInfo::Other, .. })
1485-
| None => (self.describe_place(place.as_ref()), assigned_span),
1486-
Some(decl) => (self.describe_place(err_place.as_ref()), decl.source_info.span),
1466+
| None => (self.describe_any_place(place.as_ref()), assigned_span),
1467+
Some(decl) => (self.describe_any_place(err_place.as_ref()), decl.source_info.span),
14871468
};
14881469

1489-
let mut err = self.cannot_reassign_immutable(
1490-
span,
1491-
place_description.as_ref().map(AsRef::as_ref).unwrap_or("_"),
1492-
from_arg,
1493-
);
1470+
let mut err = self.cannot_reassign_immutable(span, &place_description, from_arg);
14941471
let msg = if from_arg {
14951472
"cannot assign to immutable argument"
14961473
} else {
14971474
"cannot assign twice to immutable variable"
14981475
};
14991476
if span != assigned_span {
15001477
if !from_arg {
1501-
let value_msg = match place_description {
1502-
Some(name) => format!("`{}`", name),
1503-
None => "value".to_owned(),
1504-
};
1505-
err.span_label(assigned_span, format!("first assignment to {}", value_msg));
1478+
err.span_label(assigned_span, format!("first assignment to {}", place_description));
15061479
}
15071480
}
15081481
if let Some(decl) = local_decl {

src/librustc_mir/borrow_check/diagnostics/mod.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,23 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
137137
}
138138
}
139139

140-
/// End-user visible description of `place` if one can be found. If the
141-
/// place is a temporary for instance, None will be returned.
140+
/// End-user visible description of `place` if one can be found.
141+
/// If the place is a temporary for instance, `"value"` will be returned.
142+
pub(super) fn describe_any_place(&self, place_ref: PlaceRef<'tcx>) -> String {
143+
match self.describe_place(place_ref) {
144+
Some(mut descr) => {
145+
// Surround descr with `backticks`.
146+
descr.reserve(2);
147+
descr.insert_str(0, "`");
148+
descr.push_str("`");
149+
descr
150+
}
151+
None => "value".to_string(),
152+
}
153+
}
154+
155+
/// End-user visible description of `place` if one can be found.
156+
/// If the place is a temporary for instance, None will be returned.
142157
pub(super) fn describe_place(&self, place_ref: PlaceRef<'tcx>) -> Option<String> {
143158
self.describe_place_with_options(place_ref, IncludingDowncast(false))
144159
}

src/librustc_mir/borrow_check/diagnostics/move_errors.rs

+11-13
Original file line numberDiff line numberDiff line change
@@ -272,14 +272,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
272272
span: Span,
273273
) -> DiagnosticBuilder<'a> {
274274
let description = if place.projection.len() == 1 {
275-
format!("static item `{}`", self.describe_place(place.as_ref()).unwrap())
275+
format!("static item {}", self.describe_any_place(place.as_ref()))
276276
} else {
277277
let base_static = PlaceRef { local: place.local, projection: &[ProjectionElem::Deref] };
278278

279279
format!(
280-
"`{:?}` as `{:?}` is a static item",
281-
self.describe_place(place.as_ref()).unwrap(),
282-
self.describe_place(base_static).unwrap(),
280+
"{} as {} is a static item",
281+
self.describe_any_place(place.as_ref()),
282+
self.describe_any_place(base_static),
283283
)
284284
};
285285

@@ -349,16 +349,14 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
349349
let upvar_name = upvar.name;
350350
let upvar_span = self.infcx.tcx.hir().span(upvar_hir_id);
351351

352-
let place_name = self.describe_place(move_place.as_ref()).unwrap();
352+
let place_name = self.describe_any_place(move_place.as_ref());
353353

354-
let place_description = if self
355-
.is_upvar_field_projection(move_place.as_ref())
356-
.is_some()
357-
{
358-
format!("`{}`, a {}", place_name, capture_description)
359-
} else {
360-
format!("`{}`, as `{}` is a {}", place_name, upvar_name, capture_description,)
361-
};
354+
let place_description =
355+
if self.is_upvar_field_projection(move_place.as_ref()).is_some() {
356+
format!("{}, a {}", place_name, capture_description)
357+
} else {
358+
format!("{}, as `{}` is a {}", place_name, upvar_name, capture_description)
359+
};
362360

363361
debug!(
364362
"report: closure_kind_ty={:?} closure_kind={:?} place_description={:?}",

src/librustc_mir/borrow_check/diagnostics/mutability_errors.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
169169
borrow_spans.var_span_label(
170170
&mut err,
171171
format!(
172-
"mutable borrow occurs due to use of `{}` in closure",
173-
// always Some() if the message is printed.
174-
self.describe_place(access_place.as_ref()).unwrap_or_default(),
172+
"mutable borrow occurs due to use of {} in closure",
173+
self.describe_any_place(access_place.as_ref()),
175174
),
176175
);
177176
borrow_span

0 commit comments

Comments
 (0)