Skip to content

Commit

Permalink
Optimize attribute parsing for the overwhelmingly common case
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Mar 16, 2023
1 parent 615d66c commit 6a4e49d
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/attr.rs
Expand Up @@ -535,6 +535,7 @@ impl<'a> FilterAttrs<'a> for &'a [Attribute] {
#[cfg(feature = "parsing")]
pub(crate) mod parsing {
use super::*;
use crate::parse::discouraged::Speculative;
use crate::parse::{Parse, ParseStream, Result};

pub(crate) fn parse_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
Expand Down Expand Up @@ -609,10 +610,19 @@ pub(crate) mod parsing {

fn parse_meta_name_value_after_path(path: Path, input: ParseStream) -> Result<MetaNameValue> {
let eq_token: Token![=] = input.parse()?;
if input.peek(Token![#]) && input.peek2(token::Bracket) {
let ahead = input.fork();
let lit: Option<Lit> = ahead.parse()?;
let value = if let (Some(lit), true) = (lit, ahead.is_empty()) {
input.advance_to(&ahead);
Expr::Lit(ExprLit {
attrs: Vec::new(),
lit,
})
} else if input.peek(Token![#]) && input.peek2(token::Bracket) {
return Err(input.error("unexpected attribute inside of attribute"));
}
let value: Expr = input.parse()?;
} else {
input.parse()?
};
Ok(MetaNameValue {
path,
eq_token,
Expand Down

0 comments on commit 6a4e49d

Please sign in to comment.