rust/compiler/rustc_hir_analysis/src
Nicholas Nethercote 6b62f37402 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.
2023-05-03 08:44:39 +10:00
..
astconv Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
check Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
coherence Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
collect Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
impl_wf_check Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
outlives Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
structured_errors Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
variance Rollup merge of #110556 - kylematsuda:earlybinder-explicit-item-bounds, r=compiler-errors 2023-04-25 21:06:32 +02:00
autoderef.rs Remove type-traversal trait aliases 2023-02-22 17:04:58 +00:00
bounds.rs Fix typos in compiler 2023-04-10 22:02:52 +02:00
check_unused.rs Remove last instances of HashSet in query result types. 2023-03-01 10:20:45 +01:00
collect.rs Make some region folders a little stricter. 2023-04-26 10:14:16 +10:00
constrained_generic_params.rs Remove type-traversal trait aliases 2023-02-22 17:04:58 +00:00
errors.rs Rollup merge of #108161 - WaffleLapkin:const_param_ty, r=BoxyUwU 2023-05-02 11:44:50 +05:30
hir_wf_check.rs Auto merge of #109119 - lcnr:trait-system-cleanup, r=compiler-errors 2023-03-22 05:33:18 +00:00
impl_wf_check.rs Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
lib.rs Do not consider associated type bounds for super_predicates_that_define_assoc_type 2023-05-01 15:45:28 +00:00
structured_errors.rs rustc_typeck to rustc_hir_analysis 2022-09-27 10:37:23 +02:00