Skip to content

Commit

Permalink
Merge pull request #1146 from dtolnay/innerattr
Browse files Browse the repository at this point in the history
Remove support for inner attributes on non-block expressions
  • Loading branch information
dtolnay committed Mar 16, 2022
2 parents fcaca36 + fe5508b commit be3238b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 66 deletions.
16 changes: 2 additions & 14 deletions src/data.rs
Expand Up @@ -246,12 +246,11 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for Variant {
fn parse(input: ParseStream) -> Result<Self> {
let mut attrs = input.call(Attribute::parse_outer)?;
let attrs = input.call(Attribute::parse_outer)?;
let _visibility: Visibility = input.parse()?;
let ident: Ident = input.parse()?;
let fields = if input.peek(token::Brace) {
let fields = parse_braced(input, &mut attrs)?;
Fields::Named(fields)
Fields::Named(input.parse()?)
} else if input.peek(token::Paren) {
Fields::Unnamed(input.parse()?)
} else {
Expand Down Expand Up @@ -295,17 +294,6 @@ pub mod parsing {
}
}

pub(crate) fn parse_braced(
input: ParseStream,
attrs: &mut Vec<Attribute>,
) -> Result<FieldsNamed> {
let content;
let brace_token = braced!(content in input);
attr::parsing::parse_inner(&content, attrs)?;
let named = content.parse_terminated(Field::parse_named)?;
Ok(FieldsNamed { brace_token, named })
}

impl Field {
/// Parses a named (braced struct) field.
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
Expand Down
20 changes: 7 additions & 13 deletions src/derive.rs
Expand Up @@ -95,15 +95,15 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for DeriveInput {
fn parse(input: ParseStream) -> Result<Self> {
let mut attrs = input.call(Attribute::parse_outer)?;
let attrs = input.call(Attribute::parse_outer)?;
let vis = input.parse::<Visibility>()?;

let lookahead = input.lookahead1();
if lookahead.peek(Token![struct]) {
let struct_token = input.parse::<Token![struct]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, fields, semi) = data_struct(input, &mut attrs)?;
let (where_clause, fields, semi) = data_struct(input)?;
Ok(DeriveInput {
attrs,
vis,
Expand All @@ -122,7 +122,7 @@ pub mod parsing {
let enum_token = input.parse::<Token![enum]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, brace, variants) = data_enum(input, &mut attrs)?;
let (where_clause, brace, variants) = data_enum(input)?;
Ok(DeriveInput {
attrs,
vis,
Expand All @@ -141,7 +141,7 @@ pub mod parsing {
let union_token = input.parse::<Token![union]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, fields) = data_union(input, &mut attrs)?;
let (where_clause, fields) = data_union(input)?;
Ok(DeriveInput {
attrs,
vis,
Expand All @@ -163,7 +163,6 @@ pub mod parsing {

pub fn data_struct(
input: ParseStream,
attrs: &mut Vec<Attribute>,
) -> Result<(Option<WhereClause>, Fields, Option<Token![;]>)> {
let mut lookahead = input.lookahead1();
let mut where_clause = None;
Expand All @@ -188,7 +187,7 @@ pub mod parsing {
Err(lookahead.error())
}
} else if lookahead.peek(token::Brace) {
let fields = data::parsing::parse_braced(input, attrs)?;
let fields = input.parse()?;
Ok((where_clause, Fields::Named(fields), None))
} else if lookahead.peek(Token![;]) {
let semi = input.parse()?;
Expand All @@ -200,7 +199,6 @@ pub mod parsing {

pub fn data_enum(
input: ParseStream,
attrs: &mut Vec<Attribute>,
) -> Result<(
Option<WhereClause>,
token::Brace,
Expand All @@ -210,18 +208,14 @@ pub mod parsing {

let content;
let brace = braced!(content in input);
attr::parsing::parse_inner(&content, attrs)?;
let variants = content.parse_terminated(Variant::parse)?;

Ok((where_clause, brace, variants))
}

pub fn data_union(
input: ParseStream,
attrs: &mut Vec<Attribute>,
) -> Result<(Option<WhereClause>, FieldsNamed)> {
pub fn data_union(input: ParseStream) -> Result<(Option<WhereClause>, FieldsNamed)> {
let where_clause = input.parse()?;
let fields = data::parsing::parse_braced(input, attrs)?;
let fields = input.parse()?;
Ok((where_clause, fields))
}
}
Expand Down
44 changes: 13 additions & 31 deletions src/expr.rs
Expand Up @@ -1863,8 +1863,7 @@ pub(crate) mod parsing {
}

if allow_struct.0 && input.peek(token::Brace) {
let outer_attrs = Vec::new();
let expr_struct = expr_struct_helper(input, outer_attrs, expr.path)?;
let expr_struct = expr_struct_helper(input, expr.path)?;
if expr.qself.is_some() {
Ok(Expr::Verbatim(verbatim::between(begin, input)))
} else {
Expand All @@ -1890,10 +1889,9 @@ pub(crate) mod parsing {
fn paren_or_tuple(input: ParseStream) -> Result<Expr> {
let content;
let paren_token = parenthesized!(content in input);
let inner_attrs = content.call(Attribute::parse_inner)?;
if content.is_empty() {
return Ok(Expr::Tuple(ExprTuple {
attrs: inner_attrs,
attrs: Vec::new(),
paren_token,
elems: Punctuated::new(),
}));
Expand All @@ -1902,7 +1900,7 @@ pub(crate) mod parsing {
let first: Expr = content.parse()?;
if content.is_empty() {
return Ok(Expr::Paren(ExprParen {
attrs: inner_attrs,
attrs: Vec::new(),
paren_token,
expr: Box::new(first),
}));
Expand All @@ -1920,7 +1918,7 @@ pub(crate) mod parsing {
elems.push_value(value);
}
Ok(Expr::Tuple(ExprTuple {
attrs: inner_attrs,
attrs: Vec::new(),
paren_token,
elems,
}))
Expand All @@ -1930,10 +1928,9 @@ pub(crate) mod parsing {
fn array_or_repeat(input: ParseStream) -> Result<Expr> {
let content;
let bracket_token = bracketed!(content in input);
let inner_attrs = content.call(Attribute::parse_inner)?;
if content.is_empty() {
return Ok(Expr::Array(ExprArray {
attrs: inner_attrs,
attrs: Vec::new(),
bracket_token,
elems: Punctuated::new(),
}));
Expand All @@ -1953,15 +1950,15 @@ pub(crate) mod parsing {
elems.push_value(value);
}
Ok(Expr::Array(ExprArray {
attrs: inner_attrs,
attrs: Vec::new(),
bracket_token,
elems,
}))
} else if content.peek(Token![;]) {
let semi_token: Token![;] = content.parse()?;
let len: Expr = content.parse()?;
Ok(Expr::Repeat(ExprRepeat {
attrs: inner_attrs,
attrs: Vec::new(),
bracket_token,
expr: Box::new(first),
semi_token,
Expand All @@ -1978,7 +1975,6 @@ pub(crate) mod parsing {
fn parse(input: ParseStream) -> Result<Self> {
let content;
let bracket_token = bracketed!(content in input);
let inner_attrs = content.call(Attribute::parse_inner)?;
let mut elems = Punctuated::new();

while !content.is_empty() {
Expand All @@ -1992,7 +1988,7 @@ pub(crate) mod parsing {
}

Ok(ExprArray {
attrs: inner_attrs,
attrs: Vec::new(),
bracket_token,
elems,
})
Expand All @@ -2006,7 +2002,7 @@ pub(crate) mod parsing {
let content;
Ok(ExprRepeat {
bracket_token: bracketed!(content in input),
attrs: content.call(Attribute::parse_inner)?,
attrs: Vec::new(),
expr: content.parse()?,
semi_token: content.parse()?,
len: content.parse()?,
Expand Down Expand Up @@ -2648,27 +2644,21 @@ pub(crate) mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ExprStruct {
fn parse(input: ParseStream) -> Result<Self> {
let attrs = Vec::new();
let path: Path = input.parse()?;
expr_struct_helper(input, attrs, path)
expr_struct_helper(input, path)
}
}

#[cfg(feature = "full")]
fn expr_struct_helper(
input: ParseStream,
mut attrs: Vec<Attribute>,
path: Path,
) -> Result<ExprStruct> {
fn expr_struct_helper(input: ParseStream, path: Path) -> Result<ExprStruct> {
let content;
let brace_token = braced!(content in input);
attr::parsing::parse_inner(&content, &mut attrs)?;

let mut fields = Punctuated::new();
while !content.is_empty() {
if content.peek(Token![..]) {
return Ok(ExprStruct {
attrs,
attrs: Vec::new(),
brace_token,
path,
fields,
Expand All @@ -2690,7 +2680,7 @@ pub(crate) mod parsing {
}

Ok(ExprStruct {
attrs,
attrs: Vec::new(),
brace_token,
path,
fields,
Expand Down Expand Up @@ -2957,9 +2947,6 @@ pub(crate) mod printing {
#[cfg(not(feature = "full"))]
pub(crate) fn outer_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}

#[cfg(not(feature = "full"))]
fn inner_attrs_to_tokens(_attrs: &[Attribute], _tokens: &mut TokenStream) {}

#[cfg(feature = "full")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "printing")))]
impl ToTokens for ExprBox {
Expand All @@ -2976,7 +2963,6 @@ pub(crate) mod printing {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.bracket_token.surround(tokens, |tokens| {
inner_attrs_to_tokens(&self.attrs, tokens);
self.elems.to_tokens(tokens);
});
}
Expand Down Expand Up @@ -3036,7 +3022,6 @@ pub(crate) mod printing {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.paren_token.surround(tokens, |tokens| {
inner_attrs_to_tokens(&self.attrs, tokens);
self.elems.to_tokens(tokens);
// If we only have one argument, we need a trailing comma to
// distinguish ExprTuple from ExprParen.
Expand Down Expand Up @@ -3433,7 +3418,6 @@ pub(crate) mod printing {
outer_attrs_to_tokens(&self.attrs, tokens);
self.path.to_tokens(tokens);
self.brace_token.surround(tokens, |tokens| {
inner_attrs_to_tokens(&self.attrs, tokens);
self.fields.to_tokens(tokens);
if let Some(dot2_token) = &self.dot2_token {
dot2_token.to_tokens(tokens);
Expand All @@ -3451,7 +3435,6 @@ pub(crate) mod printing {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.bracket_token.surround(tokens, |tokens| {
inner_attrs_to_tokens(&self.attrs, tokens);
self.expr.to_tokens(tokens);
self.semi_token.to_tokens(tokens);
self.len.to_tokens(tokens);
Expand All @@ -3475,7 +3458,6 @@ pub(crate) mod printing {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
self.paren_token.surround(tokens, |tokens| {
inner_attrs_to_tokens(&self.attrs, tokens);
self.expr.to_tokens(tokens);
});
}
Expand Down
14 changes: 6 additions & 8 deletions src/item.rs
Expand Up @@ -1987,13 +1987,12 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ItemStruct {
fn parse(input: ParseStream) -> Result<Self> {
let mut attrs = input.call(Attribute::parse_outer)?;
let attrs = input.call(Attribute::parse_outer)?;
let vis = input.parse::<Visibility>()?;
let struct_token = input.parse::<Token![struct]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, fields, semi_token) =
derive::parsing::data_struct(input, &mut attrs)?;
let (where_clause, fields, semi_token) = derive::parsing::data_struct(input)?;
Ok(ItemStruct {
attrs,
vis,
Expand All @@ -2012,13 +2011,12 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ItemEnum {
fn parse(input: ParseStream) -> Result<Self> {
let mut attrs = input.call(Attribute::parse_outer)?;
let attrs = input.call(Attribute::parse_outer)?;
let vis = input.parse::<Visibility>()?;
let enum_token = input.parse::<Token![enum]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, brace_token, variants) =
derive::parsing::data_enum(input, &mut attrs)?;
let (where_clause, brace_token, variants) = derive::parsing::data_enum(input)?;
Ok(ItemEnum {
attrs,
vis,
Expand All @@ -2037,12 +2035,12 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ItemUnion {
fn parse(input: ParseStream) -> Result<Self> {
let mut attrs = input.call(Attribute::parse_outer)?;
let attrs = input.call(Attribute::parse_outer)?;
let vis = input.parse::<Visibility>()?;
let union_token = input.parse::<Token![union]>()?;
let ident = input.parse::<Ident>()?;
let generics = input.parse::<Generics>()?;
let (where_clause, fields) = derive::parsing::data_union(input, &mut attrs)?;
let (where_clause, fields) = derive::parsing::data_union(input)?;
Ok(ItemUnion {
attrs,
vis,
Expand Down

0 comments on commit be3238b

Please sign in to comment.