Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse unsafe attributes #1759

Merged
merged 1 commit into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading