Skip to content

Commit 957986d

Browse files
committed
syntax: Support modern attribute syntax in the meta matcher
1 parent 535d474 commit 957986d

File tree

11 files changed

+68
-56
lines changed

11 files changed

+68
-56
lines changed

src/libsyntax/attr/mod.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -255,9 +255,8 @@ impl MetaItem {
255255
}
256256
}
257257

258-
impl Attribute {
259-
/// Extracts the `MetaItem` from inside this `Attribute`.
260-
pub fn meta(&self) -> Option<MetaItem> {
258+
impl AttrItem {
259+
crate fn meta(&self, span: Span) -> Option<MetaItem> {
261260
let mut tokens = self.tokens.trees().peekable();
262261
Some(MetaItem {
263262
path: self.path.clone(),
@@ -269,9 +268,16 @@ impl Attribute {
269268
} else {
270269
return None;
271270
},
272-
span: self.span,
271+
span,
273272
})
274273
}
274+
}
275+
276+
impl Attribute {
277+
/// Extracts the MetaItem from inside this Attribute.
278+
pub fn meta(&self) -> Option<MetaItem> {
279+
self.item.meta(self.span)
280+
}
275281

276282
pub fn parse<'a, T, F>(&self, sess: &'a ParseSess, mut f: F) -> PResult<'a, T>
277283
where F: FnMut(&mut Parser<'a>) -> PResult<'a, T>,
@@ -524,7 +530,7 @@ impl MetaItem {
524530
}
525531
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
526532
token::Nonterminal::NtIdent(ident, _) => Path::from_ident(ident),
527-
token::Nonterminal::NtMeta(ref meta) => return Some(meta.clone()),
533+
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
528534
token::Nonterminal::NtPath(ref path) => path.clone(),
529535
_ => return None,
530536
},

src/libsyntax/config.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,8 @@ impl<'a> StripUnconfigured<'a> {
122122

123123
while !parser.check(&token::CloseDelim(token::Paren)) {
124124
let lo = parser.token.span.lo();
125-
let (path, tokens) = parser.parse_meta_item_unrestricted()?;
126-
expanded_attrs.push((path, tokens, parser.prev_span.with_lo(lo)));
125+
let item = parser.parse_attr_item()?;
126+
expanded_attrs.push((item, parser.prev_span.with_lo(lo)));
127127
parser.expect_one_of(&[token::Comma], &[token::CloseDelim(token::Paren)])?;
128128
}
129129

@@ -150,8 +150,8 @@ impl<'a> StripUnconfigured<'a> {
150150
// `cfg_attr` inside of another `cfg_attr`. E.g.
151151
// `#[cfg_attr(false, cfg_attr(true, some_attr))]`.
152152
expanded_attrs.into_iter()
153-
.flat_map(|(path, tokens, span)| self.process_cfg_attr(ast::Attribute {
154-
item: ast::AttrItem { path, tokens },
153+
.flat_map(|(item, span)| self.process_cfg_attr(ast::Attribute {
154+
item,
155155
id: attr::mk_attr_id(),
156156
style: attr.style,
157157
is_sugared_doc: false,

src/libsyntax/ext/mbe/macro_parser.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ fn parse_nt(p: &mut Parser<'_>, sp: Span, name: Symbol) -> Nonterminal {
924924
FatalError.raise()
925925
}
926926
sym::path => token::NtPath(panictry!(p.parse_path(PathStyle::Type))),
927-
sym::meta => token::NtMeta(panictry!(p.parse_meta_item())),
927+
sym::meta => token::NtMeta(panictry!(p.parse_attr_item())),
928928
sym::vis => token::NtVis(panictry!(p.parse_visibility(true))),
929929
sym::lifetime => if p.check_lifetime() {
930930
token::NtLifetime(p.expect_lifetime().ident)

src/libsyntax/mut_visit.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,10 @@ pub fn noop_visit_interpolated<T: MutVisitor>(nt: &mut token::Nonterminal, vis:
682682
token::NtIdent(ident, _is_raw) => vis.visit_ident(ident),
683683
token::NtLifetime(ident) => vis.visit_ident(ident),
684684
token::NtLiteral(expr) => vis.visit_expr(expr),
685-
token::NtMeta(meta) => vis.visit_meta_item(meta),
685+
token::NtMeta(AttrItem { path, tokens }) => {
686+
vis.visit_path(path);
687+
vis.visit_tts(tokens);
688+
}
686689
token::NtPath(path) => vis.visit_path(path),
687690
token::NtTT(tt) => vis.visit_tt(tt),
688691
token::NtImplItem(item) =>

src/libsyntax/parse/attr.rs

+18-13
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ impl<'a> Parser<'a> {
9090
debug!("parse_attribute_with_inner_parse_policy: inner_parse_policy={:?} self.token={:?}",
9191
inner_parse_policy,
9292
self.token);
93-
let (span, path, tokens, style) = match self.token.kind {
93+
let (span, item, style) = match self.token.kind {
9494
token::Pound => {
9595
let lo = self.token.span;
9696
self.bump();
@@ -107,7 +107,7 @@ impl<'a> Parser<'a> {
107107
};
108108

109109
self.expect(&token::OpenDelim(token::Bracket))?;
110-
let (path, tokens) = self.parse_meta_item_unrestricted()?;
110+
let item = self.parse_attr_item()?;
111111
self.expect(&token::CloseDelim(token::Bracket))?;
112112
let hi = self.prev_span;
113113

@@ -142,7 +142,7 @@ impl<'a> Parser<'a> {
142142
}
143143
}
144144

145-
(attr_sp, path, tokens, style)
145+
(attr_sp, item, style)
146146
}
147147
_ => {
148148
let token_str = self.this_token_to_string();
@@ -151,7 +151,7 @@ impl<'a> Parser<'a> {
151151
};
152152

153153
Ok(ast::Attribute {
154-
item: ast::AttrItem { path, tokens },
154+
item,
155155
id: attr::mk_attr_id(),
156156
style,
157157
is_sugared_doc: false,
@@ -168,17 +168,17 @@ impl<'a> Parser<'a> {
168168
/// PATH
169169
/// PATH `=` TOKEN_TREE
170170
/// The delimiters or `=` are still put into the resulting token stream.
171-
pub fn parse_meta_item_unrestricted(&mut self) -> PResult<'a, (ast::Path, TokenStream)> {
172-
let meta = match self.token.kind {
171+
pub fn parse_attr_item(&mut self) -> PResult<'a, ast::AttrItem> {
172+
let item = match self.token.kind {
173173
token::Interpolated(ref nt) => match **nt {
174-
Nonterminal::NtMeta(ref meta) => Some(meta.clone()),
174+
Nonterminal::NtMeta(ref item) => Some(item.clone()),
175175
_ => None,
176176
},
177177
_ => None,
178178
};
179-
Ok(if let Some(meta) = meta {
179+
Ok(if let Some(item) = item {
180180
self.bump();
181-
(meta.path, meta.kind.tokens(meta.span))
181+
item
182182
} else {
183183
let path = self.parse_path(PathStyle::Mod)?;
184184
let tokens = if self.check(&token::OpenDelim(DelimToken::Paren)) ||
@@ -205,7 +205,7 @@ impl<'a> Parser<'a> {
205205
} else {
206206
TokenStream::empty()
207207
};
208-
(path, tokens)
208+
ast::AttrItem { path, tokens }
209209
})
210210
}
211211

@@ -273,9 +273,14 @@ impl<'a> Parser<'a> {
273273
_ => None,
274274
};
275275

276-
if let Some(meta) = nt_meta {
277-
self.bump();
278-
return Ok(meta);
276+
if let Some(item) = nt_meta {
277+
return match item.meta(item.path.span) {
278+
Some(meta) => {
279+
self.bump();
280+
Ok(meta)
281+
}
282+
None => self.unexpected(),
283+
}
279284
}
280285

281286
let lo = self.token.span;

src/libsyntax/parse/parser/path.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,9 @@ impl<'a> Parser<'a> {
114114
pub fn parse_path_allowing_meta(&mut self, style: PathStyle) -> PResult<'a, Path> {
115115
let meta_ident = match self.token.kind {
116116
token::Interpolated(ref nt) => match **nt {
117-
token::NtMeta(ref meta) => match meta.kind {
118-
ast::MetaItemKind::Word => Some(meta.path.clone()),
119-
_ => None,
117+
token::NtMeta(ref item) => match item.tokens.is_empty() {
118+
true => Some(item.path.clone()),
119+
false => None,
120120
},
121121
_ => None,
122122
},

src/libsyntax/parse/token.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -687,7 +687,7 @@ pub enum Nonterminal {
687687
NtLifetime(ast::Ident),
688688
NtLiteral(P<ast::Expr>),
689689
/// Stuff inside brackets for attributes
690-
NtMeta(ast::MetaItem),
690+
NtMeta(ast::AttrItem),
691691
NtPath(ast::Path),
692692
NtVis(ast::Visibility),
693693
NtTT(TokenTree),

src/libsyntax/print/pprust.rs

+22-18
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ fn token_to_string_ext(token: &Token, convert_dollar_crate: bool) -> String {
324324
crate fn nonterminal_to_string(nt: &Nonterminal) -> String {
325325
match *nt {
326326
token::NtExpr(ref e) => expr_to_string(e),
327-
token::NtMeta(ref e) => meta_item_to_string(e),
327+
token::NtMeta(ref e) => attr_item_to_string(e),
328328
token::NtTy(ref e) => ty_to_string(e),
329329
token::NtPath(ref e) => path_to_string(e),
330330
token::NtItem(ref e) => item_to_string(e),
@@ -412,8 +412,8 @@ pub fn meta_list_item_to_string(li: &ast::NestedMetaItem) -> String {
412412
to_string(|s| s.print_meta_list_item(li))
413413
}
414414

415-
pub fn meta_item_to_string(mi: &ast::MetaItem) -> String {
416-
to_string(|s| s.print_meta_item(mi))
415+
fn attr_item_to_string(ai: &ast::AttrItem) -> String {
416+
to_string(|s| s.print_attr_item(ai, ai.path.span))
417417
}
418418

419419
pub fn attribute_to_string(attr: &ast::Attribute) -> String {
@@ -629,24 +629,28 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
629629
ast::AttrStyle::Inner => self.word("#!["),
630630
ast::AttrStyle::Outer => self.word("#["),
631631
}
632-
self.ibox(0);
633-
match attr.tokens.trees().next() {
634-
Some(TokenTree::Delimited(_, delim, tts)) => {
635-
self.print_mac_common(
636-
Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span
637-
);
638-
}
639-
tree => {
640-
self.print_path(&attr.path, false, 0);
641-
if tree.is_some() {
642-
self.space();
643-
self.print_tts(attr.tokens.clone(), true);
644-
}
632+
self.print_attr_item(&attr.item, attr.span);
633+
self.word("]");
634+
}
635+
}
636+
637+
fn print_attr_item(&mut self, item: &ast::AttrItem, span: Span) {
638+
self.ibox(0);
639+
match item.tokens.trees().next() {
640+
Some(TokenTree::Delimited(_, delim, tts)) => {
641+
self.print_mac_common(
642+
Some(MacHeader::Path(&item.path)), false, None, delim, tts, true, span
643+
);
644+
}
645+
tree => {
646+
self.print_path(&item.path, false, 0);
647+
if tree.is_some() {
648+
self.space();
649+
self.print_tts(item.tokens.clone(), true);
645650
}
646651
}
647-
self.end();
648-
self.word("]");
649652
}
653+
self.end();
650654
}
651655

652656
fn print_meta_list_item(&mut self, item: &ast::NestedMetaItem) {

src/libsyntax_ext/cmdline_attrs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Attributes injected into the crate root from command line using `-Z crate-attr`.
22
3-
use syntax::ast::{self, AttrStyle};
3+
use syntax::ast::{self, AttrItem, AttrStyle};
44
use syntax::attr::mk_attr;
55
use syntax::panictry;
66
use syntax::parse::{self, token, ParseSess};
@@ -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 (path, tokens) = panictry!(parser.parse_meta_item_unrestricted());
18+
let AttrItem { path, tokens } = panictry!(parser.parse_attr_item());
1919
let end_span = parser.token.span;
2020
if parser.token != token::Eof {
2121
parse_sess.span_diagnostic

src/test/ui/cfg/cfg_stmt_expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ fn main() {
5757
// check that macro expanded code works
5858

5959
macro_rules! if_cfg {
60-
($cfg:meta $ib:block else $eb:block) => {
60+
($cfg:meta? $ib:block else $eb:block) => {
6161
{
6262
let r;
6363
#[cfg($cfg)]
@@ -69,7 +69,7 @@ fn main() {
6969
}
7070
}
7171

72-
let n = if_cfg!(unset {
72+
let n = if_cfg!(unset? {
7373
413
7474
} else {
7575
612

src/test/ui/macros/macro-first-set.rs

-6
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,6 @@ test_path!(::std);
252252
test_path!(std::u8,);
253253
test_path!(any, super, super::super::self::path, X<Y>::Z<'a, T=U>);
254254

255-
macro_rules! test_meta_block {
256-
($($m:meta)* $b:block) => {};
257-
}
258-
259-
test_meta_block!(windows {});
260-
261255
macro_rules! test_lifetime {
262256
(1. $($l:lifetime)* $($b:block)*) => {};
263257
(2. $($b:block)* $($l:lifetime)*) => {};

0 commit comments

Comments
 (0)