rust/compiler/rustc_ty_utils/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
..
abi.rs Support TLS access into dylibs on Windows 2023-03-29 08:55:21 +02:00
assoc.rs Walk return-position impl trait in trait deeply in associated_item_def_ids 2023-03-29 11:19:48 -03:00
common_traits.rs remove some trait solver helpers 2023-03-21 09:57:20 +01:00
consts.rs Auto merge of #106934 - DrMeepster:offset_of, r=WaffleLapkin 2023-04-22 00:10:44 +00:00
errors.rs Fix typos in compiler 2023-04-10 22:02:52 +02:00
implied_bounds.rs rustc_middle: Remove trait DefIdTree 2023-03-02 23:46:44 +04:00
instance.rs Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
layout_sanity_check.rs Spelling - compiler 2023-04-17 16:09:18 -04:00
layout.rs Split {Idx, IndexVec, IndexSlice} into their own modules 2023-04-24 13:53:35 +00:00
lib.rs Add rustc_fluent_macro to decouple fluent from rustc_macros 2023-04-18 18:56:22 +00:00
needs_drop.rs Restrict From<S> for {D,Subd}iagnosticMessage. 2023-05-03 08:44:39 +10:00
representability.rs Use local key in providers 2023-03-21 15:38:51 +00:00
structural_match.rs fix clippy::toplevel_ref_arg and ::manual_map 2023-04-16 13:28:13 +02:00
ty.rs Update ty::VariantDef to use IndexVec<FieldIdx, FieldDef> 2023-03-30 09:23:40 -07:00