Skip to content

Commit

Permalink
Merge pull request #1759 from dtolnay/unsafeattr
Browse files Browse the repository at this point in the history
Parse unsafe attributes
  • Loading branch information
dtolnay authored Oct 20, 2024
2 parents e011ba7 + a45af00 commit 4c7f82e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 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
7 changes: 0 additions & 7 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,6 @@ static EXCLUDE_FILES: &[&str] = &[
"tests/rustdoc/unsafe-extern-blocks.rs",
"tests/ui/rust-2024/unsafe-extern-blocks/safe-items.rs",

// TODO: unsafe attributes: `#[unsafe(path::to)]`
// https://github.com/dtolnay/syn/issues/1710
"src/tools/rust-analyzer/crates/parser/test_data/parser/inline/ok/0213_metas.rs",
"src/tools/rustfmt/tests/target/unsafe_attributes.rs",
"tests/ui/attributes/unsafe/unsafe-attributes.rs",
"tests/ui/rust-2024/unsafe-attributes/unsafe-attribute-marked.rs",

// TODO: non-lifetime binders: `where for<'a, T> &'a Struct<T>: Trait`
// https://github.com/dtolnay/syn/issues/1435
"src/tools/rustfmt/tests/source/issue_5721.rs",
Expand Down

0 comments on commit 4c7f82e

Please sign in to comment.