Skip to content

Commit c7b5567

Browse files
authored
Rollup merge of rust-lang#64486 - matthewjasper:hygiene-debugging, r=petrochenkov
Print out more information for `-Zunpretty=expanded,hygiene` I've found this helpful when trying to understand how hygiene works. Closes rust-lang#16420
2 parents 5283791 + 3c2fd1a commit c7b5567

File tree

6 files changed

+59
-6
lines changed

6 files changed

+59
-6
lines changed

src/librustc_driver/pretty.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ impl<'hir> pprust::PpAnn for IdentifiedAnnotation<'hir> {
326326
}
327327
fn post(&self, s: &mut pprust::State<'_>, node: pprust::AnnNode<'_>) {
328328
match node {
329+
pprust::AnnNode::Crate(_) |
329330
pprust::AnnNode::Ident(_) |
330331
pprust::AnnNode::Name(_) => {},
331332

@@ -431,14 +432,18 @@ impl<'a> pprust::PpAnn for HygieneAnnotation<'a> {
431432
match node {
432433
pprust::AnnNode::Ident(&ast::Ident { name, span }) => {
433434
s.s.space();
434-
// FIXME #16420: this doesn't display the connections
435-
// between syntax contexts
436435
s.synth_comment(format!("{}{:?}", name.as_u32(), span.ctxt()))
437436
}
438437
pprust::AnnNode::Name(&name) => {
439438
s.s.space();
440439
s.synth_comment(name.as_u32().to_string())
441440
}
441+
pprust::AnnNode::Crate(_) => {
442+
s.s.hardbreak();
443+
let verbose = self.sess.verbose();
444+
s.synth_comment(syntax_pos::hygiene::debug_hygiene_data(verbose));
445+
s.s.hardbreak_if_not_bol();
446+
}
442447
_ => {}
443448
}
444449
}

src/libsyntax/ast.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2387,7 +2387,7 @@ pub enum ItemKind {
23872387
),
23882388
/// A macro invocation.
23892389
///
2390-
/// E.g., `macro_rules! foo { .. }` or `foo!(..)`.
2390+
/// E.g., `foo!(..)`.
23912391
Mac(Mac),
23922392

23932393
/// A macro definition.

src/libsyntax/print/pprust.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub enum AnnNode<'a> {
3535
SubItem(ast::NodeId),
3636
Expr(&'a ast::Expr),
3737
Pat(&'a ast::Pat),
38+
Crate(&'a ast::Crate),
3839
}
3940

4041
pub trait PpAnn {
@@ -140,6 +141,7 @@ pub fn print_crate<'a>(cm: &'a SourceMap,
140141

141142
s.print_mod(&krate.module, &krate.attrs);
142143
s.print_remaining_comments();
144+
s.ann.post(&mut s, AnnNode::Crate(krate));
143145
s.s.eof()
144146
}
145147

@@ -1369,8 +1371,12 @@ impl<'a> State<'a> {
13691371
}
13701372
}
13711373
ast::ItemKind::MacroDef(ref macro_def) => {
1372-
let (kw, has_bang) =
1373-
if macro_def.legacy { ("macro_rules", true) } else { ("macro", false) };
1374+
let (kw, has_bang) = if macro_def.legacy {
1375+
("macro_rules", true)
1376+
} else {
1377+
self.print_visibility(&item.vis);
1378+
("macro", false)
1379+
};
13741380
self.print_mac_common(
13751381
Some(MacHeader::Keyword(kw)),
13761382
has_bang,

src/libsyntax_pos/hygiene.rs

+32
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,38 @@ pub fn update_dollar_crate_names(mut get_name: impl FnMut(SyntaxContext) -> Symb
343343
}))
344344
}
345345

346+
pub fn debug_hygiene_data(verbose: bool) -> String {
347+
HygieneData::with(|data| {
348+
if verbose {
349+
format!("{:#?}", data)
350+
} else {
351+
let mut s = String::from("");
352+
s.push_str("Expansions:");
353+
data.expn_data.iter().enumerate().for_each(|(id, expn_info)| {
354+
let expn_info = expn_info.as_ref().expect("no expansion data for an expansion ID");
355+
s.push_str(&format!(
356+
"\n{}: parent: {:?}, call_site_ctxt: {:?}, kind: {:?}",
357+
id,
358+
expn_info.parent,
359+
expn_info.call_site.ctxt(),
360+
expn_info.kind,
361+
));
362+
});
363+
s.push_str("\n\nSyntaxContexts:");
364+
data.syntax_context_data.iter().enumerate().for_each(|(id, ctxt)| {
365+
s.push_str(&format!(
366+
"\n#{}: parent: {:?}, outer_mark: ({:?}, {:?})",
367+
id,
368+
ctxt.parent,
369+
ctxt.outer_expn,
370+
ctxt.outer_transparency,
371+
));
372+
});
373+
s
374+
}
375+
})
376+
}
377+
346378
impl SyntaxContext {
347379
#[inline]
348380
pub const fn root() -> Self {

src/test/pretty/macro.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
#![feature(decl_macro)]
44

5-
macro mac { ($ arg : expr) => { $ arg + $ arg } }
5+
pub(crate) macro mac { ($ arg : expr) => { $ arg + $ arg } }
66

77
fn main() { }

src/test/ui/hygiene/unpretty-debug.stdout

+10
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ macro_rules! foo /* 0#0 */ { ($ x : ident) => { y + $ x } }
1313
fn bar /* 0#0 */() { let x /* 0#0 */ = 1; y /* 0#1 */ + x /* 0#0 */ }
1414

1515
fn y /* 0#0 */() { }
16+
17+
/*
18+
Expansions:
19+
0: parent: ExpnId(0), call_site_ctxt: #0, kind: Root
20+
1: parent: ExpnId(0), call_site_ctxt: #0, kind: Macro(Bang, foo)
21+
22+
SyntaxContexts:
23+
#0: parent: #0, outer_mark: (ExpnId(0), Opaque)
24+
#1: parent: #0, outer_mark: (ExpnId(1), SemiTransparent)
25+
*/

0 commit comments

Comments
 (0)