summaryrefslogtreecommitdiffstats
path: root/third_party/rust/thiserror-impl/src/attr.rs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 01:14:29 +0000
commitfbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8 (patch)
tree4c1ccaf5486d4f2009f9a338a98a83e886e29c97 /third_party/rust/thiserror-impl/src/attr.rs
parentReleasing progress-linux version 124.0.1-1~progress7.99u1. (diff)
downloadfirefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.tar.xz
firefox-fbaf0bb26397aa498eb9156f06d5a6fe34dd7dd8.zip
Merging upstream version 125.0.1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/thiserror-impl/src/attr.rs')
-rw-r--r--third_party/rust/thiserror-impl/src/attr.rs34
1 files changed, 30 insertions, 4 deletions
diff --git a/third_party/rust/thiserror-impl/src/attr.rs b/third_party/rust/thiserror-impl/src/attr.rs
index 4beb8c9628..269c69eca5 100644
--- a/third_party/rust/thiserror-impl/src/attr.rs
+++ b/third_party/rust/thiserror-impl/src/attr.rs
@@ -1,6 +1,7 @@
use proc_macro2::{Delimiter, Group, Span, TokenStream, TokenTree};
use quote::{format_ident, quote, ToTokens};
use std::collections::BTreeSet as Set;
+use syn::parse::discouraged::Speculative;
use syn::parse::ParseStream;
use syn::{
braced, bracketed, parenthesized, token, Attribute, Error, Ident, Index, LitInt, LitStr, Meta,
@@ -20,6 +21,7 @@ pub struct Display<'a> {
pub original: &'a Attribute,
pub fmt: LitStr,
pub args: TokenStream,
+ pub requires_fmt_machinery: bool,
pub has_bonus_display: bool,
pub implied_bounds: Set<(usize, Trait)>,
}
@@ -103,10 +105,24 @@ fn parse_error_attribute<'a>(attrs: &mut Attrs<'a>, attr: &'a Attribute) -> Resu
return Ok(());
}
+ let fmt: LitStr = input.parse()?;
+
+ let ahead = input.fork();
+ ahead.parse::<Option<Token![,]>>()?;
+ let args = if ahead.is_empty() {
+ input.advance_to(&ahead);
+ TokenStream::new()
+ } else {
+ parse_token_expr(input, false)?
+ };
+
+ let requires_fmt_machinery = !args.is_empty();
+
let display = Display {
original: attr,
- fmt: input.parse()?,
- args: parse_token_expr(input, false)?,
+ fmt,
+ args,
+ requires_fmt_machinery,
has_bonus_display: false,
implied_bounds: Set::new(),
};
@@ -196,8 +212,18 @@ impl ToTokens for Display<'_> {
fn to_tokens(&self, tokens: &mut TokenStream) {
let fmt = &self.fmt;
let args = &self.args;
- tokens.extend(quote! {
- ::core::write!(__formatter, #fmt #args)
+
+ // Currently `write!(f, "text")` produces less efficient code than
+ // `f.write_str("text")`. We recognize the case when the format string
+ // has no braces and no interpolated values, and generate simpler code.
+ tokens.extend(if self.requires_fmt_machinery {
+ quote! {
+ ::core::write!(__formatter, #fmt #args)
+ }
+ } else {
+ quote! {
+ __formatter.write_str(#fmt)
+ }
});
}
}