Use Cow
in {D,Subd}iagnosticMessage
.
Each of `{D,Subd}iagnosticMessage::{Str,Eager}` has a comment: ``` // FIXME(davidtwco): can a `Cow<'static, str>` be used here? ``` This commit answers that question in the affirmative. It's not the most compelling change ever, but it might be worth merging. This requires changing the `impl<'a> From<&'a str>` impls to `impl From<&'static str>`, which involves a bunch of knock-on changes that require/result in call sites being a little more precise about exactly what kind of string they use to create errors, and not just `&str`. This will result in fewer unnecessary allocations, though this will not have any notable perf effects given that these are error paths. Note that I was lazy within Clippy, using `to_string` in a few places to preserve the existing string imprecision. I could have used `impl Into<{D,Subd}iagnosticMessage>` in various places as is done in the compiler, but that would have required changes to *many* call sites (mostly changing `&format("...")` to `format!("...")`) which didn't seem worthwhile.
This commit is contained in:
parent
1c53407e8c
commit
781111ef35
45 changed files with 308 additions and 287 deletions
|
@ -263,8 +263,7 @@ type FluentId = Cow<'static, str>;
|
|||
#[rustc_diagnostic_item = "SubdiagnosticMessage"]
|
||||
pub enum SubdiagnosticMessage {
|
||||
/// Non-translatable diagnostic message.
|
||||
// FIXME(davidtwco): can a `Cow<'static, str>` be used here?
|
||||
Str(String),
|
||||
Str(Cow<'static, str>),
|
||||
/// Translatable message which has already been translated eagerly.
|
||||
///
|
||||
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
|
||||
|
@ -275,8 +274,7 @@ pub enum SubdiagnosticMessage {
|
|||
/// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
|
||||
/// happening immediately after the subdiagnostic derive's logic has been run. This variant
|
||||
/// stores messages which have been translated eagerly.
|
||||
// FIXME(#100717): can a `Cow<'static, str>` be used here?
|
||||
Eager(String),
|
||||
Eager(Cow<'static, str>),
|
||||
/// Identifier of a Fluent message. Instances of this variant are generated by the
|
||||
/// `Subdiagnostic` derive.
|
||||
FluentIdentifier(FluentId),
|
||||
|
@ -290,17 +288,17 @@ pub enum SubdiagnosticMessage {
|
|||
|
||||
impl From<String> for SubdiagnosticMessage {
|
||||
fn from(s: String) -> Self {
|
||||
SubdiagnosticMessage::Str(s)
|
||||
SubdiagnosticMessage::Str(Cow::Owned(s))
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a str> for SubdiagnosticMessage {
|
||||
fn from(s: &'a str) -> Self {
|
||||
SubdiagnosticMessage::Str(s.to_string())
|
||||
impl From<&'static str> for SubdiagnosticMessage {
|
||||
fn from(s: &'static str) -> Self {
|
||||
SubdiagnosticMessage::Str(Cow::Borrowed(s))
|
||||
}
|
||||
}
|
||||
impl From<Cow<'static, str>> for SubdiagnosticMessage {
|
||||
fn from(s: Cow<'static, str>) -> Self {
|
||||
SubdiagnosticMessage::Str(s.to_string())
|
||||
SubdiagnosticMessage::Str(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -312,8 +310,7 @@ impl From<Cow<'static, str>> for SubdiagnosticMessage {
|
|||
#[rustc_diagnostic_item = "DiagnosticMessage"]
|
||||
pub enum DiagnosticMessage {
|
||||
/// Non-translatable diagnostic message.
|
||||
// FIXME(#100717): can a `Cow<'static, str>` be used here?
|
||||
Str(String),
|
||||
Str(Cow<'static, str>),
|
||||
/// Translatable message which has already been translated eagerly.
|
||||
///
|
||||
/// Some diagnostics have repeated subdiagnostics where the same interpolated variables would
|
||||
|
@ -324,8 +321,7 @@ pub enum DiagnosticMessage {
|
|||
/// incorrect diagnostics. Eager translation results in translation for a subdiagnostic
|
||||
/// happening immediately after the subdiagnostic derive's logic has been run. This variant
|
||||
/// stores messages which have been translated eagerly.
|
||||
// FIXME(#100717): can a `Cow<'static, str>` be used here?
|
||||
Eager(String),
|
||||
Eager(Cow<'static, str>),
|
||||
/// Identifier for a Fluent message (with optional attribute) corresponding to the diagnostic
|
||||
/// message.
|
||||
///
|
||||
|
@ -363,17 +359,17 @@ impl DiagnosticMessage {
|
|||
|
||||
impl From<String> for DiagnosticMessage {
|
||||
fn from(s: String) -> Self {
|
||||
DiagnosticMessage::Str(s)
|
||||
DiagnosticMessage::Str(Cow::Owned(s))
|
||||
}
|
||||
}
|
||||
impl<'a> From<&'a str> for DiagnosticMessage {
|
||||
fn from(s: &'a str) -> Self {
|
||||
DiagnosticMessage::Str(s.to_string())
|
||||
impl From<&'static str> for DiagnosticMessage {
|
||||
fn from(s: &'static str) -> Self {
|
||||
DiagnosticMessage::Str(Cow::Borrowed(s))
|
||||
}
|
||||
}
|
||||
impl From<Cow<'static, str>> for DiagnosticMessage {
|
||||
fn from(s: Cow<'static, str>) -> Self {
|
||||
DiagnosticMessage::Str(s.to_string())
|
||||
DiagnosticMessage::Str(s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue