Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6b62f37

Browse files
committedMay 2, 2023
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.
1 parent a368898 commit 6b62f37

File tree

177 files changed

+791
-787
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

177 files changed

+791
-787
lines changed
 

‎compiler/rustc_ast_lowering/src/path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
136136

137137
self.diagnostic().span_bug(
138138
p.span,
139-
&format!(
139+
format!(
140140
"lower_qpath: no final extension segment in {}..{}",
141141
proj_start,
142142
p.segments.len()

‎compiler/rustc_ast_passes/src/feature_gate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ impl<'a> PostExpansionVisitor<'a> {
8383
&self,
8484
const_extern_fn,
8585
span,
86-
&format!("`{}` as a `const fn` ABI is unstable", abi)
86+
format!("`{}` as a `const fn` ABI is unstable", abi)
8787
),
8888
}
8989
}
@@ -104,7 +104,7 @@ impl<'a> PostExpansionVisitor<'a> {
104104
if self.sess.opts.pretty.map_or(true, |ppm| ppm.needs_hir()) {
105105
self.sess.parse_sess.span_diagnostic.delay_span_bug(
106106
span,
107-
&format!(
107+
format!(
108108
"unrecognized ABI not caught in lowering: {}",
109109
symbol_unescaped.as_str()
110110
),

0 commit comments

Comments
 (0)
Please sign in to comment.