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 inner attributes in traits #803

Merged
merged 1 commit into from
May 19, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ pub mod parsing {

impl Parse for ItemTrait {
fn parse(input: ParseStream) -> Result<Self> {
let attrs = input.call(Attribute::parse_outer)?;
let outer_attrs = input.call(Attribute::parse_outer)?;
let vis: Visibility = input.parse()?;
let unsafety: Option<Token![unsafe]> = input.parse()?;
let auto_token: Option<Token![auto]> = input.parse()?;
Expand All @@ -2092,7 +2092,7 @@ pub mod parsing {
let generics: Generics = input.parse()?;
parse_rest_of_trait(
input,
attrs,
outer_attrs,
vis,
unsafety,
auto_token,
Expand All @@ -2105,7 +2105,7 @@ pub mod parsing {

fn parse_rest_of_trait(
input: ParseStream,
attrs: Vec<Attribute>,
outer_attrs: Vec<Attribute>,
vis: Visibility,
unsafety: Option<Token![unsafe]>,
auto_token: Option<Token![auto]>,
Expand Down Expand Up @@ -2133,13 +2133,14 @@ pub mod parsing {

let content;
let brace_token = braced!(content in input);
let inner_attrs = content.call(Attribute::parse_inner)?;
let mut items = Vec::new();
while !content.is_empty() {
items.push(content.parse()?);
}

Ok(ItemTrait {
attrs,
attrs: private::attrs(outer_attrs, inner_attrs),
vis,
unsafety,
auto_token,
Expand Down
36 changes: 36 additions & 0 deletions tests/test_trait_inner_attr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#[macro_use]
mod macros;

use quote::quote;
use syn::ItemTrait;

#[test]
fn test_trait_inner_attr() {
let input = quote! {
trait Foo {
#![allow(bar)]
}
};

snapshot!(input as ItemTrait, @r###"
ItemTrait {
attrs: [
Attribute {
style: Inner,
path: Path {
segments: [
PathSegment {
ident: "allow",
arguments: None,
},
],
},
tokens: `( bar )`,
},
],
vis: Inherited,
ident: "Foo",
generics: Generics,
}
"###);
}