Skip to content

Commit 334bf8d

Browse files
authored
Merge pull request #1404 from dtolnay/fastattr
Optimize attribute parsing for the overwhelmingly common case
2 parents 615d66c + 6a4e49d commit 334bf8d

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

src/attr.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ impl<'a> FilterAttrs<'a> for &'a [Attribute] {
535535
#[cfg(feature = "parsing")]
536536
pub(crate) mod parsing {
537537
use super::*;
538+
use crate::parse::discouraged::Speculative;
538539
use crate::parse::{Parse, ParseStream, Result};
539540

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

610611
fn parse_meta_name_value_after_path(path: Path, input: ParseStream) -> Result<MetaNameValue> {
611612
let eq_token: Token![=] = input.parse()?;
612-
if input.peek(Token![#]) && input.peek2(token::Bracket) {
613+
let ahead = input.fork();
614+
let lit: Option<Lit> = ahead.parse()?;
615+
let value = if let (Some(lit), true) = (lit, ahead.is_empty()) {
616+
input.advance_to(&ahead);
617+
Expr::Lit(ExprLit {
618+
attrs: Vec::new(),
619+
lit,
620+
})
621+
} else if input.peek(Token![#]) && input.peek2(token::Bracket) {
613622
return Err(input.error("unexpected attribute inside of attribute"));
614-
}
615-
let value: Expr = input.parse()?;
623+
} else {
624+
input.parse()?
625+
};
616626
Ok(MetaNameValue {
617627
path,
618628
eq_token,

0 commit comments

Comments
 (0)