Skip to content

Commit 3815e91

Browse files
committed
Attach tokens to NtMeta (ast::AttrItem)
An `AttrItem` does not have outer attributes, so we only capture tokens when parsing a `macro_rules!` matcher
1 parent d5a04a9 commit 3815e91

File tree

9 files changed

+17
-6
lines changed

9 files changed

+17
-6
lines changed

compiler/rustc_ast/src/ast.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2419,6 +2419,7 @@ impl<D: Decoder> rustc_serialize::Decodable<D> for AttrId {
24192419
pub struct AttrItem {
24202420
pub path: Path,
24212421
pub args: MacArgs,
2422+
pub tokens: Option<TokenStream>,
24222423
}
24232424

24242425
/// A list of attributes.

compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ crate fn mk_attr_id() -> AttrId {
330330
}
331331

332332
pub fn mk_attr(style: AttrStyle, path: Path, args: MacArgs, span: Span) -> Attribute {
333-
mk_attr_from_item(style, AttrItem { path, args }, span)
333+
mk_attr_from_item(style, AttrItem { path, args, tokens: None }, span)
334334
}
335335

336336
pub fn mk_attr_from_item(style: AttrStyle, item: AttrItem, span: Span) -> Attribute {

compiler/rustc_ast/src/mut_visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -579,7 +579,7 @@ pub fn noop_visit_local<T: MutVisitor>(local: &mut P<Local>, vis: &mut T) {
579579
pub fn noop_visit_attribute<T: MutVisitor>(attr: &mut Attribute, vis: &mut T) {
580580
let Attribute { kind, id: _, style: _, span } = attr;
581581
match kind {
582-
AttrKind::Normal(AttrItem { path, args }) => {
582+
AttrKind::Normal(AttrItem { path, args, tokens: _ }) => {
583583
vis.visit_path(path);
584584
visit_mac_args(args, vis);
585585
}
@@ -709,7 +709,7 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
709709
token::NtLifetime(ident) => vis.visit_ident(ident),
710710
token::NtLiteral(expr) => vis.visit_expr(expr),
711711
token::NtMeta(item) => {
712-
let AttrItem { path, args } = item.deref_mut();
712+
let AttrItem { path, args, tokens: _ } = item.deref_mut();
713713
vis.visit_path(path);
714714
visit_mac_args(args, vis);
715715
}

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -967,6 +967,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
967967
AttrKind::Normal(ref item) => AttrKind::Normal(AttrItem {
968968
path: item.path.clone(),
969969
args: self.lower_mac_args(&item.args),
970+
tokens: None,
970971
}),
971972
AttrKind::DocComment(comment_kind, data) => AttrKind::DocComment(comment_kind, data),
972973
};

compiler/rustc_builtin_macros/src/cmdline_attrs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ pub fn inject(mut krate: ast::Crate, parse_sess: &ParseSess, attrs: &[String]) -
1515
);
1616

1717
let start_span = parser.token.span;
18-
let AttrItem { path, args } = match parser.parse_attr_item() {
18+
let AttrItem { path, args, tokens: _ } = match parser.parse_attr_item() {
1919
Ok(ai) => ai,
2020
Err(mut err) => {
2121
err.emit();

compiler/rustc_expand/src/expand.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1777,6 +1777,7 @@ impl<'a, 'b> MutVisitor for InvocationCollector<'a, 'b> {
17771777
kind: ast::AttrKind::Normal(AttrItem {
17781778
path: meta.path,
17791779
args: meta.kind.mac_args(meta.span),
1780+
tokens: None,
17801781
}),
17811782
span: at.span,
17821783
id: at.id,

compiler/rustc_parse/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
277277
Nonterminal::NtLifetime(ident) => {
278278
Some(tokenstream::TokenTree::token(token::Lifetime(ident.name), ident.span).into())
279279
}
280+
Nonterminal::NtMeta(ref attr) => attr.tokens.clone(),
280281
Nonterminal::NtTT(ref tt) => Some(tt.clone().into()),
281282
Nonterminal::NtExpr(ref expr) | Nonterminal::NtLiteral(ref expr) => {
282283
if expr.tokens.is_none() {

compiler/rustc_parse/src/parser/attr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ impl<'a> Parser<'a> {
162162
} else {
163163
let path = self.parse_path(PathStyle::Mod)?;
164164
let args = self.parse_attr_args()?;
165-
ast::AttrItem { path, args }
165+
ast::AttrItem { path, args, tokens: None }
166166
})
167167
}
168168

compiler/rustc_parse/src/parser/nonterminal.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,14 @@ impl<'a> Parser<'a> {
169169
}
170170
}
171171
NonterminalKind::Path => token::NtPath(self.parse_path(PathStyle::Type)?),
172-
NonterminalKind::Meta => token::NtMeta(P(self.parse_attr_item()?)),
172+
NonterminalKind::Meta => {
173+
let (mut attr, tokens) = self.collect_tokens(|this| this.parse_attr_item())?;
174+
// We may have eaten a nonterminal, which could already have tokens
175+
if attr.tokens.is_none() {
176+
attr.tokens = Some(tokens);
177+
}
178+
token::NtMeta(P(attr))
179+
}
173180
NonterminalKind::TT => token::NtTT(self.parse_token_tree()),
174181
NonterminalKind::Vis => token::NtVis(self.parse_visibility(FollowedByType::Yes)?),
175182
NonterminalKind::Lifetime => {

0 commit comments

Comments
 (0)