1
Fork 0

Restrict From<S> for {D,Subd}iagnosticMessage.

Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.

This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.

As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
This commit is contained in:
Nicholas Nethercote 2023-04-20 13:26:58 +10:00
parent a368898de7
commit 6b62f37402
177 changed files with 791 additions and 787 deletions

View file

@ -205,7 +205,7 @@ impl CanonicalizeMode for CanonicalizeQueryResponse {
// `delay_span_bug` to allow type error over an ICE.
canonicalizer.tcx.sess.delay_span_bug(
rustc_span::DUMMY_SP,
&format!("unexpected region in query response: `{:?}`", r),
format!("unexpected region in query response: `{:?}`", r),
);
r
}

View file

@ -192,7 +192,7 @@ impl<'tcx> InferCtxt<'tcx> {
self.tcx.check_tys_might_be_eq(canonical).map_err(|_| {
self.tcx.sess.delay_span_bug(
DUMMY_SP,
&format!("cannot relate consts of different types (a={:?}, b={:?})", a, b,),
format!("cannot relate consts of different types (a={:?}, b={:?})", a, b,),
)
})
});

View file

@ -282,9 +282,9 @@ fn emit_msg_span(
let message = format!("{}{}{}", prefix, description, suffix);
if let Some(span) = span {
err.span_note(span, &message);
err.span_note(span, message);
} else {
err.note(&message);
err.note(message);
}
}
@ -298,9 +298,9 @@ fn label_msg_span(
let message = format!("{}{}{}", prefix, description, suffix);
if let Some(span) = span {
err.span_label(span, &message);
err.span_label(span, message);
} else {
err.note(&message);
err.note(message);
}
}
@ -2395,7 +2395,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
} else {
let consider = format!("{} `{}: {}`...", msg, bound_kind, sub);
err.help(&consider);
err.help(consider);
}
}
@ -2625,7 +2625,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
);
err.span_note(
sup_trace.cause.span,
&format!("...so that the {}", sup_trace.cause.as_requirement_str()),
format!("...so that the {}", sup_trace.cause.as_requirement_str()),
);
err.note_expected_found(&"", sup_expected, &"", sup_found);

View file

@ -299,7 +299,7 @@ pub fn suggest_new_region_bound(
if let Some(explicit_static) = &explicit_static {
err.span_suggestion_verbose(
span,
&format!("{consider} `{ty}`'s {explicit_static}"),
format!("{consider} `{ty}`'s {explicit_static}"),
&lifetime_name,
Applicability::MaybeIncorrect,
);
@ -367,7 +367,7 @@ pub fn suggest_new_region_bound(
spans_suggs
.push((fn_return.span.shrink_to_hi(), format!(" + {name} ")));
err.multipart_suggestion_verbose(
&format!(
format!(
"{declare} `{ty}` {captures}, {use_lt}",
),
spans_suggs,
@ -376,7 +376,7 @@ pub fn suggest_new_region_bound(
} else {
err.span_suggestion_verbose(
fn_return.span.shrink_to_hi(),
&format!("{declare} `{ty}` {captures}, {explicit}",),
format!("{declare} `{ty}` {captures}, {explicit}",),
&plus_lt,
Applicability::MaybeIncorrect,
);
@ -387,7 +387,7 @@ pub fn suggest_new_region_bound(
if let LifetimeName::ImplicitObjectLifetimeDefault = lt.res {
err.span_suggestion_verbose(
fn_return.span.shrink_to_hi(),
&format!(
format!(
"{declare} the trait object {captures}, {explicit}",
declare = declare,
captures = captures,
@ -404,7 +404,7 @@ pub fn suggest_new_region_bound(
if let Some(explicit_static) = &explicit_static {
err.span_suggestion_verbose(
lt.ident.span,
&format!("{} the trait object's {}", consider, explicit_static),
format!("{} the trait object's {}", consider, explicit_static),
&lifetime_name,
Applicability::MaybeIncorrect,
);

View file

@ -209,7 +209,7 @@ impl<T> Trait<T> for X {
if !sp.contains(p_span) {
diag.span_label(p_span, "this type parameter");
}
diag.help(&format!(
diag.help(format!(
"every closure has a distinct type and so could not always match the \
caller-chosen type of parameter `{}`",
p
@ -248,7 +248,7 @@ impl<T> Trait<T> for X {
proj_ty,
values.expected,
)) {
diag.help(&msg);
diag.help(msg);
diag.note(
"for more information, visit \
https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
@ -415,12 +415,12 @@ impl<T> Trait<T> for X {
if !impl_comparison {
// Generic suggestion when we can't be more specific.
if callable_scope {
diag.help(&format!(
diag.help(format!(
"{} or calling a method that returns `{}`",
msg, values.expected
));
} else {
diag.help(&msg);
diag.help(msg);
}
diag.note(
"for more information, visit \
@ -536,7 +536,7 @@ fn foo(&self) -> Self::T { String::new() }
for (sp, label) in methods.into_iter() {
span.push_span_label(sp, label);
}
diag.span_help(span, &msg);
diag.span_help(span, msg);
return true;
}
false

View file

@ -824,7 +824,7 @@ impl<'cx, 'tcx> LexicalResolver<'cx, 'tcx> {
// resolution errors here; delay ICE in favor of those errors.
self.tcx().sess.delay_span_bug(
self.var_infos[node_idx].origin.span(),
&format!(
format!(
"collect_error_for_expanding_node() could not find \
error for var {:?} in universe {:?}, lower_bounds={:#?}, \
upper_bounds={:#?}",

View file

@ -42,7 +42,7 @@ impl<'tcx> Drop for OpaqueTypeStorage<'tcx> {
fn drop(&mut self) {
if !self.opaque_types.is_empty() {
ty::tls::with(|tcx| {
tcx.sess.delay_span_bug(DUMMY_SP, &format!("{:?}", self.opaque_types))
tcx.sess.delay_span_bug(DUMMY_SP, format!("{:?}", self.opaque_types))
});
}
}

View file

@ -256,7 +256,7 @@ where
// this point it never will be
self.tcx.sess.delay_span_bug(
origin.span(),
&format!("unresolved inference variable in outlives: {:?}", v),
format!("unresolved inference variable in outlives: {:?}", v),
);
}
}

View file

@ -179,7 +179,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
// this point it never will be
self.tcx.sess.delay_span_bug(
rustc_span::DUMMY_SP,
&format!("unresolved inference variable in outlives: {:?}", v),
format!("unresolved inference variable in outlives: {:?}", v),
);
// add a bound that never holds
VerifyBound::AnyBound(vec![])

View file

@ -73,7 +73,7 @@ pub fn report_object_safety_error<'tcx>(
format!("...because {}", violation.error_msg())
};
if spans.is_empty() {
err.note(&msg);
err.note(msg);
} else {
for span in spans {
multi_span.push(span);