Skip to content

Commit 4d0519a

Browse files
committed
Remove error condition in parse_attribute
This function is only ever called when the `#` has already been consumed, no need to produce an error message here.
1 parent e1b1d7b commit 4d0519a

File tree

1 file changed

+12
-25
lines changed
  • compiler/rustc_parse/src/parser

1 file changed

+12
-25
lines changed

compiler/rustc_parse/src/parser/attr.rs

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,35 +124,22 @@ impl<'a> Parser<'a> {
124124
let lo = self.token.span;
125125
// Attributes can't have attributes of their own [Editor's note: not with that attitude]
126126
self.collect_tokens_no_attrs(|this| {
127-
if this.eat(&token::Pound) {
128-
let style = if this.eat(&token::Not) {
129-
ast::AttrStyle::Inner
130-
} else {
131-
ast::AttrStyle::Outer
132-
};
127+
assert!(this.eat(&token::Pound), "parse_attribute called in non-attribute position");
133128

134-
this.expect(&token::OpenDelim(Delimiter::Bracket))?;
135-
let item = this.parse_attr_item(false)?;
136-
this.expect(&token::CloseDelim(Delimiter::Bracket))?;
137-
let attr_sp = lo.to(this.prev_token.span);
129+
let style =
130+
if this.eat(&token::Not) { ast::AttrStyle::Inner } else { ast::AttrStyle::Outer };
138131

139-
// Emit error if inner attribute is encountered and forbidden.
140-
if style == ast::AttrStyle::Inner {
141-
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
142-
}
132+
this.expect(&token::OpenDelim(Delimiter::Bracket))?;
133+
let item = this.parse_attr_item(false)?;
134+
this.expect(&token::CloseDelim(Delimiter::Bracket))?;
135+
let attr_sp = lo.to(this.prev_token.span);
143136

144-
Ok(attr::mk_attr_from_item(
145-
&self.sess.attr_id_generator,
146-
item,
147-
None,
148-
style,
149-
attr_sp,
150-
))
151-
} else {
152-
let token_str = pprust::token_to_string(&this.token);
153-
let msg = &format!("expected `#`, found `{token_str}`");
154-
Err(this.struct_span_err(this.token.span, msg))
137+
// Emit error if inner attribute is encountered and forbidden.
138+
if style == ast::AttrStyle::Inner {
139+
this.error_on_forbidden_inner_attr(attr_sp, inner_parse_policy);
155140
}
141+
142+
Ok(attr::mk_attr_from_item(&self.sess.attr_id_generator, item, None, style, attr_sp))
156143
})
157144
}
158145

0 commit comments

Comments
 (0)