Skip to content

Commit 5378955

Browse files
committed
syntax: Use ast::MacArgs for macro definitions
1 parent 1a496f3 commit 5378955

File tree

9 files changed

+37
-42
lines changed

9 files changed

+37
-42
lines changed

src/librustc/hir/lowering/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ impl LoweringContext<'_> {
233233

234234
if let ItemKind::MacroDef(ref def) = i.kind {
235235
if !def.legacy || attr::contains_name(&i.attrs, sym::macro_export) {
236-
let body = self.lower_token_stream(def.stream());
236+
let body = self.lower_token_stream(def.body.inner_tokens());
237237
let hir_id = self.lower_node_id(i.id);
238238
self.exported_macros.push(hir::MacroDef {
239239
name: ident.name,

src/librustc_lint/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ impl KeywordIdents {
14501450

14511451
impl EarlyLintPass for KeywordIdents {
14521452
fn check_mac_def(&mut self, cx: &EarlyContext<'_>, mac_def: &ast::MacroDef, _id: ast::NodeId) {
1453-
self.check_tokens(cx, mac_def.stream());
1453+
self.check_tokens(cx, mac_def.body.inner_tokens());
14541454
}
14551455
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) {
14561456
self.check_tokens(cx, mac.args.inner_tokens());

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ use syntax::source_map;
3232
use syntax::source_map::Spanned;
3333
use syntax::symbol::Symbol;
3434
use syntax::expand::allocator::AllocatorKind;
35+
use syntax::ptr::P;
36+
use syntax::tokenstream::DelimSpan;
3537
use syntax_pos::{Span, FileName};
3638

3739
macro_rules! provide {
@@ -427,6 +429,7 @@ impl CStore {
427429

428430
let source_file = sess.parse_sess.source_map().new_source_file(source_name, def.body);
429431
let local_span = Span::with_root_ctxt(source_file.start_pos, source_file.end_pos);
432+
let dspan = DelimSpan::from_single(local_span);
430433
let (body, mut errors) = source_file_to_stream(&sess.parse_sess, source_file, None);
431434
emit_unclosed_delims(&mut errors, &sess.parse_sess);
432435

@@ -448,7 +451,7 @@ impl CStore {
448451
span: local_span,
449452
attrs: attrs.iter().cloned().collect(),
450453
kind: ast::ItemKind::MacroDef(ast::MacroDef {
451-
tokens: body.into(),
454+
body: P(ast::MacArgs::Delimited(dspan, ast::MacDelimiter::Brace, body)),
452455
legacy: def.legacy,
453456
}),
454457
vis: source_map::respan(local_span.shrink_to_lo(), ast::VisibilityKind::Inherited),

src/librustc_parse/parser/item.rs

+19-21
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ use syntax::ast::{ItemKind, ImplItem, ImplItemKind, TraitItem, TraitItemKind, Us
88
use syntax::ast::{PathSegment, IsAuto, Constness, IsAsync, Unsafety, Defaultness, Extern, StrLit};
99
use syntax::ast::{Visibility, VisibilityKind, Mutability, FnHeader, ForeignItem, ForeignItemKind};
1010
use syntax::ast::{Ty, TyKind, Generics, TraitRef, EnumDef, Variant, VariantData, StructField};
11-
use syntax::ast::{Mac, Block, BindingMode, FnDecl, FnSig, SelfKind, Param};
11+
use syntax::ast::{Mac, MacArgs, MacDelimiter, Block, BindingMode, FnDecl, FnSig, SelfKind, Param};
1212
use syntax::print::pprust;
1313
use syntax::ptr::P;
1414
use syntax::ThinVec;
1515
use syntax::token;
16-
use syntax::tokenstream::{TokenTree, TokenStream};
16+
use syntax::tokenstream::{DelimSpan, TokenTree, TokenStream};
1717
use syntax::source_map::{self, respan, Span};
1818
use syntax::struct_span_err;
1919
use syntax_pos::BytePos;
@@ -1617,33 +1617,31 @@ impl<'a> Parser<'a> {
16171617
vis: &Visibility,
16181618
lo: Span
16191619
) -> PResult<'a, Option<P<Item>>> {
1620-
let token_lo = self.token.span;
16211620
let (ident, def) = if self.eat_keyword(kw::Macro) {
16221621
let ident = self.parse_ident()?;
1623-
let tokens = if self.check(&token::OpenDelim(token::Brace)) {
1624-
match self.parse_token_tree() {
1625-
TokenTree::Delimited(_, _, tts) => tts,
1626-
_ => unreachable!(),
1627-
}
1622+
let body = if self.check(&token::OpenDelim(token::Brace)) {
1623+
self.parse_mac_args()?
16281624
} else if self.check(&token::OpenDelim(token::Paren)) {
1629-
let args = self.parse_token_tree();
1625+
let params = self.parse_token_tree();
1626+
let pspan = params.span();
16301627
let body = if self.check(&token::OpenDelim(token::Brace)) {
16311628
self.parse_token_tree()
16321629
} else {
1633-
self.unexpected()?;
1634-
unreachable!()
1630+
return self.unexpected();
16351631
};
1636-
TokenStream::new(vec![
1637-
args.into(),
1638-
TokenTree::token(token::FatArrow, token_lo.to(self.prev_span)).into(),
1632+
let bspan = body.span();
1633+
let tokens = TokenStream::new(vec![
1634+
params.into(),
1635+
TokenTree::token(token::FatArrow, pspan.between(bspan)).into(),
16391636
body.into(),
1640-
])
1637+
]);
1638+
let dspan = DelimSpan::from_pair(pspan.shrink_to_lo(), bspan.shrink_to_hi());
1639+
P(MacArgs::Delimited(dspan, MacDelimiter::Brace, tokens))
16411640
} else {
1642-
self.unexpected()?;
1643-
unreachable!()
1641+
return self.unexpected();
16441642
};
16451643

1646-
(ident, ast::MacroDef { tokens: tokens.into(), legacy: false })
1644+
(ident, ast::MacroDef { body, legacy: false })
16471645
} else if self.check_keyword(sym::macro_rules) &&
16481646
self.look_ahead(1, |t| *t == token::Not) &&
16491647
self.look_ahead(2, |t| t.is_ident()) {
@@ -1653,12 +1651,12 @@ impl<'a> Parser<'a> {
16531651
self.bump();
16541652

16551653
let ident = self.parse_ident()?;
1656-
let args = self.parse_mac_args()?;
1657-
if args.need_semicolon() && !self.eat(&token::Semi) {
1654+
let body = self.parse_mac_args()?;
1655+
if body.need_semicolon() && !self.eat(&token::Semi) {
16581656
self.report_invalid_macro_expansion_item();
16591657
}
16601658

1661-
(ident, ast::MacroDef { tokens: args.inner_tokens(), legacy: true })
1659+
(ident, ast::MacroDef { body, legacy: true })
16621660
} else {
16631661
return Ok(None);
16641662
};

src/librustdoc/clean/inline.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ fn build_macro(cx: &DocContext<'_>, did: DefId, name: ast::Name) -> clean::ItemE
482482
match cx.enter_resolver(|r| r.cstore().load_macro_untracked(did, cx.sess())) {
483483
LoadedMacro::MacroDef(def, _) => {
484484
let matchers: hir::HirVec<Span> = if let ast::ItemKind::MacroDef(ref def) = def.kind {
485-
let tts: Vec<_> = def.stream().into_trees().collect();
485+
let tts: Vec<_> = def.body.inner_tokens().into_trees().collect();
486486
tts.chunks(4).map(|arm| arm[0].span()).collect()
487487
} else {
488488
unreachable!()

src/libsyntax/ast.rs

+1-7
Original file line numberDiff line numberDiff line change
@@ -1474,17 +1474,11 @@ impl MacDelimiter {
14741474
/// Represents a macro definition.
14751475
#[derive(Clone, RustcEncodable, RustcDecodable, Debug)]
14761476
pub struct MacroDef {
1477-
pub tokens: TokenStream,
1477+
pub body: P<MacArgs>,
14781478
/// `true` if macro was defined with `macro_rules`.
14791479
pub legacy: bool,
14801480
}
14811481

1482-
impl MacroDef {
1483-
pub fn stream(&self) -> TokenStream {
1484-
self.tokens.clone().into()
1485-
}
1486-
}
1487-
14881482
// Clippy uses Hash and PartialEq
14891483
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, Copy, Hash, PartialEq, HashStable_Generic)]
14901484
pub enum StrStyle {

src/libsyntax/mut_visit.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,8 @@ pub fn noop_visit_mac<T: MutVisitor>(mac: &mut Mac, vis: &mut T) {
586586
}
587587

588588
pub fn noop_visit_macro_def<T: MutVisitor>(macro_def: &mut MacroDef, vis: &mut T) {
589-
let MacroDef { tokens, legacy: _ } = macro_def;
590-
vis.visit_tts(tokens);
589+
let MacroDef { body, legacy: _ } = macro_def;
590+
visit_mac_args(body, vis);
591591
}
592592

593593
pub fn noop_visit_meta_list_item<T: MutVisitor>(li: &mut NestedMetaItem, vis: &mut T) {

src/libsyntax/print/pprust.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1380,8 +1380,8 @@ impl<'a> State<'a> {
13801380
Some(MacHeader::Keyword(kw)),
13811381
has_bang,
13821382
Some(item.ident),
1383-
DelimToken::Brace,
1384-
macro_def.stream(),
1383+
macro_def.body.delim(),
1384+
macro_def.body.inner_tokens(),
13851385
true,
13861386
item.span,
13871387
);

src/libsyntax_expand/mbe/macro_rules.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,8 @@ pub fn compile_declarative_macro(
318318
let tt_spec = ast::Ident::new(sym::tt, def.span);
319319

320320
// Parse the macro_rules! invocation
321-
let body = match def.kind {
322-
ast::ItemKind::MacroDef(ref body) => body,
321+
let (is_legacy, body) = match &def.kind {
322+
ast::ItemKind::MacroDef(macro_def) => (macro_def.legacy, macro_def.body.inner_tokens()),
323323
_ => unreachable!(),
324324
};
325325

@@ -338,7 +338,7 @@ pub fn compile_declarative_macro(
338338
mbe::TokenTree::MetaVarDecl(def.span, rhs_nm, tt_spec),
339339
],
340340
separator: Some(Token::new(
341-
if body.legacy { token::Semi } else { token::Comma },
341+
if is_legacy { token::Semi } else { token::Comma },
342342
def.span,
343343
)),
344344
kleene: mbe::KleeneToken::new(mbe::KleeneOp::OneOrMore, def.span),
@@ -350,7 +350,7 @@ pub fn compile_declarative_macro(
350350
DelimSpan::dummy(),
351351
Lrc::new(mbe::SequenceRepetition {
352352
tts: vec![mbe::TokenTree::token(
353-
if body.legacy { token::Semi } else { token::Comma },
353+
if is_legacy { token::Semi } else { token::Comma },
354354
def.span,
355355
)],
356356
separator: None,
@@ -360,7 +360,7 @@ pub fn compile_declarative_macro(
360360
),
361361
];
362362

363-
let argument_map = match parse(sess, body.stream(), &argument_gram, None, true) {
363+
let argument_map = match parse(sess, body, &argument_gram, None, true) {
364364
Success(m) => m,
365365
Failure(token, msg) => {
366366
let s = parse_failure_msg(&token);
@@ -435,7 +435,7 @@ pub fn compile_declarative_macro(
435435
// that is not lint-checked and trigger the "failed to process buffered lint here" bug.
436436
valid &= macro_check::check_meta_variables(sess, ast::CRATE_NODE_ID, def.span, &lhses, &rhses);
437437

438-
let (transparency, transparency_error) = attr::find_transparency(&def.attrs, body.legacy);
438+
let (transparency, transparency_error) = attr::find_transparency(&def.attrs, is_legacy);
439439
match transparency_error {
440440
Some(TransparencyError::UnknownTransparency(value, span)) =>
441441
diag.span_err(span, &format!("unknown macro transparency: `{}`", value)),

0 commit comments

Comments
 (0)