Skip to content

Commit 0cdd18d

Browse files
committed
pprust: Support macro macros
1 parent a2a1cd1 commit 0cdd18d

File tree

3 files changed

+31
-9
lines changed

3 files changed

+31
-9
lines changed

src/libsyntax/print/pprust.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ use syntax_pos::{DUMMY_SP, FileName, Span};
2020

2121
use std::borrow::Cow;
2222

23+
pub enum MacHeader<'a> {
24+
Path(&'a ast::Path),
25+
Keyword(&'static str),
26+
}
27+
2328
pub enum AnnNode<'a> {
2429
Ident(&'a ast::Ident),
2530
Name(&'a ast::Name),
@@ -620,7 +625,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
620625
match attr.tokens.trees().next() {
621626
Some(TokenTree::Delimited(_, delim, tts)) => {
622627
self.print_mac_common(
623-
Some(&attr.path), false, None, delim, tts, true, attr.span
628+
Some(MacHeader::Path(&attr.path)), false, None, delim, tts, true, attr.span
624629
);
625630
}
626631
tree => {
@@ -706,7 +711,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
706711

707712
fn print_mac_common(
708713
&mut self,
709-
path: Option<&ast::Path>,
714+
header: Option<MacHeader<'_>>,
710715
has_bang: bool,
711716
ident: Option<ast::Ident>,
712717
delim: DelimToken,
@@ -717,8 +722,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
717722
if delim == DelimToken::Brace {
718723
self.cbox(INDENT_UNIT);
719724
}
720-
if let Some(path) = path {
721-
self.print_path(path, false, 0);
725+
match header {
726+
Some(MacHeader::Path(path)) => self.print_path(path, false, 0),
727+
Some(MacHeader::Keyword(kw)) => self.word(kw),
728+
None => {}
722729
}
723730
if has_bang {
724731
self.word("!");
@@ -729,7 +736,7 @@ pub trait PrintState<'a>: std::ops::Deref<Target=pp::Printer> + std::ops::DerefM
729736
}
730737
match delim {
731738
DelimToken::Brace => {
732-
if path.is_some() || has_bang || ident.is_some() {
739+
if header.is_some() || has_bang || ident.is_some() {
733740
self.nbsp();
734741
}
735742
self.word("{");
@@ -1357,9 +1364,11 @@ impl<'a> State<'a> {
13571364
}
13581365
}
13591366
ast::ItemKind::MacroDef(ref macro_def) => {
1367+
let (kw, has_bang) =
1368+
if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) };
13601369
self.print_mac_common(
1361-
Some(&ast::Path::from_ident(ast::Ident::with_empty_ctxt(sym::macro_rules))),
1362-
true,
1370+
Some(MacHeader::Keyword(kw)),
1371+
has_bang,
13631372
Some(item.ident),
13641373
DelimToken::Brace,
13651374
macro_def.stream(),
@@ -1754,7 +1763,13 @@ impl<'a> State<'a> {
17541763

17551764
crate fn print_mac(&mut self, m: &ast::Mac) {
17561765
self.print_mac_common(
1757-
Some(&m.node.path), true, None, m.node.delim.to_token(), m.node.stream(), true, m.span
1766+
Some(MacHeader::Path(&m.node.path)),
1767+
true,
1768+
None,
1769+
m.node.delim.to_token(),
1770+
m.node.stream(),
1771+
true,
1772+
m.span,
17581773
);
17591774
}
17601775

src/test/pretty/macro.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// pp-exact
2+
3+
#![feature(decl_macro)]
4+
5+
macro mac { ($ arg : expr) => { $ arg + $ arg } }
6+
7+
fn main() { }

src/test/run-make-fulldeps/pretty-expanded-hygiene/input.pp.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![feature /* 0#0 */(no_core)]
33
#![no_core /* 0#0 */]
44

5-
macro_rules /* 0#0 */! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
5+
macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
66

77
fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }
88

0 commit comments

Comments
 (0)