Skip to content

Commit

Permalink
Parse unsafe attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Oct 20, 2024
1 parent e011ba7 commit e6be383
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,7 @@ pub(crate) mod parsing {
use crate::parse::{Parse, ParseStream};
use crate::path::Path;
use crate::{mac, token};
use proc_macro2::Ident;
use std::fmt::{self, Display};

pub(crate) fn parse_inner(input: ParseStream, attrs: &mut Vec<Attribute>) -> Result<()> {
Expand Down Expand Up @@ -685,27 +686,38 @@ pub(crate) mod parsing {
#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for Meta {
fn parse(input: ParseStream) -> Result<Self> {
let path = input.call(Path::parse_mod_style)?;
let path = parse_outermost_meta_path(input)?;
parse_meta_after_path(path, input)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for MetaList {
fn parse(input: ParseStream) -> Result<Self> {
let path = input.call(Path::parse_mod_style)?;
let path = parse_outermost_meta_path(input)?;
parse_meta_list_after_path(path, input)
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for MetaNameValue {
fn parse(input: ParseStream) -> Result<Self> {
let path = input.call(Path::parse_mod_style)?;
let path = parse_outermost_meta_path(input)?;
parse_meta_name_value_after_path(path, input)
}
}

// Unlike meta::parse_meta_path which accepts arbitrary keywords in the path,
// only the `unsafe` keyword is accepted as an attribute's outermost path.
fn parse_outermost_meta_path(input: ParseStream) -> Result<Path> {
if input.peek(Token![unsafe]) {
let unsafe_token: Token![unsafe] = input.parse()?;
Ok(Path::from(Ident::new("unsafe", unsafe_token.span)))
} else {
Path::parse_mod_style(input)
}
}

pub(crate) fn parse_meta_after_path(path: Path, input: ParseStream) -> Result<Meta> {
if input.peek(token::Paren) || input.peek(token::Bracket) || input.peek(token::Brace) {
parse_meta_list_after_path(path, input).map(Meta::List)
Expand Down

0 comments on commit e6be383

Please sign in to comment.