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

@ -243,13 +243,13 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let note = format!("{title} is defined in an impl for the type `{impl_ty}`");
if let Some(span) = note_span {
err.span_note(span, &note);
err.span_note(span, note);
} else {
err.note(&note);
err.note(note);
}
}
if candidates.len() > limit {
err.note(&format!("and {} others", candidates.len() - limit));
err.note(format!("and {} others", candidates.len() - limit));
}
}
@ -303,7 +303,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
"associated type `{name}` not found for `{self_ty}` in the current scope"
);
err.span_label(name.span, format!("associated item not found in `{self_ty}`"));
err.note(&format!(
err.note(format!(
"the associated type was found for\n{type_candidates}{additional_types}",
));
add_def_label(&mut err);
@ -390,10 +390,10 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
let mut err = tcx.sess.struct_span_err(
name.span,
&format!("the associated type `{name}` exists for `{self_ty}`, but its trait bounds were not satisfied")
format!("the associated type `{name}` exists for `{self_ty}`, but its trait bounds were not satisfied")
);
if !bounds.is_empty() {
err.note(&format!(
err.note(format!(
"the following trait bounds were not satisfied:\n{}",
bounds.join("\n")
));
@ -409,7 +409,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
if !tcx.sess.source_map().is_span_accessible(span) {
continue;
}
err.span_label(span, &msg);
err.span_label(span, msg);
}
add_def_label(&mut err);
err.emit()
@ -589,7 +589,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
if !suggestions.is_empty() {
err.multipart_suggestion(
&format!("specify the associated type{}", pluralize!(types_count)),
format!("specify the associated type{}", pluralize!(types_count)),
suggestions,
Applicability::HasPlaceholders,
);