Skip to content

Commit

Permalink
Use fmt named parameters in rustc_borrowck
Browse files Browse the repository at this point in the history
  • Loading branch information
wcampbell0x2a committed Jan 7, 2023
1 parent 0fb8b72 commit d0cf7e3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 92 deletions.
12 changes: 5 additions & 7 deletions compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_note(
MultiSpan::from_spans(reinit_spans),
&if reinits <= 3 {
format!("these {} reinitializations might get skipped", reinits)
format!("these {reinits} reinitializations might get skipped")
} else {
format!(
"these 3 reinitializations and {} other{} might get skipped",
Expand Down Expand Up @@ -225,9 +225,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
span,
format!(
"value {} here after {}move",
"value {} here after {partial_str}move",
desired_action.as_verb_in_past_tense(),
partial_str
),
);
}
Expand Down Expand Up @@ -257,7 +256,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&format!(
"consider creating a fresh reborrow of {} here",
self.describe_place(moved_place)
.map(|n| format!("`{}`", n))
.map(|n| format!("`{n}`"))
.unwrap_or_else(|| "the mutable reference".to_string()),
),
"&mut *",
Expand All @@ -271,7 +270,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
DescribePlaceOpt { including_downcast: true, including_tuple_field: true },
);
let note_msg = match opt_name {
Some(name) => format!("`{}`", name),
Some(name) => format!("`{name}`"),
None => "value".to_owned(),
};
if self.suggest_borrow_fn_like(&mut err, ty, &move_site_vec, &note_msg) {
Expand All @@ -297,9 +296,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
} = use_spans
{
err.note(&format!(
"{} occurs due to deref coercion to `{}`",
"{} occurs due to deref coercion to `{deref_target_ty}`",
desired_action.as_noun(),
deref_target_ty
));

// Check first whether the source is accessible (issue #87060)
Expand Down
39 changes: 13 additions & 26 deletions compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
if borrow_span.map(|sp| !sp.overlaps(var_or_use_span)).unwrap_or(true) {
err.span_label(
var_or_use_span,
format!("{}borrow later {}", borrow_desc, message),
format!("{borrow_desc}borrow later {message}"),
);
}
} else {
Expand All @@ -90,7 +90,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
let capture_kind_label = message;
err.span_label(
var_or_use_span,
format!("{}borrow later {}", borrow_desc, capture_kind_label),
format!("{borrow_desc}borrow later {capture_kind_label}"),
);
err.span_label(path_span, path_label);
}
Expand All @@ -110,7 +110,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
};
// We can use `var_or_use_span` if either `path_span` is not present, or both spans are the same
if path_span.map(|path_span| path_span == var_or_use_span).unwrap_or(true) {
err.span_label(var_or_use_span, format!("{}{}", borrow_desc, message));
err.span_label(var_or_use_span, format!("{borrow_desc}{message}"));
} else {
// path_span must be `Some` as otherwise the if condition is true
let path_span = path_span.unwrap();
Expand All @@ -121,7 +121,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
let capture_kind_label = message;
err.span_label(
var_or_use_span,
format!("{}borrow later {}", borrow_desc, capture_kind_label),
format!("{borrow_desc}borrow later {capture_kind_label}"),
);
err.span_label(path_span, path_label);
}
Expand Down Expand Up @@ -160,12 +160,8 @@ impl<'tcx> BorrowExplanation<'tcx> {
match local_names[dropped_local] {
Some(local_name) if !local_decl.from_compiler_desugaring() => {
let message = format!(
"{B}borrow might be used here, when `{LOC}` is dropped \
and runs the {DTOR} for {TYPE}",
B = borrow_desc,
LOC = local_name,
TYPE = type_desc,
DTOR = dtor_desc
"{borrow_desc}borrow might be used here, when `{local_name}` is dropped \
and runs the {dtor_desc} for {type_desc}",
);
err.span_label(body.source_info(drop_loc).span, message);

Expand All @@ -180,18 +176,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
err.span_label(
local_decl.source_info.span,
format!(
"a temporary with access to the {B}borrow \
"a temporary with access to the {borrow_desc}borrow \
is created here ...",
B = borrow_desc
),
);
let message = format!(
"... and the {B}borrow might be used here, \
"... and the {borrow_desc}borrow might be used here, \
when that temporary is dropped \
and runs the {DTOR} for {TYPE}",
B = borrow_desc,
TYPE = type_desc,
DTOR = dtor_desc
and runs the {dtor_desc} for {type_desc}",
);
err.span_label(body.source_info(drop_loc).span, message);

Expand Down Expand Up @@ -249,20 +241,16 @@ impl<'tcx> BorrowExplanation<'tcx> {
err.span_label(
span,
format!(
"{}requires that `{}` is borrowed for `{}`",
"{}requires that `{desc}` is borrowed for `{region_name}`",
category.description(),
desc,
region_name,
),
);
} else {
err.span_label(
span,
format!(
"{}requires that {}borrow lasts for `{}`",
"{}requires that {borrow_desc}borrow lasts for `{region_name}`",
category.description(),
borrow_desc,
region_name,
),
);
};
Expand Down Expand Up @@ -296,15 +284,14 @@ impl<'tcx> BorrowExplanation<'tcx> {
if region_name.was_named() { region_name.name } else { kw::UnderscoreLifetime };

let msg = format!(
"you can add a bound to the {}to make it last less than `'static` and match `{}`",
"you can add a bound to the {}to make it last less than `'static` and match `{region_name}`",
category.description(),
region_name,
);

err.span_suggestion_verbose(
span.shrink_to_hi(),
&msg,
format!(" + {}", suggestable_name),
format!(" + {suggestable_name}"),
Applicability::Unspecified,
);
}
Expand Down
38 changes: 16 additions & 22 deletions compiler/rustc_borrowck/src/diagnostics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,8 +403,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
move_prefix: &str,
) {
let message = format!(
"{}move occurs because {} has type `{}`, which does not implement the `Copy` trait",
move_prefix, place_desc, ty,
"{move_prefix}move occurs because {place_desc} has type `{ty}`, which does not implement the `Copy` trait",
);
if let Some(span) = span {
err.span_label(span, message);
Expand Down Expand Up @@ -739,11 +738,11 @@ impl<'tcx> BorrowedContentSource<'tcx> {
BorrowedContentSource::OverloadedDeref(ty) => ty
.ty_adt_def()
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
_ => None,
})
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{}`", ty),
.unwrap_or_else(|| format!("dereference of `{ty}`")),
BorrowedContentSource::OverloadedIndex(ty) => format!("index of `{ty}`"),
}
}

Expand All @@ -769,11 +768,11 @@ impl<'tcx> BorrowedContentSource<'tcx> {
BorrowedContentSource::OverloadedDeref(ty) => ty
.ty_adt_def()
.and_then(|adt| match tcx.get_diagnostic_name(adt.did())? {
name @ (sym::Rc | sym::Arc) => Some(format!("an `{}`", name)),
name @ (sym::Rc | sym::Arc) => Some(format!("an `{name}`")),
_ => None,
})
.unwrap_or_else(|| format!("dereference of `{}`", ty)),
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{}`", ty),
.unwrap_or_else(|| format!("dereference of `{ty}`")),
BorrowedContentSource::OverloadedIndex(ty) => format!("an index of `{ty}`"),
}
}

Expand Down Expand Up @@ -1033,7 +1032,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
if let UseSpans::FnSelfUse { var_span, fn_call_span, fn_span, kind } = move_spans {
let place_name = self
.describe_place(moved_place.as_ref())
.map(|n| format!("`{}`", n))
.map(|n| format!("`{n}`"))
.unwrap_or_else(|| "value".to_owned());
match kind {
CallKind::FnCall { fn_trait_id, .. }
Expand All @@ -1042,8 +1041,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
fn_call_span,
&format!(
"{} {}moved due to this call{}",
place_name, partially_str, loop_message
"{place_name} {partially_str}moved due to this call{loop_message}",
),
);
err.span_note(
Expand All @@ -1056,8 +1054,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
fn_call_span,
&format!(
"{} {}moved due to usage in operator{}",
place_name, partially_str, loop_message
"{place_name} {partially_str}moved due to usage in operator{loop_message}",
),
);
if self.fn_self_span_reported.insert(fn_span) {
Expand Down Expand Up @@ -1089,9 +1086,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_suggestion_verbose(
move_span.shrink_to_lo(),
&format!(
"consider iterating over a slice of the `{}`'s content to \
"consider iterating over a slice of the `{ty}`'s content to \
avoid moving into the `for` loop",
ty,
),
"&",
Applicability::MaybeIncorrect,
Expand All @@ -1101,8 +1097,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
fn_call_span,
&format!(
"{} {}moved due to this implicit call to `.into_iter()`{}",
place_name, partially_str, loop_message
"{place_name} {partially_str}moved due to this implicit call to `.into_iter()`{loop_message}",
),
);
// If the moved place was a `&mut` ref, then we can
Expand All @@ -1118,7 +1113,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
&format!(
"consider creating a fresh reborrow of {} here",
self.describe_place(moved_place.as_ref())
.map(|n| format!("`{}`", n))
.map(|n| format!("`{n}`"))
.unwrap_or_else(|| "the mutable reference".to_string()),
),
"&mut *",
Expand All @@ -1130,8 +1125,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
err.span_label(
fn_call_span,
&format!(
"{} {}moved due to this method call{}",
place_name, partially_str, loop_message
"{place_name} {partially_str}moved due to this method call{loop_message}",
),
);
let infcx = tcx.infer_ctxt().build();
Expand Down Expand Up @@ -1206,15 +1200,15 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
if move_span != span || !loop_message.is_empty() {
err.span_label(
move_span,
format!("value {}moved{} here{}", partially_str, move_msg, loop_message),
format!("value {partially_str}moved{move_msg} here{loop_message}"),
);
}
// If the move error occurs due to a loop, don't show
// another message for the same span
if loop_message.is_empty() {
move_spans.var_span_label(
err,
format!("variable {}moved due to use{}", partially_str, move_spans.describe()),
format!("variable {partially_str}moved due to use{}", move_spans.describe()),
"moved",
);
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
ProjectionElem::Deref,
],
} => {
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, format!("cannot {act}"));

if let Some(span) = get_mut_span_in_struct_field(
self.infcx.tcx,
Expand All @@ -290,7 +290,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
.unwrap_or(false) =>
{
let decl = &self.body.local_decls[local];
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, format!("cannot {act}"));
if let Some(mir::Statement {
source_info,
kind:
Expand Down Expand Up @@ -634,7 +634,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
}

PlaceRef { local: _, projection: [.., ProjectionElem::Deref] } => {
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, format!("cannot {act}"));

match opt_source {
Some(BorrowedContentSource::OverloadedDeref(ty)) => {
Expand Down Expand Up @@ -1207,7 +1207,7 @@ fn suggest_ampmut<'tcx>(
{
let lt_name = &src[1..ws_pos];
let ty = &src[ws_pos..];
return (true, highlight_span, format!("&{} mut{}", lt_name, ty));
return (true, highlight_span, format!("&{lt_name} mut{ty}"));
}

let ty_mut = local_decl.ty.builtin_deref(true).unwrap();
Expand Down
18 changes: 8 additions & 10 deletions compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,14 @@ impl OutlivesSuggestionBuilder {
let mut diag = if suggested.len() == 1 {
mbcx.infcx.tcx.sess.diagnostic().struct_help(&match suggested.last().unwrap() {
SuggestedConstraint::Outlives(a, bs) => {
let bs: SmallVec<[String; 2]> = bs.iter().map(|r| format!("{}", r)).collect();
format!("add bound `{}: {}`", a, bs.join(" + "))
let bs: SmallVec<[String; 2]> = bs.iter().map(|r| r.to_string()).collect();
format!("add bound `{a}: {}`", bs.join(" + "))
}

SuggestedConstraint::Equal(a, b) => {
format!("`{}` and `{}` must be the same: replace one with the other", a, b)
format!("`{a}` and `{b}` must be the same: replace one with the other")
}
SuggestedConstraint::Static(a) => format!("replace `{}` with `'static`", a),
SuggestedConstraint::Static(a) => format!("replace `{a}` with `'static`"),
})
} else {
// Create a new diagnostic.
Expand All @@ -231,18 +231,16 @@ impl OutlivesSuggestionBuilder {
for constraint in suggested {
match constraint {
SuggestedConstraint::Outlives(a, bs) => {
let bs: SmallVec<[String; 2]> =
bs.iter().map(|r| format!("{}", r)).collect();
diag.help(&format!("add bound `{}: {}`", a, bs.join(" + ")));
let bs: SmallVec<[String; 2]> = bs.iter().map(|r| r.to_string()).collect();
diag.help(&format!("add bound `{a}: {}`", bs.join(" + ")));
}
SuggestedConstraint::Equal(a, b) => {
diag.help(&format!(
"`{}` and `{}` must be the same: replace one with the other",
a, b
"`{a}` and `{b}` must be the same: replace one with the other",
));
}
SuggestedConstraint::Static(a) => {
diag.help(&format!("replace `{}` with `'static`", a));
diag.help(&format!("replace `{a}` with `'static`"));
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_borrowck/src/diagnostics/region_errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
);
(desc, note)
}
_ => panic!("Unexpected type {:?}", ty),
_ => panic!("Unexpected type {ty:?}"),
};
diag.note(&format!("requirement occurs because of {desc}",));
diag.note(&note);
Expand Down Expand Up @@ -725,10 +725,10 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
let lifetime = if f.has_name() { fr_name.name } else { kw::UnderscoreLifetime };

let arg = match param.param.pat.simple_ident() {
Some(simple_ident) => format!("argument `{}`", simple_ident),
Some(simple_ident) => format!("argument `{simple_ident}`"),
None => "the argument".to_string(),
};
let captures = format!("captures data from {}", arg);
let captures = format!("captures data from {arg}");

return nice_region_error::suggest_new_region_bound(
self.infcx.tcx,
Expand Down
Loading

0 comments on commit d0cf7e3

Please sign in to comment.