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

@ -1146,7 +1146,7 @@ impl<'a> ExtCtxt<'a> {
for (span, notes) in self.expansions.iter() {
let mut db = self.sess.parse_sess.create_note(errors::TraceMacro { span: *span });
for note in notes {
db.note(note);
db.note(note.clone());
}
db.emit();
}

View file

@ -797,7 +797,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
&self.cx.sess.parse_sess,
sym::proc_macro_hygiene,
span,
&format!("custom attributes cannot be applied to {}", kind),
format!("custom attributes cannot be applied to {}", kind),
)
.emit();
}

View file

@ -474,7 +474,7 @@ pub fn compile_declarative_macro(
let s = parse_failure_msg(&token);
let sp = token.span.substitute_dummy(def.span);
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, &s);
let mut err = sess.parse_sess.span_diagnostic.struct_span_err(sp, s);
err.span_label(sp, msg);
annotate_doc_comment(&mut err, sess.source_map(), sp);
err.emit();
@ -483,7 +483,7 @@ pub fn compile_declarative_macro(
Error(sp, msg) => {
sess.parse_sess
.span_diagnostic
.struct_span_err(sp.substitute_dummy(def.span), &msg)
.struct_span_err(sp.substitute_dummy(def.span), msg)
.emit();
return dummy_syn_ext();
}
@ -555,7 +555,7 @@ pub fn compile_declarative_macro(
let (transparency, transparency_error) = attr::find_transparency(&def.attrs, macro_rules);
match transparency_error {
Some(TransparencyError::UnknownTransparency(value, span)) => {
diag.span_err(span, &format!("unknown macro transparency: `{}`", value));
diag.span_err(span, format!("unknown macro transparency: `{}`", value));
}
Some(TransparencyError::MultipleTransparencyAttrs(old_span, new_span)) => {
diag.span_err(vec![old_span, new_span], "multiple macro transparency attributes");
@ -1164,7 +1164,7 @@ fn check_matcher_core<'tt>(
let sp = next_token.span();
let mut err = sess.span_diagnostic.struct_span_err(
sp,
&format!(
format!(
"`${name}:{frag}` {may_be} followed by `{next}`, which \
is not allowed for `{frag}` fragments",
name = name,
@ -1196,13 +1196,13 @@ fn check_matcher_core<'tt>(
match possible {
&[] => {}
&[t] => {
err.note(&format!(
err.note(format!(
"only {} is allowed after `{}` fragments",
t, kind,
));
}
ts => {
err.note(&format!(
err.note(format!(
"{}{} or {}",
msg,
ts[..ts.len() - 1].to_vec().join(", "),

View file

@ -78,7 +78,7 @@ fn check_trailing_token<'sess>(
if let Some(tt) = iter.next() {
let mut diag = sess
.span_diagnostic
.struct_span_err(tt.span(), &format!("unexpected token: {}", pprust::tt_to_string(tt)));
.struct_span_err(tt.span(), format!("unexpected token: {}", pprust::tt_to_string(tt)));
diag.span_note(tt.span(), "meta-variable expression must not have trailing tokens");
Err(diag)
} else {
@ -137,11 +137,11 @@ fn parse_ident<'sess>(
let token_str = pprust::token_to_string(token);
let mut err = sess.span_diagnostic.struct_span_err(
span,
&format!("expected identifier, found `{}`", &token_str)
format!("expected identifier, found `{}`", &token_str)
);
err.span_suggestion(
token.span,
&format!("try removing `{}`", &token_str),
format!("try removing `{}`", &token_str),
"",
Applicability::MaybeIncorrect,
);

View file

@ -85,7 +85,7 @@ pub(super) fn parse(
frag.name
);
sess.span_diagnostic
.struct_span_err(span, &msg)
.struct_span_err(span, msg)
.help(VALID_FRAGMENT_NAMES_MSG)
.emit();
token::NonterminalKind::Ident
@ -195,7 +195,7 @@ fn parse_tree(
_ => {
let tok = pprust::token_kind_to_string(&token::OpenDelim(delim));
let msg = format!("expected `(` or `{{`, found `{}`", tok);
sess.span_diagnostic.span_err(delim_span.entire(), &msg);
sess.span_diagnostic.span_err(delim_span.entire(), msg);
}
}
}
@ -246,7 +246,7 @@ fn parse_tree(
"expected identifier, found `{}`",
pprust::token_to_string(&token),
);
sess.span_diagnostic.span_err(token.span, &msg);
sess.span_diagnostic.span_err(token.span, msg);
TokenTree::MetaVar(token.span, Ident::empty())
}
@ -358,7 +358,7 @@ fn parse_sep_and_kleene_op(
// For example, `macro_rules! foo { ( ${length()} ) => {} }`
fn span_dollar_dollar_or_metavar_in_the_lhs_err(sess: &ParseSess, token: &Token) {
sess.span_diagnostic
.span_err(token.span, &format!("unexpected token: {}", pprust::token_to_string(token)));
.span_err(token.span, format!("unexpected token: {}", pprust::token_to_string(token)));
sess.span_diagnostic.span_note_without_error(
token.span,
"`$$` and meta-variable expressions are not allowed inside macro parameter definitions",

View file

@ -95,7 +95,7 @@ impl base::AttrProcMacro for AttrProcMacro {
|e| {
let mut err = ecx.struct_span_err(span, "custom attribute panicked");
if let Some(s) = e.as_str() {
err.help(&format!("message: {}", s));
err.help(format!("message: {}", s));
}
err.emit()
},
@ -148,7 +148,7 @@ impl MultiItemModifier for DeriveProcMacro {
Err(e) => {
let mut err = ecx.struct_span_err(span, "proc-macro derive panicked");
if let Some(s) = e.as_str() {
err.help(&format!("message: {}", s));
err.help(format!("message: {}", s));
}
err.emit();
return ExpandResult::Ready(vec![]);