about summary refs log tree commit diff
path: root/compiler/rustc_builtin_macros/src
diff options
context:
space:
mode:
authorNicholas Nethercote <n.nethercote@gmail.com>2023-04-20 13:26:58 +1000
committerNicholas Nethercote <n.nethercote@gmail.com>2023-05-03 08:44:39 +1000
commit6b62f37402cb2990c7d350379238579af0360b10 (patch)
tree64a4f63d5bdb61fd2d02c675d17ce9444ef0669b /compiler/rustc_builtin_macros/src
parenta368898de758e1b8def6c9060044a5b40eb79e84 (diff)
downloadrust-6b62f37402cb2990c7d350379238579af0360b10.tar.gz
rust-6b62f37402cb2990c7d350379238579af0360b10.zip
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.
Diffstat (limited to 'compiler/rustc_builtin_macros/src')
-rw-r--r--compiler/rustc_builtin_macros/src/asm.rs8
-rw-r--r--compiler/rustc_builtin_macros/src/format.rs8
-rw-r--r--compiler/rustc_builtin_macros/src/proc_macro_harness.rs6
-rw-r--r--compiler/rustc_builtin_macros/src/source_util.rs2
4 files changed, 12 insertions, 12 deletions
diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs
index 0ea8454db08..ac817d9a152 100644
--- a/compiler/rustc_builtin_macros/src/asm.rs
+++ b/compiler/rustc_builtin_macros/src/asm.rs
@@ -553,7 +553,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
             let mut e = ecx.struct_span_err(err_sp, msg);
             e.span_label(err_sp, err.label + " in asm template string");
             if let Some(note) = err.note {
-                e.note(&note);
+                e.note(note);
             }
             if let Some((label, span)) = err.secondary_label {
                 let err_sp = template_span.from_inner(InnerSpan::new(span.start, span.end));
@@ -600,7 +600,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
                                     1 => format!("there is 1 {}argument", positional),
                                     x => format!("there are {} {}arguments", x, positional),
                                 };
-                                err.note(&msg);
+                                err.note(msg);
 
                                 if named_pos.contains_key(&idx) {
                                     err.span_label(args.operands[idx].1, "named argument");
@@ -703,7 +703,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
             let (sp, msg) = unused_operands.into_iter().next().unwrap();
             let mut err = ecx.struct_span_err(sp, msg);
             err.span_label(sp, msg);
-            err.help(&format!(
+            err.help(format!(
                 "if this argument is intentionally unused, \
                  consider using it in an asm comment: `\"/*{} */\"`",
                 help_str
@@ -718,7 +718,7 @@ fn expand_preparsed_asm(ecx: &mut ExtCtxt<'_>, args: AsmArgs) -> Option<ast::Inl
             for (sp, msg) in unused_operands {
                 err.span_label(sp, msg);
             }
-            err.help(&format!(
+            err.help(format!(
                 "if these arguments are intentionally unused, \
                  consider using them in an asm comment: `\"/*{} */\"`",
                 help_str
diff --git a/compiler/rustc_builtin_macros/src/format.rs b/compiler/rustc_builtin_macros/src/format.rs
index f17df5b0a83..adc12ec84f5 100644
--- a/compiler/rustc_builtin_macros/src/format.rs
+++ b/compiler/rustc_builtin_macros/src/format.rs
@@ -616,14 +616,14 @@ fn report_missing_placeholders(
                         } else {
                             diag.span_note(
                                 sp,
-                                &format!("format specifiers use curly braces, and {}", trn),
+                                format!("format specifiers use curly braces, and {}", trn),
                             );
                         }
                     } else {
                         if success {
-                            diag.help(&format!("`{}` should be written as `{}`", sub, trn));
+                            diag.help(format!("`{}` should be written as `{}`", sub, trn));
                         } else {
-                            diag.note(&format!("`{}` should use curly braces, and {}", sub, trn));
+                            diag.note(format!("`{}` should use curly braces, and {}", sub, trn));
                         }
                     }
                 }
@@ -777,7 +777,7 @@ fn report_invalid_references(
                 has_precision_star = true;
                 e.span_label(
                     *span,
-                    &format!(
+                    format!(
                         "this precision flag adds an extra required argument at position {}, which is why there {} expected",
                         index,
                         if num_placeholders == 1 {
diff --git a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
index 378d5f39f4a..52b5601bb11 100644
--- a/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
+++ b/compiler/rustc_builtin_macros/src/proc_macro_harness.rs
@@ -194,7 +194,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
                     };
 
                     self.handler
-                        .struct_span_err(attr.span, &msg)
+                        .struct_span_err(attr.span, msg)
                         .span_label(prev_attr.span, "previous attribute here")
                         .emit();
 
@@ -219,7 +219,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
                 pprust::path_to_string(&attr.get_normal_item().path),
             );
 
-            self.handler.span_err(attr.span, &msg);
+            self.handler.span_err(attr.span, msg);
             return;
         }
 
@@ -233,7 +233,7 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> {
                 pprust::path_to_string(&attr.get_normal_item().path),
             );
 
-            self.handler.span_err(attr.span, &msg);
+            self.handler.span_err(attr.span, msg);
             return;
         }
 
diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs
index 0b17e92efe9..b8a24f1102d 100644
--- a/compiler/rustc_builtin_macros/src/source_util.rs
+++ b/compiler/rustc_builtin_macros/src/source_util.rs
@@ -150,7 +150,7 @@ pub fn expand_include<'cx>(
                         if self.p.token != token::Eof {
                             let token = pprust::token_to_string(&self.p.token);
                             let msg = format!("expected item, found `{}`", token);
-                            self.p.struct_span_err(self.p.token.span, &msg).emit();
+                            self.p.struct_span_err(self.p.token.span, msg).emit();
                         }
 
                         break;